Is there a function buried in the std lib somewher...
# getting-started
j
Is there a function buried in the std lib somewhere that makes this more succinct? Trying to parse something like
"1234 abcd"
to
1234
Copy code
val myInt = someString.takeWhile { it.isDigit() }.toInt()
🚫 1
r
What do you mean by more succinct? If you want fewer characters:
Copy code
val myInt = someString.split(' ')[0].toInt()
j
I remember from something (maybe a different language like python?) which was a bit more lenient with integer parsing and grabbed the first bit of a string that was a number. Not sure if there's a function that does this in Kotlin or Java
h
Javascript does this (shudder).
j
Ah that's probably what I was remembering haha
r
I believe that is the way GDScript works for the Godot game engine as well. It has been the source of more than one bug for me.
j
Glad I checked here then. Hopefully the integer parsing behavior is more likely to stick in my brain after this interaction. Probably not though. lol
k
Another possibility (just a possibility, not a recommendation):
Copy code
val myInt = java.util.Scanner(someString).nextInt()
Scanner is too heavy a class for this purpose; I would just keep your original code.