Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Extract values from a string

 
thomas colding
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2957
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tony, I was unaware of this improvement in the language
 
thomas colding
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
thomas colding
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic