【问题标题】:Determine how many characters there are after a certain character in UITextField确定 UITextField 中某个字符后有多少个字符
【发布时间】:2013-09-02 18:11:54
【问题描述】:

我正在使用UITextField 和数字输入。我需要确定特定字符(。)之后有多少个字符。 NSString 方法是否可以做到这一点,如果可以的话如何?我曾尝试使用shouldReplaceCharacters: 方法,但没有取得多大成功。

谢谢, 维林德·博拉

【问题讨论】:

    标签: ios objective-c nsstring uitextfield


    【解决方案1】:
    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;
         (...)
    }
    

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

    【讨论】:

    • NSUInteger 是一种类型,因此我认为它不需要指针。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-21
    相关资源
    最近更新 更多