• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

spliting a string

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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/
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
}
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
many thanks for the replies
 
author
Posts: 23949
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
What are you saying? I thought you said that Santa gave you that. And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic