Verifying that a string contains only letters in C#
Only letters:
Regex.IsMatch(input, @"^[a-zA-Z]+$");
Only letters and numbers:
Regex.IsMatch(input, @"^[a-zA-Z0-9]+$");
Only letters, numbers and underscore:
Regex.IsMatch(input, @"^[a-zA-Z0-9_]+$");
Verifying that a string contains only letters in console application in c#
Use char.IsLetter
to check if all the characters in a string are Letter.
if (name.All(char.IsLetter))
{
//All characters are letters
}
It is same as:
if (name.All(r => char.IsLetter(r)))
Check if a string contains only letters, digits and underscores
I love Linq for this kind of question:
bool validA = sname.All(c => Char.IsLetterOrDigit(c) || c.Equals('_'));
Verifying that a string contains only uppercase letters and numbers in C#
All()
takes a predicate which can be a lambda expression. You can use this to check IsLetter && IsUpper
separately from IsDigit
.
input.All(c => (Char.IsLetter(c) && Char.IsUpper(c)) || Char.IsDigit(c))
Try it online
Alternatively, you could use a regular expression that matches zero or more uppercase characters or digits:
using System.Text.RegularExpressions;
Regex r = new Regex(@"^[A-Z0-9]*$");
r.IsMatch(input);
Try it online
Regex explanation:
^[A-Z0-9]*$
^ Start of string
[A-Z0-9] The uppercase letter characters or the digit characters
* Zero or more of the previous
$ End of string
Check if string contains only one type of letter C#
Try this:
private bool ContainsOnlyOneLetter(string String)
{
if(String.Length == 0)
{
return true;
}
for(int i = 0; i < String.Length;i++)
{
if(String.Substring(i,1) != String.Substring(0,1))
{
return false;
}
}
return true;
}
And you can use the function like this:
bool containsOneLetter = ContainsOnlyOneLetter("...");
if (containsOneLetter == true)
{
//Put code here when the letters are the same...
}
else
{
//Code when there are different letters..
}
How to check if a String contains any letter from a to z?
Replace your for loop
by this :
errorCounter = Regex.Matches(yourstring,@"[a-zA-Z]").Count;
Remember to use Regex
class, you have to using System.Text.RegularExpressions;
in your import
Check string contains only alphabets in C# and other allowed charaters are: ., whitespace, ,
Try this
var regex = new Regex(@"(?i)^[a-z.,\s]+$");
bool res = regex.IsMatch(subject);
How to validate that a string contains only certain characters
You need else block.
public void setFirstName(string newFirstName)
{
bool valid;
valid = System.Text.RegularExpressions.Regex.IsMatch(newFirstName, "^[- a-zA-Z]*$");
if (valid)
{
firstName = newFirstName;
}
else
{
firstName = " ";
}
}
Update
Your regex seems also wrong it needs * for zero or more occurance instead of ?.
Correct regex => ^[- a-zA-Z]*$
Click here to see working fiddle => link
Check a string if it only contains letters from the alphabet and not numbers or other stuff in C#?
This should do the job :
bool result = input.All(Char.IsLetter);
and also a regex solution :
Regex.IsMatch(theString, @"^[\p{L}]+$");
The Char.IsLetter is the better solution since it counts as a letter any language alphabets. This regex will also count them but still it looks more compact with IsLetter
Verifying that a string contains only letters in C++
No numbers:
if(std::none_of(str.begin(), str.end(), [](unsigned char c){return std::isdigit(c);})) {
// stuff
}
All letters:
if(std::all_of(str.begin(), str.end(), [](unsigned char c){return std::isalpha(c);})) {
// stuff
}
Related Topics
How to Catch a Specific SQLexception Error
Question Mark and Colon in Statement. What Does It Mean
Linq to SQL and a Running Total on Ordered Results
What's the Best Way of Accessing Field in the Enclosing Class from the Nested Class
What Interfaces Do All Arrays Implement in C#
The Maximum Number of Characters a Textbox Can Display
How to Backup and Restore the System Clipboard in C#
How to Have an Enum Bound Combobox with Custom String Formatting for Enum Values
How to Execute an Ssis Package from .Net
Difference Between MVC 5 Project and Web API Project
Does the Use of the "Async" Suffix in a Method Name Depend on Whether the 'Async' Modifier Is Used
Is There Any Simple Way to Convert .Xls File to .CSV File? (Excel)
Using the Wpf Dispatcher in Unit Tests
Connect to Network Drive with User Name and Password
How to Run Unit Tests (Mstest) in Parallel