File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Extract values from a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Extract values from a string" Watch "Extract values from a string" New topic
Author

Extract values from a string

thomas colding
Ranch Hand

Joined: Mar 23, 2007
Posts: 60
Very simple Qustion how to Extract all the values from following String that is seperated with the Sign '#'.
One#Two#Tree#Four#Five.

Thanks.
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
Could you be a little more specific as to what you would like to accomplish?

If you are trying to get the substrings (one.tow, etc) then you could use a StringTokenizer...
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2311
    
  49
You could use StringTokenizer but the preferred method these days is to use the String.split() method. This uses regular expressions to split the string and returns an array of the parts.

For example:

Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
Thanks Tony, I was unaware of this improvement in the language
thomas colding
Ranch Hand

Joined: Mar 23, 2007
Posts: 60
Exactly what I would like to do.
but I need to Put each value in a new Variable as following af after I have split it.

String Val1=One
String Val2=Two
String Val3=Tree
String Val4=Four
String Val5=Five

the mention Split-object will put all the values in a array .
How do I extract the each value and put them in to a string.
[ August 18, 2007: Message edited by: thomas colding ]
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Why would you prefer not to reference the results by the array index? Anyway if you must you could do something like this:


But seriously, I don't see what is gained by doing this.
[ August 18, 2007: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
thomas colding
Ranch Hand

Joined: Mar 23, 2007
Posts: 60
Lake off Java-knowledge, after a little Weekend study I found out that it is possible to ref. drirect to a Array.
And I can not use Split-method since my Java-version is 1.4.1
Thanks for the Guidens everybody,
You have to Crawl before you can Walk, I am just in the first Phase:-)

[ August 20, 2007: Message edited by: thomas colding ]
[ August 20, 2007: Message edited by: thomas colding ]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15
Originally posted by thomas colding:
And I can not use Split-method since my Java-version is 1.4.1


String.split() was introduced in 1.4, so you can use it.


Joanne
 
jQuery in Action, 2nd edition
 
subject: Extract values from a string