【问题标题】:How to detect if certain characters are at the end of an NSString?如何检测某些字符是否在 NSString 的末尾?
【发布时间】:2010-04-06 19:25:47
【问题描述】:

假设我可以有以下字符串:

"hey @john..."
"@john, hello"
"@john(hello)"

我正在对字符串进行标记以使每个单词都用空格分隔:

[myString componentsSeparatedByString:@" "];

我的令牌数组现在包含:

@john...
@john,
@john(hello)

我正在检查标点符号如下:

NSRange textRange = [words rangeOfString:@","];
if(textRange.location != NSNotFound){ } //do something

对于这些情况。如何确保只有 @john 被标记,同时保留尾随字符:

...
,
(hello)

注意:我希望能够处理字符串末尾的所有字符大小写。以上只是3个例子。

【问题讨论】:

    标签: objective-c cocoa cocoa-touch string nsstring


    【解决方案1】:

    查看 NSString 的-rangeOfString:options:range:... 给它一个{ [myString length] - [searchString length], [searchString length] } 的范围,看看结果范围的位置是否等于NSNotFound。请参阅文档中的NSStringCompareOptions 选项以了解区分大小写等。

    【讨论】:

      【解决方案2】:

      您可以使用NSScannerNSCharacterSet 来执行此操作。 NSScanner 可以扫描字符串直到字符集中第一次出现。如果你得到+alphaNumericCharacterSet,然后调用-invertedSet,你会得到一组非字母数字字符。

      这可能不是超级高效,但它会起作用:

      NSArray* strings = [NSArray arrayWithObjects:
                          @"hey @john...",
                          @"@john, hello",
                          @"@john(hello)",
                          nil];
      
      //get the characters we want to skip, which is everything except letters and numbers
      NSCharacterSet* illegalChars = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
      
      
      for(NSString* currentString in strings)
      {
          //this stores the tokens for the current string
          NSMutableArray* tokens = [NSMutableArray array];
      
          //split the string into unparsed tokens
          NSArray* split = [currentString componentsSeparatedByString:@" "];
      
          for(NSString* currentToken in split)
          {
              //we only want tokens that start with an @ symbol
              if([currentToken hasPrefix:@"@"])
              {
                  NSString* token = nil;
      
                  //start a scanner from the first character after the @ symbol
                  NSScanner* scanner = [NSScanner scannerWithString:[currentToken substringFromIndex:1]];
                  //keep scanning until we hit an illegal character
                  [scanner scanUpToCharactersFromSet:illegalChars intoString:&token];
      
                  //get the rest of the string
                  NSString* suffix = [currentToken substringFromIndex:[scanner scanLocation] + 1];
      
                  if(token)
                  {
                      //store the token in a dictionary
                      NSDictionary* tokenDict = [NSDictionary dictionaryWithObjectsAndKeys:
                                                 [@"@" stringByAppendingString:token], @"token", //prepend the @ symbol that we skipped
                                                 suffix, @"suffix",
                                                 nil];
                      [tokens addObject:tokenDict];
                  }
              }
          }
          //output
          for(NSDictionary* dict in tokens)
          {
              NSLog(@"Found token: %@ additional characters: %@",[dict objectForKey:@"token"],[dict objectForKey:@"suffix"]);
          }
      }
      

      【讨论】:

      • 不错的解决方案。虽然这可行,并且可以在我的字符串中检测到非字母数字,但我仍然需要能够为用户保留字母数字字符。
      • 我已经修改了示例以存储附加字符。
      【解决方案3】:

      您确定CFStringTokenizerits new Snow-Leopard-only Cocoa equivalent 不是更合适吗?

      如您所见,仅在空格上分割是一种非常幼稚的标记方式。 CFStringTokenizer 和enumerateSubstrings… 在真正的人类语言词汇规则方面要聪明得多。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-30
        • 1970-01-01
        • 1970-01-01
        • 2012-09-30
        相关资源
        最近更新 更多