How to Test a String for Letters Only

Check if string contains only letters in javascript

With /^[a-zA-Z]/ you only check the first character:

  • ^: Assert position at the beginning of the string
  • [a-zA-Z]: Match a single character present in the list below:

    • a-z: A character in the range between "a" and "z"
    • A-Z: A character in the range between "A" and "Z"

If you want to check if all characters are letters, use this instead:

/^[a-zA-Z]+$/.test(str);
  • ^: Assert position at the beginning of the string
  • [a-zA-Z]: Match a single character present in the list below:

    • +: Between one and unlimited times, as many as possible, giving back as needed (greedy)
    • a-z: A character in the range between "a" and "z"
    • A-Z: A character in the range between "A" and "Z"
  • $: Assert position at the end of the string (or before the line break at the end of the string, if any)

Or, using the case-insensitive flag i, you could simplify it to

/^[a-z]+$/i.test(str);

Or, since you only want to test, and not match, you could check for the opposite, and negate it:

!/[^a-z]/i.test(str);

how to test a string for letters only

STL way:

struct TestFunctor
{
bool stringIsCorrect;
TestFunctor()
:stringIsCorrect(true)
{}

void operator() (char ch)
{
if(stringIsCorrect && !((ch <= 'z' && ch >= 'a') || (ch <= 'Z' && ch >= 'A')))
stringIsCorrect = false;
}
}

TestFunctor functor;

for_each(name.begin(), name.end(), functor);

if(functor.stringIsCorrect)
cout << "Yay";

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_]+$");

How to check that a string contains only “a-z”, “A-Z” and “0-9” characters

You could use regex for this, e.g. check string against following pattern:

import re
pattern = re.compile("[A-Za-z0-9]+")
pattern.fullmatch(string)

Explanation:

[A-Za-z0-9] matches a character in the range of A-Z, a-z and 0-9, so letters and numbers.

+ means to match 1 or more of the preceeding token.

The re.fullmatch() method allows to check if the whole string matches the regular expression pattern. Returns a corresponding match object if match found, else returns None if the string does not match the pattern.

All together:

import re

if __name__ == '__main__':
string = "YourString123"
pattern = re.compile("[A-Za-z0-9]+")

# if found match (entire string matches pattern)
if pattern.fullmatch(string) is not None:
print("Found match: " + string)
else:
# if not found match
print("No match")

Checking if string is only letters and spaces

The function can be written more simpler and correctly if to use standard C functions isalpha and isblank declared in header <ctype.h> For example

#include <ctype.h>

//...

int checkString( const char s[] )
{
unsigned char c;

while ( ( c = *s ) && ( isalpha( c ) || isblank( c ) ) ) ++s;

return *s == '\0';
}

If you want to check whether a string contains white spaces then instead of function isblank you should use function isspace.

Take into account that it is not a good idea to use statement continue in such simple loops. It is better to rewrite the loop without the continue statement.

And instead of function scanf it is better to use function fgets if you want to enter a sentence The function allows to enter several words as one string until the Enter will be pressed.

For example

fgets( str1, sizeof( str1 ), stdin );

Take into account that the function includes the new line character. So after entering a string you should remove this character. For example

size_t n = strlen( str1 );
if ( n != 0 && str1[n-1] == '\n' ) str1[n-1] = '\0';


Related Topics



Leave a reply



Submit