Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there such thing as math.substring in java?

Okay, I'm just getting curious. But I was wondering if there was such thing as a substring for numbers (math.substring ?), and then it would get the character(s) in the position specified.

Example (really poorly thought out one)

int number = 5839;
int example = number.substring(0,1)
println = example;

and then it displays 5?

like image 364
Kalvin Avatar asked Sep 22 '26 04:09

Kalvin


1 Answers

Why don't you just convert it to a string, and then call substring(0,1)?...

int number = 5839;
int example = Integer.parseInt((number+"").substring(0,1));

Where calling number+"" causes the JVM to convert it into a String, and then it calls substring() on it. You then finally parse it back into an int

like image 148
wattostudios Avatar answered Sep 24 '26 17:09

wattostudios



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!