This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SubString Question

 
Rekha Pande
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

Seprating of a string having a dot operator
Eg: String all = "NAME.PHONE.CODE.COUNTRY",
How would i go about it...

Means i want to get
String name = all.subString(???); will be returning the NAME part
String phone = all.subString(???); will be returning the PHONE part
String code = all.subString(???); will be returning the CODE part
String country = all.subString(???); will be returning the COUNTRY part


Can anyone help me on this???

Thanxs in advance.
 
Steve McCann
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rekha

Use the String indexOf() method to find the positions of the "." inside your string. Then you can use the substring() method to get the parts you need, e.g.


Check out the Java tutorial for more info on substrings etchere

And the full tutorial:Java Tutorial

Steve
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You better use the String Tokenizer for that.
It would look like this: (token would be "." in your case)


Additionally you should check if there is really a next token.
Best check the Javadocs for infos about the StringTokenizer. As far
as i remember there are also examples how to use it.

Olli




Originally posted by Rekha Pande:
Hello all,

Seprating of a string having a dot operator
Eg: String all = "NAME.PHONE.CODE.COUNTRY",
How would i go about it...

Means i want to get
String name = all.subString(???); will be returning the NAME part
String phone = all.subString(???); will be returning the PHONE part
String code = all.subString(???); will be returning the CODE part
String country = all.subString(???); will be returning the COUNTRY part


Can anyone help me on this???

Thanxs in advance.
 
madhu gadde
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
upto what i know u can use the StringTokenizer.just check the API for StringTokenizer,there are some methods which will help you in getting substrings.
 
Rekha Pande
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your prompt reply.
I tried the String Tokenizer method and it works just perfectly...

Thank u all once again.

 
Steve McCann
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringTokenizer method is a better way than mine, I think.

Steve
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using j2sdk1.4 or higher you can take advantage of the new regexp methods in the String object and just use String.split(regexp). Which will break the string into an array on whatever expression you use as your argument.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65220
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to Java in General (i).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic