944,220 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 829
  • Java RSS
Nov 3rd, 2009
0

String splitting to array...

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

Java Syntax (Toggle Plain Text)
  1. public static void tryp(String inputString){
  2. int index = 0;
  3.  
  4. String[] rArray = new String[1000];
  5. String[] kArray = new String[1000];
  6.  
  7. int startPos = 0;
  8. int x;
  9. int newRPos = 0;
  10. int newKPos = 0;
  11. String protString;
  12.  
  13. while (newRPos != -1){
  14.  
  15. for (x = 0; x < inputString.length(); x++){
  16.  
  17. int ind = 0;
  18.  
  19. newRPos = inputString.indexOf('R');
  20.  
  21. String nextRPep = inputString.substring(startPos, (newRPos+1));
  22. protString = inputString.substring(newRPos+1, inputString.length());
  23. rArray[ind] = nextRPep;
  24. ind++;
  25. startPos = newRPos+1;
  26. newRPos = protString.indexOf('R');
  27. rArray[ind+1] = protString;
  28. }
  29.  
  30. for (index = 0; index < rArray.length; index++){
  31.  
  32. newKPos = inputString.indexOf('K');
  33.  
  34. while (newKPos != -1){
  35.  
  36. for (x = 0; x < rArray.length; x++){
  37.  
  38. int ind = 0;
  39.  
  40. String rPep = rArray[x];
  41.  
  42. String nextPep = rPep.substring(startPos, (newKPos+1));
  43. protString = rPep.substring(newKPos+1, rPep.length());
  44. kArray[ind] = nextPep;
  45. ind++;
  46. startPos = newKPos+1;
  47. newKPos = protString.indexOf('K');
  48. kArray[ind+1] = protString;
  49. }
  50. }
  51. }
  52. }

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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
skiplatte is offline Offline
10 posts
since Oct 2009
Nov 3rd, 2009
0
Re: String splitting to array...
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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 4th, 2009
0
Re: String splitting to array...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
skiplatte is offline Offline
10 posts
since Oct 2009
Nov 4th, 2009
0
Re: String splitting to array...
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

Java Syntax (Toggle Plain Text)
  1. String[] wheresR = myString.split(splitOnR);
  2. int index = 0;
  3. for (String str: wheresR){
  4. index+=str.length();
  5. System.out.println(index);
  6. }

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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 5th, 2009
0
Re: String splitting to array...
Java Syntax (Toggle Plain Text)
  1. String[] wheresR = myString.split(splitOnR);
  2. int index = 0;
  3. for (String str: wheresR){
  4. index+=str.length();
  5. System.out.println(index);
  6. }

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

Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
skiplatte is offline Offline
10 posts
since Oct 2009
Nov 5th, 2009
1
Re: String splitting to array...
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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 5th, 2009
2
Re: String splitting to array...
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.
Java Syntax (Toggle Plain Text)
  1. Pattern pat = Pattern.compile("\\w+?[RK]|\\G\\w++$");
  2. String input = "AMLARMLAKFGFP";
  3. Matcher matcher = pat.matcher(input);
  4. while (matcher.find()){
  5. System.out.println(matcher.group());
  6. }
Last edited by Ezzaral; Nov 5th, 2009 at 5:02 pm.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Nov 6th, 2009
0
Re: String splitting to array...
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

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

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

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

Click to Expand / Collapse  Quote originally posted by Ezzaral ...
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.
Java Syntax (Toggle Plain Text)
  1. Pattern pat = Pattern.compile("\\w+?[RK]|\\G\\w++$");
  2. String input = "AMLARMLAKFGFP";
  3. Matcher matcher = pat.matcher(input);
  4. while (matcher.find()){
  5. System.out.println(matcher.group());
  6. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
skiplatte is offline Offline
10 posts
since Oct 2009
Nov 6th, 2009
0
Re: String splitting to array...
Had a little read and just calling the method on matcher.group() seems to work

Many thanks again.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
skiplatte is offline Offline
10 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Merge file program compiles but not run.
Next Thread in Java Forum Timeline: location of the 'outfile'





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC