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 check if a string only contains letters, numbers, underscores and period. Flutter/Dart
The problem with your RegExp
is that you allow it to match substrings, and you match only a single character. You can force it to require that the entire string be matched with ^
and $
, and you can match against one or more of the expression with +
:
print(RegExp(r'^[a-z]+$').hasMatch(mainString));
To match all the characters you mentioned:
print(RegExp(r'^[A-Za-z0-9_.]+$').hasMatch(mainString));
Java String - See if a string contains only numbers and not letters
If you'll be processing the number as text, then change:
if (text.contains("[a-zA-Z]+") == false && text.length() > 2){
to:
if (text.matches("[0-9]+") && text.length() > 2) {
Instead of checking that the string doesn't contain alphabetic characters, check to be sure it contains only numerics.
If you actually want to use the numeric value, use Integer.parseInt()
or Double.parseDouble()
as others have explained below.
As a side note, it's generally considered bad practice to compare boolean values to true
or false
. Just use if (condition)
or if (!condition)
.
JavaScript: check if a giving string contains only letters or digits
Add 0-9 also to your regex
boolean onlyLetters(String str) {
return str.match("^[A-Za-z0-9]+$");
}
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
Time Complexity of Accessing a Python Dict
How to Show Explosion Image When Collision Happens
Convert Words Between Verb/Noun/Adjective Forms
How to Convert 24 Hour Time to 12 Hour Time
How to Add Conda Environment to Jupyter Lab
Python:2D Contour Plot from 3 Lists:X, Y and Rho
Sql-Like Window Functions in Pandas: Row Numbering in Python Pandas Dataframe
Pygame How to Let Balls Collide
Pandas - Explanation on Apply Function Being Slow
How to Redirect Print Statements to Tkinter Text Widget
Delete File from Zipfile with the Zipfile Module
Getting Values from Object Oriented Tkinter
How to Form Tuple Column from Two Columns in Pandas
How to Compare Two JSON Objects with the Same Elements in a Different Order Equal