我很难从以下字符串中提取纬度和经度子字符串+ 25.1870 + 055.2784 /
由于这是一个位置字符串,+符号可能是 - 。我正在寻找一种能够识别+或 - 符号的方法,并在第二个+ / - 符号的开头创建第一个字符串,在第二个字符串中创建第二个字符串。
我在下面发布了我的方法。它完美无缺,但我觉得它有点复杂。
NSString *itemValue = [NSString stringWithFormat:@"%@",item.value];
NSLog(@"ITEM VALUE = %@", itemValue);
NSString *firstSign = [itemValue substringToIndex:1];
NSLog(@"FIRST SIGN = %@", firstSign);
NSString *stringExcludingFirstSign = [itemValue substringWithRange:NSMakeRange(1, itemValue.length - 1)];
NSLog(@"STRING EXCLUDING FIRST SIGN = %@", stringExcludingFirstSign);
NSString *latitudeWithoutSign;
NSScanner *scanner = [NSScanner scannerWithString:stringExcludingFirstSign];
if([stringExcludingFirstSign containsString:@"+"]){
[scanner scanUpToString:@"+" intoString:&latitudeWithoutSign];
}
else if([stringExcludingFirstSign containsString:@"-"]){
[scanner scanUpToString:@"-" intoString:&latitudeWithoutSign];
}
NSLog(@"LATITUDE WITHOUT SIGN = %@", latitudeWithoutSign);
NSString *latitude = [NSString stringWithFormat:@"%@%@", firstSign, latitudeWithoutSign];
NSLog(@"LATITUDE = %@", latitude);
NSString *longitude = [itemValue substringWithRange:NSMakeRange(scanner.scanLocation + 1, (itemValue.length - (scanner.scanLocation + 1)) - 1)];
NSLog(@"LONGITUDE = %@", longitude);
答案 0 :(得分:1)
你的字符串的问题在于你没有任何类型的分隔符可以指示纬度/经度的位置。所以这就是我如何处理这个问题。
这是我已经测试过的工作代码
NSString *myCordStr = @"+25.1870+055.2784/";
myCordStr=[myCordStr stringByReplacingOccurrencesOfString:@"/" withString:@""];
myCordStr=[myCordStr stringByReplacingOccurrencesOfString:@"-" withString:@" -"];
myCordStr=[myCordStr stringByReplacingOccurrencesOfString:@"+" withString:@" +"];
NSString *newCordStr = [myCordStr substringFromIndex:1];
NSLog(@"myCordStr:%@ ...", myCordStr);
NSLog(@"newCordStr:%@ ...", newCordStr);
NSArray *coordArray = [newCordStr componentsSeparatedByString:@" "];
NSLog(@"coordArray: %@ ...", coordArray);
OUTPUT:
myCordStr: +25.1870 +055.2784 ...
newCordStr:+25.1870 +055.2784 ...
coordArray: (
"+25.1870",
"+055.2784"
) ...
答案 1 :(得分:0)
虽然它有点复杂,但下面的代码可以解决问题。
NSString * itemValue = [NSString stringWithFormat:@"%@",item.value]; NSLog(@" ITEM VALUE =%@",itemValue);
NSString *firstSign = [itemValue substringToIndex:1];
NSLog(@"FIRST SIGN = %@", firstSign);
NSString *stringExcludingFirstSign = [itemValue substringWithRange:NSMakeRange(1, itemValue.length - 1)];
NSLog(@"STRING EXCLUDING FIRST SIGN = %@", stringExcludingFirstSign);
NSString *latitudeWithoutSign;
NSScanner *scanner = [NSScanner scannerWithString:stringExcludingFirstSign];
if([stringExcludingFirstSign containsString:@"+"]){
[scanner scanUpToString:@"+" intoString:&latitudeWithoutSign];
}
else if([stringExcludingFirstSign containsString:@"-"]){
[scanner scanUpToString:@"-" intoString:&latitudeWithoutSign];
}
NSLog(@"LATITUDE WITHOUT SIGN = %@", latitudeWithoutSign);
NSString *latitude = [NSString stringWithFormat:@"%@%@", firstSign, latitudeWithoutSign];
NSLog(@"LATITUDE = %@", latitude);
NSString *longitude = [itemValue substringWithRange:NSMakeRange(scanner.scanLocation + 1, (itemValue.length - (scanner.scanLocation + 1)) - 1)];
NSLog(@"LONGITUDE = %@", longitude);
答案 2 :(得分:0)
您可以使用NSRegularExpression
,然后使用matchesInString:options:range
获取两个坐标的数组。
正则表达式为(+|-)\d+\.\d+
完整代码:
NSString *string = @"+25.1870+055.2784"
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(+|-)\d+\.\d+" options:nil error:nil]
NSArray *result = [regex matchesInString:string options:nil range:NSMakeRange(0, string.length)]