| | |
String splitting to array...
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2009
Posts: 10
Reputation:
Solved Threads: 0
I'm trying to split an input string into an array of substrings and currently trying something like this...
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
Java Syntax (Toggle Plain Text)
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
•
•
Join Date: Sep 2008
Posts: 1,598
Reputation:
Solved Threads: 202
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.
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.
•
•
Join Date: Oct 2009
Posts: 10
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Sep 2008
Posts: 1,598
Reputation:
Solved Threads: 202
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
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.
Java Syntax (Toggle Plain Text)
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.
Out.
•
•
Join Date: Oct 2009
Posts: 10
Reputation:
Solved Threads: 0
0
#5 31 Days Ago
Java Syntax (Toggle Plain Text)
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
2
#7 31 Days Ago
Using the pattern
Edit: This 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)
Pattern pat = Pattern.compile("\\w+?[RK]|\\G\\w++$"); String input = "AMLARMLAKFGFP"; Matcher matcher = pat.matcher(input); while (matcher.find()){ System.out.println(matcher.group()); }
Last edited by Ezzaral; 31 Days Ago at 5:02 pm.
•
•
Join Date: Oct 2009
Posts: 10
Reputation:
Solved Threads: 0
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
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

•
•
•
•
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)
Pattern pat = Pattern.compile("\\w+?[RK]|\\G\\w++$"); String input = "AMLARMLAKFGFP"; Matcher matcher = pat.matcher(input); while (matcher.find()){ System.out.println(matcher.group()); }
![]() |
Similar Threads
- Split string into char array (C)
- Converting String^ to Int array[] or char array[] (C++)
- convert string to int array (C++)
- extracting sustrings from a string and store it in an array (ASP.NET)
- Help Needed ASAP to read csv & populate a String Multidimensional Array (Java)
- Problems from string to int array (C)
- Coverting a string to a byte array (Visual Basic 4 / 5 / 6)
Other Threads in the Java Forum
- Previous Thread: Merge file program compiles but not run.
- Next Thread: location of the 'outfile'
| Thread Tools | Search this Thread |
addball android api applet application apps array arrays automation binary bluetooth businessintelligence button card chat class classes client code collision component crashcourse css csv database eclipse ee error event exception fractal free game gis givemetehcodez graphics gui html ide image input integer integration j2me java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm key linux list loan loop machine map method methods migrate mobile netbeans newbie nls oracle output phone physics print problem program programming project radio recursion reporting scanner screen server service set size sms socket software sort sql string swing textfield threads time transfer tree trolltech utility windows






