检查和编辑NSMutableString

时间:2013-04-26 01:47:55

标签: objective-c regex nsmutablestring nsregularexpression

我得到以下格式的结果:

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。

1 个答案:

答案 0 :(得分: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,Cupertino,CA

代表@"Starbucks, 222 Main Street, Cupertino, CA"

  

222 Main Street,Cupertino,CA

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