CharSequence to int
Is there a Way to converte a Charsequence or a String to an Ingeter?
CharSequence cs = "123";
int number = (int开发者_运维百科) cs;
I'm a Noob. Solution:
CharSequence cs = "123";
int number = Integer.parseInt(cs);
Use Integer.parseInt()
. If your CharSequence
is not a String
, then you need to convert it first using toString()
.
int number = Integer.parseInt(cs.toString());
Since Java 9 you can use Integer.parseInt(CharSequence s, int from, int to, int radix)
to parse integers from any CharSequence
without first converting it to a String:
CharSequence cs = new StringBuilder("4711");
int value = Integer.parseInt(cs, 0, cs.length(), 10);
use this
int i=Integer.parseInt(cs.toString())
Integer.parseInt(cs.toString())
Use the parsers from the Wrapper classes (Integer, Float, etc)...
public static void main(String[] args) {
String s = "1";
int i = Integer.parseInt(s);
System.out.println(i);
}
Using Kotlin
CharSeqString.toString.toInt()
From Editview component,
TextView txtYear = (TextView) findViewById(R.id.txtYear);
int intYear = Integer.parseInt(txtYear.getText().toString());
精彩评论