• 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

Extracting certain values from a string

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, i'm a newbie in Java.

Given a string,

st,1=4 st,2=15 st,3=24

How am I going to extract only 4,15, and 24. which means, I'm only interested in getting the values after "=".

I've looked at .split, Pattern.compile etc. but I still can not get [4,15,24] as wanted.

Thanks in advance.

 
Ranch Hand
Posts: 83
Netbeans IDE MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tell the correct string like
String str = ""

and the pattern in which you want to extract

give one example which correctly illustrate your intention
 
Karoline Lim
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vinayak jog wrote:Tell the correct string like
String str = ""

and the pattern in which you want to extract

give one example which correctly illustrate your intention



Given,

String str = "st,1=4 st,2=15 st,3=24 ";

The output I would like to have is = [4,15,24].

So one of the ways I tried is




and this is the output I got,

[ , , ,1, 4 , , ,2, 15 , , ,3, 24], which eliminates 'st' and '=" apparently. I'm not sure what's the pattern should I put in to get only the values after "=".

thanks.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use StringTokenizer and kind of fiddle with the other formatting to get it to look the way you want.

I put it inside of a function that you can call whenever you want to extract data from a string of the specific format that you gave.



The output for your example string is:
[4,15,24]
 
Karoline Lim
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sean Brewer wrote:You can use StringTokenizer and kind of fiddle with the other formatting to get it to look the way you want.

I put it inside of a function that you can call whenever you want to extract data from a string of the specific format that you gave.



The output for your example string is:
[4,15,24]





Thank you so much Sean! It works.
I was still trying hard using .indexOf.
Thank you so much!
 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Sean Brewer
Greenhorn
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

lim kk wrote:Thank you so much Sean! It works.
I was still trying hard using .indexOf.
Thank you so much!


You're welcome. Glad that helped.

You probably could get it to work with indexOf, but the problem is you would only be able to do it on this "one" particular string since you need to know ahead of time the string that you are searching for. Using StringTokenizer will work for any generic string with the format "st,X=Y " so it can have any number of strings with different values and it will still work as long as it is in that format that you specified.
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But StringTokenizer is legacy code, which you ought not to use.
How about splitting on "\\sst,\\d+=" in the String.split method? You may have to escape the = to \\=, since = is a meta-character.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also seek the indices of = and s, and take substrings. Beware of seeking an index. If you find the index of something is 123, and seek for indexOf(something, 123), you will get 123. For subsequent matches, start from 124.
 
Karoline Lim
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Harsha and Campbell, I will certainly look into that. Very much appreciated!
Really need to polish up both the knowledge and skill on Java..
Thanks a lot again Sean!
 
This will take every ounce of my mental strength! All for a 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