简体   繁体   中英

Convert string with prepended 0s to Int in Scala

I'm looking for a simple solution to convert a String in Scala with prepended 0s into an Integer. The values were given 0s when converted to Strings to all have the same length, but I would like a simple solution to convert them back.

Sample data:

00001234
00123456
12345678

Sample output:

1234
123456
12345678

I have tried "number".toInt, but I receive a java.lang.NumberFormatException for this.

Any simple solutions?

Try

"00001234".dropWhile(_ == '0').toInt

Just "00001234".toInt seems to work as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM