2013-04-26 80 views
1

我得到的結果如下格式:檢查和編輯的NSMutableString

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。

+0

什麼是 「字母」? – 2013-04-26 02:03:01

+0

英文字母A-Z。 – user1107173 2013-04-26 02:05:05

回答

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大街,加利福尼亞州Cupertino

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

222大街,加利福尼亞州Cupertino

編輯:此代碼不正是你想要的東西;)

+0

感謝您的詳細回覆。對不起,但我今天休息。我明天會試試這件事並回復你。 – user1107173 2013-04-26 19:31:21

+0

歡迎您,慢慢來;; – HAS 2013-04-26 19:33:43

+0

謝謝!問題:For(; startIndex user1107173 2013-04-27 20:29:44