This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Substring() equivalent on numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Substring() equivalent on numbers" Watch "Substring() equivalent on numbers" New topic
Author

Substring() equivalent on numbers

sherry cuenco
Greenhorn

Joined: May 07, 2003
Posts: 24
Hello,
Is there an equivalent of substring() on primitive numbers? Given an int = 12345, I would like to extract let's say, 34 from it.
Thanks!
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
You can first convert your number to a String and then use substring.
int i = 12345;
String s = Integer.toString(i);
System.out.println(s.substring(2,3));

But maybe a String would be better from beginning on for your purpose cause this operation makes no sense for a number.
Olli
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1740
    
    2
The division (/) and remainder (%) operators can be useful too when doing this sort of thing. And remember that your integer is base 10.
sherry cuenco
Greenhorn

Joined: May 07, 2003
Posts: 24
Thank you for your suggestions gentlemen.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Substring() equivalent on numbers
 
Similar Threads
Coding - Decoding
how to extract two numbers from a string
How to iterate a Set (2 steps once)
How to parse the hours and mins string?
subString