Reputation: 1057
I'm looking for an fast method-solutions of my Problem to check a String, if this contains an min one char. IF String contains like any one of character in alphabet then return true, else false.
public bool checkString(String s)
{
return true || false;
}
For Example:
"1232133432454355467" return false
"134324239846c" return true
Upvotes: 1
Views: 331
Reputation: 26209
static void Main(string[] args)
{
Console.WriteLine(checkString("137563475634c756"));
}
static public bool checkString(String s)
{
return Regex.IsMatch(s, "[a-zA-Z]");
}
It Returns True.
Upvotes: 1
Reputation: 32541
Try:
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
var r = CheckString("112");
Console.WriteLine(r); // false
r = CheckString("112a");
Console.WriteLine(r); // true
}
public static bool CheckString(String input)
{
return Regex.Match(input, @"[a-zA-Z]").Success;
// or, as @Vlad L suggested
//return Regex.IsMatch(input, @"[a-zA-Z]");
}
}
If you want to verify against the "All Letters" Unicode character class, use this one instead:
return Regex.IsMatch(input, @"\p{L}");
Reference: Supported Unicode General Categories
Upvotes: 4
Reputation: 19407
Just for sake of completion.
// Regex to check the value consists of letters
// with atleast 1 character
private static Regex reg = new Regex(@"[a-zA-Z]+");
public bool checkString(String s)
{
return reg.Match(s).Success;
}
Upvotes: 1
Reputation: 33809
Try this with ToCharArray():
public bool checkString(String s)
{
bool retValue = s.ToCharArray()
.Any(c => ((int)c > 64 && (int)c < 91) ||
((int)c > 96 && (int)c < 123));
return retValue
}
Upvotes: 1
Reputation: 5576
If I understood the question correctly... This returns true if the string contains at least one letter.
public bool checkString(String s)
{
return s.Any(x => Char.IsLetter(x));
}
Upvotes: 2