File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Extracting certain values from a string

 
Karoline Lim
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • 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.

 
vinayak jog
Ranch Hand
Posts: 83
MySQL Database Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Sean Brewer
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
Harsha Smith
Ranch Hand
Posts: 287
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Sean Brewer
Greenhorn
Posts: 7
  • 1
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 1
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 47300
52
  • 1
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic