This guide will provide information about how to determine whether a string contains a numeric value or not.
How to Determine Whether a String Represents a Numeric Value?
In C# strings are used to store data and display it. A string contains text in it which can be in numeric form or may contain a numeric value at a particular index of the string. To check whether a string contains a numeric value there are three methods which are listed below:
Here, the first two methods are used to check the strings that are fully in numeric form and the third method will check a particular index of the string whether it is in numeric form or not.
Method 1: Using the “int.TryParse()” Method
It is the most commonly used method to check whether a string is in numeric form or not. It will check the whole string. The return type of this method is boolean which means it will return “true” if the full string is in integer form and “false” if the string contains even a single character.
Let’s check out the syntax for this method provided below:
According to the above-given syntax, this method takes two arguments. First, we need to verify the string name, and then we need to check the reference parameter. The reference argument should be in integer form.
Now, let’s understand this concept with the help of an example:
public class Example
{
public static void Main(string[] args)
{
String a = "103";
int b;
Console.WriteLine(int.TryParse(a,out b));
}
}
In this example:
- We have a class “Example” in which we have a “main()” method.
- In this class, we have a string variable named “a” that contains the number “103”.
- We use the integer “b” as a reference variable after that.
- Then, we use the “int.TryParse()” method and pass “a” and “b” as its arguments.
- This program will return “true” if “a” is fully numeric and “false” if “a” contains any character in it.
Output
Next, change the input string and check the resultant value:
public class Example
{
public static void Main(string[] args)
{
String a = "The number is 103";
int b;
Console.WriteLine(int.TryParse(a,out b));
}
}
It can be seen that the output of the “a” variable that contains text is false:
Method 2: Using the “double.TryParse()” Method
The working of this method is the same as the “int.TryParse()” method. The only difference is here we will check the values that contain decimal points.
The syntax for this method is provided below:
Here, the reference variable should be declared as double.
Let’s see an example code:
public class Example
{
public static void Main(string[] args)
{
String a = "103.77";
double b;
Console.WriteLine(double.TryParse(a,out b));
}
}
Here, we have used the same example with some differences:
- In this example, the value of the string contains a decimal point “103.77”.
- The reference variable “b” is also declared as double.
- If we use the “int.TryParse()” method instead of “double.TryParse()”, the output will be false. So we cannot use the “int.TryParse()” method to check the double value. But we can use the “double.TryParse()” method to check the value even without a decimal point.
The output for this example is:
Take another example, add some text in the input, and check the results:
public class Example
{
public static void Main(string[] args)
{
String a = "number is 103.77";
double b;
Console.WriteLine(double.TryParse(a,out b));
}
}
We can see that, the result is false which indicates the string is not numeric.
Note: We can use the “TryParse()” method with “long”, “float”, and “decimal” types in the same way.
Method 3: Using the “IsDigit()” Method
We can check for the numeric value at a particular point of a string by using the “IsDigit()” method. This method also has a boolean return type. The syntax is:
Here is an example that shows the implementation of this method:
public class Example
{
public static void Main(string[] args)
{
String a = "number is 103.77";
bool b = Char.IsDigit(a, 2);
bool c = Char.IsDigit(a, 12);
System.Console.WriteLine("Numeric value present at index 2: "+b);
System.Console.WriteLine("Numeric value present at index 12 is: "+c);
}
}
In this example:
- We have an input string “a” that contains text which is a combination of numeric and non-numeric data.
- We initialized two “bool” variables as “b” and “c”.
- In “b”, we have checked whether the index “2” contains a number or not by using the “IsDigit()” method.
- In “c” we have checked for the index “12”.
Output
Here:
- The output for index 2 is “false” as we can see the “m” present at index 2 which is not numeric.
- The output for index 12 is “true” as we can see the “0” present at index 12 which is a numeric value.
That’s all! We have learned about how we can check whether a string represents numeric data or not.
Conclusion
Strings are used to store text that can be in numeric or any other form. In C#, users may determine whether a string contains numbers by using the “int.TryParse()” method and the “double.TryParse()” method. Both methods return boolean output and check the string fully. Users can also check a specific index of a string by using the “IsDigit()” method. In this write-up, we have determined whether a string represents several values in C#.