How do I check if a character in my NSString
is a non-spacing character?
For example:
NSString *string3 = @"เบียร์เหล้า";
// 12345678 <- only take up 8 spaces
The NSString
length is 11, but actually the space that it occupies is 8.
In Java, they can check the character type using code below:
static boolean isMark(char ch)
{
int type = Character.getType(ch);
return type == Character.NON_SPACING_MARK ||
type == Character.ENCLOSING_MARK ||
type == Character.COMBINING_SPACING_MARK;
}
What about Objective-C?
Thanks to bdash.
NSCharacterSet *characterSet = [NSCharacterSet nonBaseCharacterSet];
for (int i = 0; string3.length > i; i++)
{
NSString *string = [string3 substringWithRange:NSMakeRange(i, 1)];
NSRange letterRange = [string rangeOfCharacterFromSet:characterSet];
if (letterRange.location != NSNotFound)
{
NSLog(@"non base character set");
}
else
{
NSLog(@"normal");
}
}
You will find out which character that is not occupying space. Another way, you can find out the total non spacing character with code below.
NSString *newString = [[string3 componentsSeparatedByCharactersInSet:
[[NSCharacterSet nonBaseCharacterSet] invertedSet]] componentsJoinedByString:@""];
So for this example, the newString length is 3. That's what I'm looking for. Thanks people.
You can use [NSCharacterSet nonBaseCharacterSet]
to retrieve the set of characters in the Unicode mark category. NSCharacterSet
provides the ability to test whether a given character is a member of the set, and NSString
provides the ability to find particular characters within a string that are (or aren't) members of a given NSCharacterSet
.