How do I convert a string into an integer?
I have a textbox I have the user enter a number into:
EditText et = (EditText) findViewById(R.id.entry1);
String hello = et.getText().toString();
And the value is assigned to the string hello
.
I want to convert it to a integer so I can get the number they typed; it will be used later on in code.
Is there a way to get the EditText
to a integer? That would skip the middle man. If not, string to integer will be just fine.
You should covert String to float. It is working.
Use regular expression is best way to doing this as already mentioned by ashish sahu
Use regular expression:
output: i=1234;
If you need first number combination then you should try below code:
output: i=123;
The much simpler method is to use the
decode
method ofInteger
so for example:Kotlin
There are available Extension methods to parse them into other primitive types.
"10".toInt()
"10".toLong()
"true".toBoolean()
"10.0".toFloat()
"10.0".toDouble()
"10".toByte()
"10".toShort()
Java