How to check whether a string contains whitespaces in between characters?
Use the test() method to check if a string contains whitespace, e.g. /\s/. test(str) . The test method will return true if the string contains at least one whitespace character and false otherwise.
The isspace() function in C++ checks if the given character is a whitespace character or not.
However, " " , "\t" and "\n" are all strings containing a single character which is characterized as whitespace. If you just mean a space, use a space.
In Python, string. whitespace will give the characters space, tab, linefeed, return, formfeed, and vertical tab.
use rangeOfCharactersFromSet:
NSString *foo = @"HALLO WELT"; NSRange whiteSpaceRange = [foo rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]]; if (whiteSpaceRange.location != NSNotFound) { NSLog(@"Found whitespace"); }
note: this will also find whitespace at the beginning or end of the string. If you don't want this trim the string first...
NSString *trimmedString = [foo stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; NSRange whiteSpaceRange = [trimmedString rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With