【问题标题】:How can I count the number of non-alphanumeric characters in an NSString object?如何计算 NSString 对象中非字母数字字符的数量?
【发布时间】:2010-11-16 04:14:25
【问题描述】:

我正在深入研究 iOS 开发,但我仍然熟悉 NSString 对象。我现在需要计算字符串中非字母数字字符的数量。我想出的一种方法是从字符串中剥离非字母数字字符,然后从原始字符串的长度中减去剥离字符串的长度,就像这样......

NSCharacterSet *nonalphanumericchars = [[NSCharacterSet alphanumericCharacterSet ] invertedSet];

NSString *trimmedString = [originalString stringByTrimmingCharactersInSet:nonalphanumericchars];

NSInteger numberOfNonAlphanumericChars = [originalString length] - [trimmedString length];

但它不起作用。计数始终为零。如何计算 NSString 对象中非字母数字字符的数量?

非常感谢您的智慧!

【问题讨论】:

    标签: iphone ipad ios nsstring


    【解决方案1】:

    您的方法的问题在于您不是在剥离字符,而是在修剪它们。这意味着只从与集合匹配的字符串末端剥离字符(中间没有)。

    为此,您可以遍历字符串并测试每个字符是否不是字母数字集的成员。例如:

    NSString* theString = // assume this exists
    NSMutableCharacterSet* testCharSet = [[NSMutableCharacterSet alloc] init];
    [testCharSet formUnionWithCharacterSet:[NSCharacterSet alphanumericCharacterSet]];
    [testCharSet formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]];
    NSUInteger length = [theString length];
    NSUInteger totalNonAlnumCharacters = length;
    for( NSUInteger i = 0; i < length; i++ ) {
      if( [testCharSet characterIsMember:[theString characterAtIndex:i]] )
        totalNonAlnumCharacters--;
    }
    NSLog(@"Number of non-alphanumeric characters in string: %lu", (long int)totalNonAlnumCharacters);
    [testCharSet release];
    

    【讨论】:

    • 谢谢,杰森,我会试试看,但看起来不错。另外,感谢您指出我犯的修剪错误。
    • 还有一个问题,关于空间的计算呢?我宁愿空格不计为非字母数字字符,即使我认为是。我是 NSCharacterSet 类的新手,我可以以某种方式向 alnumCharSet 对象添加空格字符吗?
    • 我刚刚找到了“whitespaceCharacterSet”,我认为应该可以。我试试看,再次感谢。
    • @BeachRunnerJoe:当然,您可以创建一个可变字符集,然后组合您想要的字符集(甚至添加/删除特定字符)。我将更新代码示例以包含空格集和字母数字集。
    【解决方案2】:

    既然您提到您使用 stringByTrimmingCharactersInSet: 进行了尝试,那么了解实际上有一种仅通过框架调用来执行此操作的方法可能会很有趣。借用 Jason 的设置代码

    NSString* theString = // assume this exists
    NSMutableCharacterSet* testCharSet = [[NSMutableCharacterSet alloc] init];
    [testCharSet formUnionWithCharacterSet:[NSCharacterSet alphanumericCharacterSet]];
    [testCharSet formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]];
    

    然后您可以通过以下方式放弃 for 循环:

    NSArray *nonComp = [theString componentsSeparatedByCharactersInSet:testCharSet];
    

    它会在从字符集中找到一个字符的每个索引处将字符串分开,创建一个剩余部分的数组。然后,您可以使用键值编码对所有片段的长度属性求和:

    NSNumber *numberOfNonANChars = [nonComp valueForKeyPath:@"@sum.length"];
    

    或者,将这些部分再次粘合在一起并计算长度:

    NSUInteger nonANChars = [[nonComp componentsJoinedByString:@""] length];
    

    但是:拆分组件将创建计数后不需要的数组和字符串 - 毫无意义的内存分配(componentJoinedByString 也是如此:)。并且使用 valueForKeyPath: 对于这个非常简单的求和似乎也比仅遍历原始字符串要昂贵得多。

    代码可能更面向对象,甚至更不容易出错,但在这两种情况下,性能都会比 Jason 的代码差几个数量级。所以在这种情况下,良好的旧 for 循环将避免滥用(因为所使用的方法不适用于此)框架功能。

    【讨论】:

      【解决方案3】:
      NSString *str=@"str56we90";
      int count;
      
      for(int i=0;i<[str length];i++)
      {
          int str1=(int)[str characterAtIndex:i];
          NSString *temp=[NSString stringWithFormat:@"%C",str1];
      
          if(str1 >96 && str1 <123  || str1 >64 && str1 <91)
      
              count=count +1;
      }
      
      int finalResult = [str length] - count;
      

      count 将是您的最终结果。

      【讨论】:

      • 嗨雪利酒,我还没有机会尝试。另外,我不是对您的回复投反对票的人。不管是谁应该留下评论说明原因,但我感谢你的时间。
      • 非ASCII字符呢? temp 是干什么用的?
      猜你喜欢
      • 1970-01-01
      • 2018-08-20
      • 2021-09-28
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2014-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多