1

I am working with a UITextField and numerical input. I need to determine how many characters there are AFTER a specific character(.). Is this possible with NSString methods at all, if so How? I have tried using the shouldReplaceCharacters: method without much success.

Thanks, Virindh Borra

4

1 回答 1

3
NSUInteger *position = [givenString rangeOfString:@"."].location;
NSUInteger result = givenString.length - position - 1;

或替代方案:

NSArray *splittedString = [givenString componentsSeparatedByString:@"."];
NSUInteger count = [splittedString count];
if (count > 1) {
     NSString *stringAfterDot = (NSString*)[splittedString objectAtIndex:1];
     NSUInteger result = stringAfterDot.length;
     (...)
}

第一个将返回第一个点之后的字符数,第二个将返回第一个和第二个点之间的字符数(或字符串的结尾,如果没有第二个点)。

于 2013-09-02T18:15:42.560 回答