• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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: 23919
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.
 
The government thinks you are too stupid to make your own lightbulb choices. But this tiny ad thinks you are smart:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic