2010-11-16 71 views
1

我正在潜心研究iOS开发,并且我仍然熟悉NSString对象。我现在需要计算字符串中非字母数字字符的数量。我想出了从字符串剥离非字母数字字符,然后从原始字符串的长度减去剥离字符串的长度,像这样的一种方法......如何计算NSString对象中非字母数字字符的数量?

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

NSString *trimmedString = [originalString stringByTrimmingCharactersInSet:nonalphanumericchars]; 

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

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

非常感谢您的智慧!

回答

5

你的方法存在的问题是,你并没有剥离人物,你正在修剪它们。这意味着只能删除字符串匹配的字符(中间没有任何字符)。

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

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]; 
+0

谢谢,杰森,我会试试看,但它看起来不错。此外,谢谢你指出我所做的修剪错误。 – BeachRunnerFred 2010-11-16 05:08:07

+0

还有一个问题,占空间呢?我宁愿空间不算作非字母数字字符,即使我想是。我是NSCharacterSet类的新手,能否以某种方式向alnumCharSet对象添加空格字符? – BeachRunnerFred 2010-11-16 05:18:34

+0

我刚刚找到“whitespaceCharacterSet”,我认为应该可以工作。我会尝试一下,再次感谢。 – BeachRunnerFred 2010-11-16 05:20:00

-2
NSString *[email protected]"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; 

数将是你的最终结果。

+0

它对你有帮助吗? – Swastik 2010-11-16 04:51:17

+0

嗨雪莉,我还没有机会尝试它呢。另外,我不是那个低估了你的回应的人。不管是谁,都应该留下评论,说明原因,但我感谢你的时间。 – BeachRunnerFred 2010-11-16 05:07:27

+0

非ASCII字符呢?什么是'临时'? – 2010-11-16 12:09:14

2

既然你提到你stringByTrimmingCharactersInSet试了一下:,这可能是有趣地了解到,实际上是一种方式只与框架调用做到这一点。通过一些借用贾森

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

设置代码,那么你可以抛弃for循环是这样的:

NSArray *nonComp = [theString componentsSeparatedByCharactersInSet:testCharSet]; 

这将打破串除了在每一个地方发现从字符集一个字符的索引,创建剩余部分的数组。然后,您可以使用键 - 值编码到再总结所有的作品length属性:

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

或代替,再次粘合在一起的碎片,计数长度:

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

但是:分裂这些组件将创建一个数组和字符串,这些数组和字符串在计数之后不需要 - 无意义的内存分配(componentJoinedByString同样如此:)。并且使用valueForKeyPath:对于这个非常简单的求和,似乎代价更高,然后迭代原始字符串。

该代码可能更面向对象或更不容易出错,但在这两种情况下,性能都会比Jason的代码差几个数量级。因此,在这种情况下,优秀的旧for-loop将最大限度地滥用框架功能(因为所使用的方法不适用于此目的)。

相关问题