aspose file tools*
The moose likes Java in General and the fly likes SubString Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "SubString Question" Watch "SubString Question" New topic
Author

SubString Question

Rekha Pande
Ranch Hand

Joined: Jan 29, 2004
Posts: 145
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.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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.
Steve McCann
Ranch Hand

Joined: Oct 20, 2004
Posts: 81
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
madhu gadde
Greenhorn

Joined: May 27, 2005
Posts: 11
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

Joined: Jan 29, 2004
Posts: 145
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

Joined: Oct 20, 2004
Posts: 81
StringTokenizer method is a better way than mine, I think.

Steve
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Moved to Java in General (i).


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SubString Question