I'm trying to split an input string into an array of substrings and currently trying something like this...

public static void tryp(String inputString){	
		int index = 0;
		
		String[] rArray = new String[1000];
		String[] kArray = new String[1000];
			
		int startPos = 0;
		int x;
		int newRPos = 0;
		int newKPos = 0;
		String protString;
		
		while (newRPos != -1){
			
		for (x = 0; x < inputString.length(); x++){
				
		int ind = 0;
				
	newRPos = inputString.indexOf('R');
				
String nextRPep = inputString.substring(startPos, (newRPos+1));
protString = inputString.substring(newRPos+1, inputString.length());
		rArray[ind] = nextRPep;
		ind++;
		startPos = newRPos+1;
		newRPos = protString.indexOf('R');
		rArray[ind+1] = protString;
	        }
			
		for (index = 0; index < rArray.length; index++){
			
		newKPos = inputString.indexOf('K');	
			
		while (newKPos != -1){
					
		for (x = 0; x < rArray.length; x++){
				
	        int ind = 0;
				
		String rPep = rArray[x];
		
                String nextPep = rPep.substring(startPos, (newKPos+1));
		protString = rPep.substring(newKPos+1, rPep.length());
		kArray[ind] = nextPep;
		ind++;
		startPos = newKPos+1;
		newKPos = protString.indexOf('K');
		kArray[ind+1] = protString;
	}
	}
	}
}

This compiles but doesn't do what I want....?

Any help much appreciated as I can't think what else I can try (have tried lots that didn't help!)

Thanks

It'd be extremely helpful if you mentioned how you want to split the Strings into substrings (e.g., do you want them to be split around spaces? Around a particular letter? Or what?)

If you read the String class documentation you'll see a method called split() that takes a regular expression as an argument; the method splits the String around matches of the regular expression. So if you were to create a regular expression that matched against the letter "K", then passed that into myString.split(regexForK), then that method call would return a String[], which is exactly what you want.

I want to split the strings after R and K, retaining those characters. I read about split but it said you can't retain the delimitters when using .split. I tried a tokenizer method which worked quite well, but now have to work out a method using arrays which as you can see is proving v difficult.

Thanks for replying.

It'd be extremely helpful if you mentioned how you want to split the Strings into substrings (e.g., do you want them to be split around spaces? Around a particular letter? Or what?)

If you read the String class documentation you'll see a method called split() that takes a regular expression as an argument; the method splits the String around matches of the regular expression. So if you were to create a regular expression that matched against the letter "K", then passed that into myString.split(regexForK), then that method call would return a String[], which is exactly what you want.

If you want a less elegant solution, then simply call String.split() two times on your String. The first time split on R, and the next time split() on K. You already know where your R and K are because they are at the index of the original string where the array was split around. So if you wanted to print out a list of where R was, do something like

String[] wheresR = myString.split(splitOnR);
int index = 0;
for (String str: wheresR){
index+=str.length();
System.out.println(index);
}

Then I suppose you could have the letter at the beginning or at the end of the String and you wouldn't be sure, but you can check for that as well by using charAt.

String[] wheresR = myString.split(splitOnR);
int index = 0;
for (String str: wheresR){
index+=str.length();
System.out.println(index);
}

Say myString was AMLARMLAKFGFP .... would this return AMLAR, MLAK, FGFP ? Or would I have to reinsert the R/Ks separately?

Thanks

Like the docs say, split does not return the character that was split around. My point in my previous post was that you can easily figure out where the characters were anyway by using a little intuition.

Using the pattern (\w+?[RK]) with the regex Matcher.find() method will capture all of those groups except for the last one, which doesn't end with R or K.

Edit: This pattern \w+?[RK]|\G\w++$ seems to produce the result you wanted.

Pattern pat = Pattern.compile("\\w+?[RK]|\\G\\w++$");
String input = "AMLARMLAKFGFP";
Matcher matcher = pat.matcher(input);
while (matcher.find()){
    System.out.println(matcher.group());
}

Omg it works! Thank you so much :) It also does seem to return the last sequence (FGFP), at least for print out. Can I insert the matcher groups into a string array using something like

String trypArray = new String[1000];
int indx = 0;

while (matcher.find()){
      trypArray[indx] = (matcher.group());
      indx++
}

and/or is there a better/simpler way to pass the result of matcher.group() to another method (in my case where the weight of the string is calculated using key/values from a hashmap)?

Many many thanks :D

Had a little read and just calling the method on matcher.group() seems to work :)

Many thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.