• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting string from a string  RSS feed

 
Pol Appan
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I need to get string from a string
Eg
This is my string
Development - 123
Support- 456

I need to get the last numbers from the string, like 123, 456 etc.

How can I acheive this?

Thanks
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are plenty of helpful methods in the String class. indexOf and substring are probably worth looking at.
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Probably , you need to know about sub String method.
 
Sultan Khadar
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cosmo,
Try this

String yourString = "Developement-123";
String desiredString = yourString.substring(13,15);

In the above code , 13 is the starting position , from where you want to extract the string and 15 is the end position, up to where you can get the String. Also have a look at java.lang.String API to know more such methods.

Regards,
Sultan.
 
vishnuvardhan chowdary
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I think you can use stringtokenizers, - as delimiter for every string or
you can use charAt() and compare it with numbers. then use subString.
I think they will work.

Regards
Vishnu
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cosmos,


are you sure you want to get a String from a String?
Your example looks more like you want to get an int from a String.
In this case, you can also use a scanner. Scanners can get all number formats (ints, doubles, ...) from a String that's gonna be tokenized. Default delimiter is a whitespace.

See example:



If you throw an exception or return a 0 when no int is in your String is up to you.

Yours,
Bu.
[ September 26, 2006: Message edited by: Burkhard Hassel ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!