How to check if my string contains text?

if i have this line:

12345 = true

123a45 = false

abcde = false

how to do it in c #?

+4
source share
6 answers
 Regex.IsMatch(sinput,@"\d+"); 

to match a string containing only numbers. If you forget the optional number in the question, use this:

 Regex.IsMatch("+12345", @"[+-]?\d+"); 
+6
source

If you want to avoid RegEx, you can use the built-in char methods:

 bool allDigits = s.All(c => char.IsDigit(c)); 
+3
source

int.TryParse or long.TryParse.

You can also use Regex for any length:

 if (Regex.IsMatch(str, "^[0-9]+$")) // ... 
0
source
 int myNumber; if( int.TryParse(myString, out myNumber) == true ) { // is a number and myNumber contains it } else { // isn't a number } 

if it is a BIG number, use long or double or .... instead of int.

0
source

This is the code for checking only letters in a string in C #. You can change it according to your needs.

 using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string MyString = "A @string & and 2."; Console.WriteLine(MyString); for (int charpos = 0; charpos < MyString.Length; charpos++) { Console.WriteLine(Char.IsLetter(MyString, charpos)); } //Keep the console on screen Console.WriteLine("Press any key to quit."); Console.ReadKey(); } } } 
0
source
 private bool ContainsText(string input) { for (int i = 0; i < input.Length; i++) { if (((int) input[i] >= 65 && (int) input[i] <= 90) || ((int) input[i] >= 97 && (int) input[i] <= 177)) return true; } return false; } 

Duration:

 MessageBox.Show(ContainsText("abc").ToString()); MessageBox.Show(ContainsText("123").ToString()); MessageBox.Show(ContainsText("123b23").ToString()); 

returns True, False, True respectively

0
source

Source: https://habr.com/ru/post/1341512/


All Articles