1

我得到以下格式的结果:

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 不变。

4

1 回答 1

0

我会选择另一种方法:

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 Main Street, 库比蒂诺, CA

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

222 Main Street, 库比蒂诺, CA

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

于 2013-04-26T06:33:14.640 回答