java - how to check positive integer in a string and remove the space in between -
i used below method validate whether string contains integer numbers, , return using "parsephone" method. such program unable check whether there whitespace in between string format such "09213 2321". , output string space removed. besides, there better way combine these 2 methods one!?
public static string parsephone(string phone) { if(phone==null) return null; string s = phone.trim(); if(s.matches("^[0-9]+$")) { while(s.startswith("0")&& s.length()>1) { s = s.substring(1); } if(s.length()>=3) return s; } return null; } public static boolean phonevalidation(string phone) { if(phone == null) return false; string string = phone.trim(); if(string.matches("^[0-9]+$")) { while(string.startswith("0")&& string.length()>1) { string = string.substring(1); } if(string.length()>=3) return true; } return false; }
try this:
public static string parsephone(string phone) { if(phone==null) return null; string s = phone.trim().replaceall("\\s",""); //the replaceall remove whitespaces if(s.matches("^[0-9]+$")) { while(s.startswith("0")&& s.length()>1) { s = s.substring(1); } if(s.length()>=3) return s; } return null; } public static boolean phonevalidation(string phone) { return parsephone(phone) != null; }
Comments
Post a Comment