Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if string contains characters other than alphabetical character

I want a function to return TRUE if a string contains only letters, and FALSE otherwise.

I had a hard time finding a solution for this problem using R even though there are many answer pages for other languages.

like image 775
ejg Avatar asked Oct 12 '15 04:10

ejg


People also ask

How do you check whether a string contains all the alphabets or not?

Now for a given string, the characters of the string are checked one by one using Regex. Regex can be used to check a string for alphabets. String. matches() method is used to check whether or not the string matches the given regex.

How do you test if a string has a certain character?

Use the String. includes() method to check if a string contains a character, e.g. if (str. includes(char)) {} . The include() method will return true if the string contains the provided character, otherwise false is returned.

How do you make sure a string only has letters?

In order to check if a String has only Unicode letters in Java, we use the isDigit() and charAt() methods with decision-making statements. The isLetter(int codePoint) method determines whether the specific character (Unicode codePoint) is a letter. It returns a boolean value, either true or false.

How can you check a string can only have alphabets and not digits?

To check whether a String contains only unicode letters or digits in Java, we use the isLetterOrDigit() method and charAt() method with decision-making statements. The isLetterOrDigit(char ch) method determines whether the specific character (Unicode ch) is either a letter or a digit.


1 Answers

We can use grep. We match letters [A-Za-z] from the start (^) to the end $ of the string.

grepl('^[A-Za-z]+$', str1)
#[1]  TRUE FALSE

data

str1 <- c('Azda', 'A123Zda')
like image 135
akrun Avatar answered Sep 30 '22 01:09

akrun