• Post Reply Bookmark Topic Watch Topic
  • New Topic

spliting a string  RSS feed

 
dale conn
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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/
 
Danish Araquei
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
}
 
Nick Meverden
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
dale conn
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
many thanks for the replies
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool beans. I don't trust my regex chops enough to try that first. Guess I oughtta work on that.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!