简体   繁体   中英

how to check positive integer in a string and remove the space in between

i used the below method to validate whether a string contains all integer numbers, and return it using "parsePhone" method. But such program is unable to check whether there is a whitespace in between a string format such as "09213 2321" . And output a string with the space removed. Besides, is there a better way to combine these two methods into 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 will 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;
}

You'll want to use Pattern and Matcher from Java.

public static boolean phoneValidation(String phone) {
    if (phone == null) return false;
    String string = phone.trim();
    Pattern p = Pattern.compile('\\d');
    Matcher m = p.matcher(string);
    String result = "";

    while (m.find()) {
      result += m.group(0);
    }

    if (result.length() >= 3)
      return true;

    return false;
}

This should look through the string, finding every digit character and returning them one by one, to be added to result. You can then check your phone number length.

If you wanted the function to return a String then substitute the if at the end to read:

if (result.length() >= 3)
  return result;

return "";

In fact a much faster way would be to do:

public static String phoneValidation(String phone) {
    if (phone == null) return false;
    String result = phone.replaceAll("[^\\d]", "");

    if (result.length() >= 3)
      return result;

    return "";

}

Which would delete every character that wasn't a digit.

If you want to validate a string whether its a phone number, even though it contains a white space, then you can follow the following regular expression pattern to find out that.

Pattern is : [0-9]* [0-9]*

If you have any certain criteria like white space should be used to separate std code and number then you can provide the pattern that way.

Note : There is a space in the pattern.

try this

    String s = "09213 2321";
    boolean matches = s.matches("\\d+\\s?\\d+");
    if (matches) {
        s = s.replaceAll("\\s", "");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM