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
Post a Comment