regex - convert string to int in java -


i have string has int value in it. want extract int value string , print.

string str="no. of days : 365"; string dayswithsplchar = str.replaceall("[a-z][a-z]","").trim(); char[] ch = dayswithsplchar.tochararray(); stringbuffer stb = new stringbuffer(); for(char c : ch) {   if(c >= '0' && c <= '9')    {       stb.append(c);    } }  int days = integer.parseint(stb.tostring()); 

is there better way this. please let me know.

try string.replaceall

    string str = "no. of days : 365";     str = str.replaceall(".*?(\\d+).*", "$1");     system.out.println(str); 

you get

365 

Comments

Popular posts from this blog

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

Android Gson.fromJson error -