• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

SubString Question

 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
StringTokenizer method is a better way than mine, I think.

Steve
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67645
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moved to Java in General (i).
 
reply
    Bookmark Topic Watch Topic
  • New Topic