CharSequence to int

passsy picture passsy · May 2, 2011 · Viewed 51.4k times · Source

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);

Answer

Joachim Sauer picture Joachim Sauer · May 2, 2011

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());