检查和编辑的NSMutableString
我得到的结果如下格式:检查和编辑的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。
我会选择另一种方法:
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
编辑:此代码不正是你想要的东西;)
感谢您的详细回复。对不起,但我今天休息。我明天会试试这件事并回复你。 – user1107173 2013-04-26 19:31:21
欢迎您,慢慢来;; – HAS 2013-04-26 19:33:43
谢谢!问题:For(; startIndex
什么是 “字母”? – 2013-04-26 02:03:01
英文字母A-Z。 – user1107173 2013-04-26 02:05:05