Use the int.TryParse method.
string x = "42";
if(int.TryParse(x, out int value))
// Do something
If it successfully parses it will return true, and the out result will have its value as an integer.
More Related Contents:
- Parse string to DateTime in C#
- Most efficient way to concatenate strings?
- How to convert UTF-8 byte[] to string
- Replace Line Breaks in a String C#
- How can I String.Format a TimeSpan object with a custom format in .NET?
- Convert String to Type in C# [duplicate]
- How do I split a string by a multi-character delimiter in C#?
- When is it better to use String.Format vs string concatenation?
- Way to have String.Replace only hit “whole words”
- Best way to resolve file path too long exception
- String Concatenation using ‘+’ operator
- Escape curly brace ‘{‘ in String.Format [duplicate]
- How to parse a string into a nullable int
- Why is there no Char.Empty like String.Empty?
- How do I split a string by strings and include the delimiters using .NET?
- How do I truncate a .NET string?
- How can I convert BitArray to single int?
- String interning in .Net Framework – What are the benefits and when to use interning
- Need to perform Wildcard (*,?, etc) search on a string using Regex
- Is String a primitive type?
- string.IsNullOrEmpty(string) vs. string.IsNullOrWhiteSpace(string)
- If strings are immutable in .NET, then why does Substring take O(n) time?
- string.ToLower() and string.ToLowerInvariant()
- C# Version Of SQL LIKE
- Best way to handle Integer overflow in C#?
- What is the correct way to compare char ignoring case?
- Compare string similarity
- How to convert string to integer in C#
- Regex nested parentheses
- string.Empty vs null.Which one do you use?