aspose file tools*
The moose likes Beginning Java and the fly likes Extracting certain values from a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Extracting certain values from a string" Watch "Extracting certain values from a string" New topic
Author

Extracting certain values from a string

Karoline Lim
Greenhorn

Joined: Oct 14, 2011
Posts: 12
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

Joined: Apr 01, 2011
Posts: 76

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

Joined: Oct 14, 2011
Posts: 12
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

Joined: Nov 19, 2010
Posts: 5
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

Joined: Oct 14, 2011
Posts: 12
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

Joined: Jul 18, 2011
Posts: 287
Sean Brewer
Greenhorn

Joined: Nov 19, 2010
Posts: 5
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

Joined: Oct 13, 2005
Posts: 36508
    
  16
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

Joined: Oct 13, 2005
Posts: 36508
    
  16
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

Joined: Oct 14, 2011
Posts: 12
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
 
subject: Extracting certain values from a string
 
Similar Threads
Read integers from a file
String replace
INSERT Issue
String.replace to remove a character
Array question