File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Substring() equivalent on numbers" Watch "Substring() equivalent on numbers" New topic

Substring() equivalent on numbers

sherry cuenco

Joined: May 07, 2003
Posts: 24
Is there an equivalent of substring() on primitive numbers? Given an int = 12345, I would like to extract let's say, 34 from it.
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);

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

Joined: Mar 25, 2001
Posts: 1779
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

Joined: May 07, 2003
Posts: 24
Thank you for your suggestions gentlemen.
It is sorta covered in the JavaRanch Style Guide.
subject: Substring() equivalent on numbers
It's not a secret anymore!