C
C#3mo ago
js

how do i check if a string contains only words and space

ive got an if statement that needs to check if a string contains only letters or spaces, online i can only find how to check for only letters which then flags the spaces as not letters
4 Replies
js
js3mo ago
like for example if the string is "Fast Food Emporium" it would flag as invalid as it has spaces even though that is valid i just want to make sure it only contains characters and spaces
public static void ContainsOnlyLetters(this string input)
{
int count5 = 0;
foreach (char c in input)
{
if (!Char.IsLetterOrDigit(c))
count5++;
else if (!Char.IsWhiteSpace(c))
count5++;
}

if (count5 != input.Length)
{
Console.WriteLine("Invalid restaurant name in file, please select a valid file.");
LoadFoodMenu();
}
else
{
return;
}

}
public static void ContainsOnlyLetters(this string input)
{
int count5 = 0;
foreach (char c in input)
{
if (!Char.IsLetterOrDigit(c))
count5++;
else if (!Char.IsWhiteSpace(c))
count5++;
}

if (count5 != input.Length)
{
Console.WriteLine("Invalid restaurant name in file, please select a valid file.");
LoadFoodMenu();
}
else
{
return;
}

}
i managed this is that ok ? doesnt work ugh
daniel2
daniel23mo ago
Char.IsLetter Method (System)
Indicates whether a Unicode character is categorized as a Unicode letter.
js
js3mo ago
i got it working but the api is so confusing to me as a beginner is that normal
Angius
Angius3mo ago
Depends what exactly is so confusing But yes, it's generally normal to be confused about the things you're new at