SoFunction
Updated on 2025-05-16

Four ways to determine whether a string is pure number

Preface

In Java, there are several common ways to determine whether a string is a pure number (that is, it only contains numeric characters). Here we will introduce several common implementation methods.

1. Use the () method

The () method can be used to determine whether a string complies with a specific regular expression. We can use the regular expression ^\d+$ to check whether a string contains only numbers.

public class Main {
    public static void main(String[] args) {
        String str = "12345";
        if (("\\d+")) {
            (str + "It's pure numbers");
        } else {
            (str + "Not pure numbers");
        }
    }
}

explain:

  • \\d+\\dDenotes a numeric character (equivalent to[0-9]),+Indicates matching one or more.
  • matches()Method returntrue, if the string fully complies with the regular expression.

2. Use the() method

If we want to check whether a string is a number character by character, we can use()method. This method checks whether the given character is a number.

public class Main {
    public static void main(String[] args) {
        String str = "12345";
        boolean isDigit = true;
        
        for (int i = 0; i < (); i++) {
            if (!((i))) {
                isDigit = false;
                break;
            }
        }
        
        if (isDigit) {
            (str + "It's pure numbers");
        } else {
            (str + "Not pure numbers");
        }
    }
}

explain:

  • (char c): Check the characterscWhether it is a numeric character.
  • By traversing each character in the string, we can determine whether it is a numeric character. If you encounter non-numeric characters, return directlyfalse

3. Use   () or  () (for integers)

If the number represented by the string does not exceedintorlongWe can also use the scope of()or()To try to convert the string to a number. If the conversion is successful, the string is a pure number.

public class Main {
    public static void main(String[] args) {
        String str = "12345";
        
        try {
            (str);  // Can also be used (str);            (str + "It's pure numbers");
        } catch (NumberFormatException e) {
            (str + "Not pure numbers");
        }
    }
}

explain:

  • (str) Try to convert the string to an integer, throwing a NumberFormatException exception if the string does not conform to the integer format.
  • This approach is suitable for cases where numbers are smaller, because () supports only 32-bit integers.

4. Use () (for floating point numbers)

If you need to check for floating point numbers instead of integers, you can use()method. It can process numeric strings containing decimal points.

public class Main {
    public static void main(String[] args) {
        String str = "123.45";
        
        try {
            (str);
            (str + "It's pure numbers");
        } catch (NumberFormatException e) {
            (str + "Not pure numbers");
        }
    }
}

explain:

  • (str)Try to parse the string into a floating point number. If the string cannot be parsed into a floating point number, it will be thrownNumberFormatExceptionException.

Summarize

  • (): Applicable to using regular expressions to determine whether it is a pure number.
  • (): Applicable to character-by-character judgment whether a string is a pure number.
  • ()or(): Applicable to judging integer types.
  • (): Suitable for judging floating point number types.

Which method to choose depends on the type of numbers you need to judge and the specific needs.

This is the end of this article about the four methods of Java to determine whether a string is pure number. For more related Java to determine whether a string is numerical, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!