SoFunction
Updated on 2025-03-06

C# Detailed tutorial on determining whether the content in a string is pure number

1. Use ASCII code to judge

You can use ASCII code to determine whether the content in the string is pure numbers.

The steps are as follows:

  • First judge whether the string is empty to ensure the stability of the code operation;
  • Get the character array according to the ASCII encoding rules. The characters are Byte type and the Byte value of the character is corresponding to the ASCII table;
  • Iterate through the character array and determine whether each character in the character array is a number. Among them, the ASCII values ​​corresponding to 0~9 in the ASCII table are 48~57.

Here is what the code looks like:

 public bool isPureNum(string str)
        {
            if (==0 || str == null)//Verify whether this string is empty            {
                return false;
            }
            byte[] strBytes = (str);//Get the character array of byte type of string, encoding method ASCII            foreach(byte strByte in strBytes)
            {
                if ((strByte<48) || (strByte>57))     //Judge whether each character is a number and judge based on the range of the ASCII value of each character                {
                    return false;                     //No, return false                }
            }
            return true;                              //Yes, return true        }

2. Use the TryParse() method

You can use the TryParse() method to identify whether a string is a number, the steps are as follows:

It returns true by converting the specified string into an equivalent signed integer representation. If the conversion is successful, true; otherwise, false.

Here is what the code looks like:

 public bool isPureNum(string str)
        {
            bool isNumeric=(str,out _);
            return isNumeric;
        }

3. Use the() method (regular expression)

You can use regular expressions to check if a string is a number.

This can be done using the() method, which determines whether the specified string matches the provided regular expression.

This is demonstrated below: (Note that ^ matches the beginning of the string and $ matches its end)

 public bool isPureNum(string str)
        {
            if (str == null)//Verify whether this string is empty. Test: If the string is empty, there will be no exception. If the string is null, there will be an exception. If the string is null, there will be an exception.            {
                return false;
            }
            bool isNumeric=(str,@"^\d+$");
            return isNumeric;
        }

4. Use the () method

You can use the Char, IsDigit() method and the () method to check if the string is a number.

The () method returns a Boolean value that indicates whether the specified character is a number.

The following code demonstrates the () method and the method of () to determine whether a string is a number:

 public bool isPureNum(string str)
        {
            if (str == null)//Verify whether this string is empty. Test: If the string is empty, there will be no exception. If the string is null, there will be an exception. If the string is null, there will be an exception.            {
                return false;
            }
            bool isNumeric = ();//Check whether all characters in the string are numeric            return isNumeric;
        }

Summarize

This is the article about judging whether the content in a C# string is purely numeric. For more related C# to determine whether a string is purely numeric, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!