代码之家  ›  专栏  ›  技术社区  ›  Tirth

如何在iphone的objective-C中从NSMutableString中断或生成nsmutablesubstring?

  •  0
  • Tirth  · 技术社区  · 15 年前

    谢谢你给我回信。。。。

    200,8,"7 Infinite Loop, Cupertino, CA 95014, USA"
    

    我只想要库珀蒂诺和CA从上面的可变字符串。 在iphone中objective-C有可能吗 我想从上面的同类og可变字符串中获取城市和州的名称。 请给我一个解决方案,它为我创造了很多hedac。

    3 回复  |  直到 15 年前
        1
  •  0
  •   Jack    15 年前

    NSArray *myArray = [string componentsSeparatedByString:@","];
    NSString * strippedNumbers = [[myArray objectAtIndex:4] stringByReplacingOccurrencesOfRegex:@"[^0-9]" withString:@""];
    NSString *myString = [NSString stringWithFormat:@"%@ %@", [myArray objectAtIndex:3], strippedNumbers];
    
        2
  •  2
  •   Stefan Arentz    15 年前
        3
  •  1
  •   Costique    15 年前

    一种方法是在逗号处拆分字符串并修剪空白:

    NSArray *substrings = [myString componentsSeparatedByString: @","];
    NSCharacterSet *whitespace = [NSCharacterSet whitespaceCharacterSet];
    NSMutableArray *result = [NSMutableArray array];
    for ( NSString *str in substrings )
        [result addObject: [str stringByTrimmingCharactersInSet: whitespace]];