[英]Determine how many characters there are after a certain character in UITextField
我正在使用UITextField
和數字輸入。 我需要確定在特定字符(。)之后有多少個字符。 NSString
方法完全可以做到這一點嗎? 我試過使用shouldReplaceCharacters
:方法,但沒有成功。
謝謝,Virindh Borra
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;
(...)
}
第一個將返回第一個點之后的字符數,第二個將返回第一個和第二個點(如果沒有第二個點,則為字符串的末尾)之間的字符數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.