【问题标题】:Checking and editing NSMutableString检查和编辑 NSMutableString
【发布时间】:2013-04-26 01:47:55
【问题描述】:

我得到以下格式的结果:

NSString *placeResult = @"111 Main Street, Cupertino, CA"

或者有时结果包含地名:

NSString *placeResult = @"Starbucks, 222 Main Street, Cupertino, CA"

我需要检查第一个逗号之前的文本是数字还是字母。如果字符是字母,那么我需要从 NSMutableString 中删除第一个逗号和它之前的所有字母,并将唯一的字母存储在变量中。因此,第二个示例中的文本将如下所示:

@"222 Main Street, Cupertino, CA"

如何使用 NSRegularExpression、NSTextCheckingResult 和 NSMutableString 完成此任务?

我在想:

 NSString *str= (NSString *)location.address;
 NSMutableString *muteStr;
 muteStr = [NSMutableString stringWithString:str];

    NSArray *matches = [detector matchesInString:muteStr options:0 range:NSMakeRange(0, muteStr.length)];

    for (NSTextCheckingResult *match in matches)
    {
        if (match.resultType == NSTextCheckingTypeAddress)
        {
            NSDictionary *data = [match addressComponents];
            NSString *name = data[NSTextCheckingNameKey];
            if (!name && match.range.location > 0)
            {
                NSRegularExpression *scan = [NSRegularExpression regularExpressionWithPattern:@"(?= )" options:0 error:NULL];  
//******I'm not sure if I have regularExpressionWithPattern correct?

                NSTextCheckingResult *result = [scan firstMatchInString:@"," options:0 range:NSMakeRange(0, name.length)];

不确定从这里开始做什么,或者即使这是正确的方法?

再次,我需要检查第一个逗号之前的文本是数字还是字母。如果文本/字符是字母,那么我需要从 NSMutableString 中删除第一个逗号和它之前的所有字母,并将唯一的字母存储在变量中。如果字符是数字,我需要保持 NSMutableString 不变。

【问题讨论】:

  • 什么是“字母”?

标签: objective-c regex nsmutablestring nsregularexpression


【解决方案1】:

我会选择另一种方法:

NSString *placeResult = @"Starbucks, 222 Main Street, Cupertino, CA";
// Split the NSString into an NSArray out of parts of the NSString
NSArray *parts = [placeResult componentsSeparatedByString:@","];
// This NSMutableString will store our edited string
NSMutableString *result = [[NSMutableString alloc] init];
// If there are only 3 NSStrings in parts it is only `Address`, `City` and `State`
// so we can use it as is
if (parts.count == 3)
    [result appendString:placeResult];
// If there are 4 NSStrings in parts there is something in front of we don't need,
// so we need to cut it off
else if (parts.count == 4) {
    // We start at `index 1` because at `index 0` is the element we don't want
    int startIndex = 1;
    // Here we append the first part and after that increment our index
    [result appendFormat:@"%@", parts[startIndex++]];
    // We loop through the NSArray starting at `index 2`, our next element
    for (; startIndex < parts.count; startIndex++)
        // We append our new element with a comma in front of it
        // Note that the string we append still starts with a space so we don't insert one here
        [result appendFormat:@",%@",parts[startIndex]];
    // Now our string is completely stored in `result`. 
    // What we need to do now is cut off the first space which was included 
    // when we inserted the first element before the loop.
    // I mean this space: @"Starbucks, 222 Main Street, Cupertino, CA";
    //                                ↑
    // Our NSString usually does always has a space in front, so this if-clause is a little superfluous but in case you get a string without a space after every comma this cuts off your first letter 
    if ([[result substringWithRange:NSMakeRange(0, 1)] isEqualToString:@" "])
        // Delete the first character which definitely is a space
        [result deleteCharactersInRange:NSMakeRange(0, 1)];
}
// I'm pretty sure what we do here ;)
NSLog(@"%@", result);

输出:

对于@"111 Main Street, Cupertino, CA"

加利福尼亚州库比蒂诺市主街 111 号

对于@"Starbucks, 222 Main Street, Cupertino, CA"

加利福尼亚州库比蒂诺大街 222 号

编辑:此代码完全符合您的要求;)

【讨论】:

  • 感谢您的详细回复。抱歉,我今天要请假。我明天试试这个,然后回复你。
  • 谢谢!问题: For (; startIndex
  • 好吧,有分号是因为for-loopnormal 格式是for (int i = 0; i &lt; 5; i++) 或抽象for (initialization; condition; increment-decrement)。但是我们已经在完全合法的循环上方进行了初始化,所以我们不再在那里初始化它;) .... startIndex 是一个计数器。它从1 开始并且应该递增,否则您再次添加相同的子字符串并重新开始。 startIndex++ 所做的是获取startIndex 的值并评估当前语句,然后将自身增加1。如果你不明白告诉我;)
猜你喜欢
  • 2015-07-13
  • 1970-01-01
  • 2012-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-29
  • 1970-01-01
相关资源
最近更新 更多