How to check if a string starts with any letter, like in az or AZ?

Here is the context

if (self.display.text hasPrefix:[NSString stringWithFormat:@"%@ = ", ANYLETTER]) 

Thanks Matt, I changed it a bit, so he still checked all the rest of the formatting, now it works as expected. :)

 if ([self.display.text rangeOfString:@" = "].location == 1 && [[NSCharacterSet letterCharacterSet] characterIsMember:[self.display.text characterAtIndex:0]]) 

EDITED: it just checks if the first character is a letter

 char character = [self.display.text characterAtIndex:0]; if ([[NSCharacterSet lowercaseLetterCharacterSet] characterIsMember:character] || [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:character]) { //String Starts With a Letter } 
+4
source share
3 answers
 if ([[NSCharacterSet letterCharacterSet] characterIsMember:[self.display.text characterAtIndex:0]]) { // Starts with letter. } 
+4
source

Here is one way:

 unichar ch = [[[self display] text] characterAtIndex:0]; BOOL startsWithLetter = (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') 

You can also use the regex library / method to check, but the above is pretty simple and fast.

+1
source

You can do - (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)aSet from NSString and pass in [NSCharacterSet letterCharacterSet] and check if the range location is not set.

0
source

All Articles