String splitting to array...

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2009
Posts: 10
Reputation: skiplatte is an unknown quantity at this point 
Solved Threads: 0
skiplatte skiplatte is offline Offline
Newbie Poster

String splitting to array...

 
0
  #1
33 Days Ago
I'm trying to split an input string into an array of substrings and currently trying something like this...

  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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,598
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 202
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #2
32 Days Ago
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.
Out.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: skiplatte is an unknown quantity at this point 
Solved Threads: 0
skiplatte skiplatte is offline Offline
Newbie Poster
 
0
  #3
32 Days Ago
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.

Originally Posted by BestJewSinceJC View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,598
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 202
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #4
32 Days Ago
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

  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.
Out.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: skiplatte is an unknown quantity at this point 
Solved Threads: 0
skiplatte skiplatte is offline Offline
Newbie Poster
 
0
  #5
31 Days Ago
  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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,598
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 202
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
1
  #6
31 Days Ago
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.
Out.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster
 
2
  #7
31 Days Ago
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.
  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; 31 Days Ago at 5:02 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: skiplatte is an unknown quantity at this point 
Solved Threads: 0
skiplatte skiplatte is offline Offline
Newbie Poster
 
0
  #8
30 Days Ago
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

Originally Posted by Ezzaral View Post
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.
  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. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: skiplatte is an unknown quantity at this point 
Solved Threads: 0
skiplatte skiplatte is offline Offline
Newbie Poster
 
0
  #9
30 Days Ago
Had a little read and just calling the method on matcher.group() seems to work

Many thanks again.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC