Forums Register Login

spliting a string

+Pie Number of slices to send: Send
hi all i have a string like this i'm trying to split it so i get just the values, i don't want the the id1, id2, id3, id4

i just want these parts
1c56c60:10ba45927f6:-7fff
1c56c60:10ba45927f6:-7ffe
1c56c60:10ba45927f6:-7ffd
1c56c60:10b

[id1:1c56c60:10ba45927f6:-7fff][id2:1c56c60:10ba45927f6:-7ffe][id3:1c56c60:10ba45927f6:-7ffd][id4:1c56c60:10b]

can anyone help me out

many thanks
+Pie Number of slices to send: Send
Look into String.split(). You could call it once to split a bunch of bracketed sections into an array. Then you could use split again or maybe just some basic indexOf() and substring() stuff to to pull off the idx: part. If you're not familiar with all these methods look them up in the JavaDoc, try a little bit of code and show us how it goes.

http://java.sun.com/j2se/1.5.0/docs/api/
+Pie Number of slices to send: Send
class StringSplit {
public static void main(String args[]){
String str ="[id1:1c56c60:10ba45927f6:-7fff][id2:1c56c60:10ba45927f6:-7ffe][id3:1c56c60:10ba45927f6:-7ffd][id4:1c56c60:10b]";
for (int i=0;i<str.length()-1;i++ ){
if (str.charAt(i)=='[') {
String str1=null;
while (!(str.charAt(i)==']')){
str1=str1+(str.charAt(i));

i++;
} // End Of While
System.out.println(str1.substring(9));
} // End Of If
} // End Of For Loop
} // End of Main
}
+Pie Number of slices to send: Send
String input = "[id1:1c56c60:10ba45927f6:-7fff][id2:1c56c60:10ba45927f6:-7ffe][id3:1c56c60:10ba45927f6:-7ffd][id4:1c56c60:10b]";
String[] split = input.split("\\]\\[");

for (int x = 0; x < split.length; x++) {
String s = split[x].trim();

if (s.startsWith("[")) {
s = s.substring(1, s.length());
}

if (s.endsWith("]")) {
s = s.substring(0, (s.length() - 1));
}

java.util.regex.Matcher m = java.util.regex.Pattern.compile("^id\\w\\ .+)$").matcher(s);
boolean matches = m.matches();

if (matches) {
s = m.group(1);
}

split[x] = s;
}

//at this point you have String[] containing everything you want.
for (int x = 0; x < split.length; x++) {
System.out.println(split[x]);
}

Not pretty but works, had some issues trying to match \\[id\\w\\ .+)\\]?+ but my regex skills in Java aren't that strong.
+Pie Number of slices to send: Send
many thanks for the replies
+Pie Number of slices to send: Send
Using a combination of Regular Expressions, splits, and substrings may be a bit of an overkill.

Try...



Henry
[ July 05, 2006: Message edited by: Henry Wong ]
+Pie Number of slices to send: Send
Cool beans. I don't trust my regex chops enough to try that first. Guess I oughtta work on that.
If you are using a wood chipper, you are doing it wrong. Even on this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1725 times.
Similar Threads
Garbage Collection problem-2
Concatenating Nodes into CSV format
argument in form access
How to copy a value to the same page?
Custom Class as Key for Hash Map
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:57:28.