0

我有三种类型的字符串,我想格式化该字符串以获取街道号码、街道名称、城市名称。

The first type is : 34 Ellis Street, San Francisco
Here i want to make it like 
street number : 34
street name : Ellis Street
city name : San Francisco


The second type is : 4FL, 800 Market Street, San Francisco
Here i want to delete 4FL, 
And i want to make it like 
street number : 800
street name : Market Street
city name : San Francisco

The third type is : Ellis & Market, San Francisco
Here i want to make it like 
street number : 
street name : Ellis & Market
city name : San Francisco

我该如何执行此操作或任何显示此类字符串格式的链接,而不是建议。是的,我在这里写的字符串只是我得到的字符串格式,每次都会更改字符串。

4

7 回答 7

1

1) 分开:

NSArray *arrayOfComponents = [yourString componentsSeparatedByString:@","];

2)最后一个组件将始终是您的城市名称

3) 然后检查 (Last - 1) 组件

NSArray *array = [yourString componentsSeparatedByString:@" "];

2)取数组的第一个元素并使用它

NSCharacterSet* notDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
if ([newString rangeOfCharacterFromSet:notDigits].location == NSNotFound)
{
    // newString consists only of the digits 0 through 9
}

3)如果它只有数字,那么第一个元素是你的街道号码,只需通过附加剩余元素来创建一个新字符串以获得街道名称。否则,前一个数组中的过去(最后一个 -1)是街道名称。

这是基本思路,逻辑显然可以改进。

编辑:既然你提到这个字符串是由 google api 提供的,这意味着你可能会得到一个 JSON 响应。您应该使用完整的 JSON 响应来填充您的文本字段。您可以使用 JSON 到 NSDictionary 类:请参阅此处

于 2013-01-25T08:27:14.263 回答
1

地址字符串传递给此方法,然后将其转换为具有 3 个字符串对象的数组,其中包含 streetNumber、streetName 和 cityName。然后将数组返回给调用者。

-(NSArray *)brakeAddress:(NSString *)address{

     NSMutableArray *arr=[NSMutableArray arrayWithArray:[address componentsSeparatedByString:@","]];

     if (arr.count>2) {
     [arr removeObjectAtIndex:0];
     }

     NSInteger streetNameInd=[arr count]-2, cityNameInd=[arr count]-1;

     NSMutableArray *streetNameArray=[NSMutableArray arrayWithObjects:arr[0], nil];

     if ([arr[streetNameInd] intValue]) {

     streetNameArray=[NSMutableArray arrayWithArray:([arr[streetNameInd] componentsSeparatedByString:@" "])];

     [streetNameArray removeObjectAtIndex:0];
     if ([streetNameArray[0] intValue] ==[arr[streetNameInd] intValue]) {
     [streetNameArray removeObjectAtIndex:0];
     }
     }

     NSString *streetName=[streetNameArray componentsJoinedByString:@" "];

     NSString *streetNumber=@"";
     if ([arr[streetNameInd] intValue]!=0) {
     streetNumber=[NSString stringWithFormat:@"%d", [arr[streetNameInd] intValue]];
     }

    NSString *city=arr[cityNameInd];
    // NSLog(@"\nstreet number :%@\nstreet name  :%@\ncity name :%@",streetNumber, streetName,city);

    NSArray *addressParts=[NSArray arrayWithObjects:streetNumber, streetName, city, nil];

    return addressParts;
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{

    NSString *str1=@"34 Ellis Street, San Francisco";
    NSString *str2=@"4FL, 800 Market Street, San Francisco";
    NSString *str3=@"Ellis & Market, San Francisco";

    NSArray *firstAddress=[self brakeAddress:str1];
    NSArray *secondAddress=[self brakeAddress:str2];
    NSArray *thirdAddress=[self brakeAddress:str3];

    NSLog(@"\n1st : street number :%@\nstreet name  :%@\ncity name :%@",firstAddress[0],firstAddress[1],firstAddress[2]);
    NSLog(@"\n2nd : street number :%@\nstreet name  :%@\ncity name :%@",secondAddress[0],secondAddress[1],secondAddress[2]);
    NSLog(@"\n3rd : street number :%@\nstreet name  :%@\ncity name :%@",thirdAddress[0],thirdAddress[1],thirdAddress[2]);


}
于 2013-01-25T08:35:02.003 回答
0

可能首先您可以实现代码将地址字符串分解为不同的变量,例如_streetNum,_streetName,_cityName然后您可以使用以下代码行来格式化字符串

NSString* formattedString = [[NSString alloc] initWithFormat:@"street number : %@\nstreet name :%@\ncity name :%@",_streetNum,_streetName,_cityName];
于 2013-01-25T07:49:40.480 回答
0

试试这个:

NSArray *array = [yourString componentsSeparatedByString:@" "];

它将为您提供一个数组,其中包含由“”分隔的所有字符串;

于 2013-01-25T07:58:54.463 回答
0

尝试以下方式并解析与该地址相关的任何您需要的响应

http://maps.googleapis.com/maps/api/geocode/json?address=34%20Ellis%20Street,%20San%20Francisco,+CA&sensor=true

希望它可以帮助你。

于 2013-01-25T08:03:46.727 回答
0

您需要使用正则表达式。看看这个:正则表达式

于 2013-01-25T08:09:18.693 回答
0

这是一段可能对您有所帮助的代码。

NSString *addressString;
NSArray *tempArray = [addressString componentsSeparatedByString:@", "];
if([tempArray count]==3){
    city = [tempArray objectAtIndex:2];
    NSString *tempString = [tempArray objectAtIndex:1];

    NSArray *temp1Array = [tempString componentsSeparatedByString:@" "];
    if ([temp1Array count]>1) {
        st_num = [tempArray objectAtIndex:0];
        st_name = [tempString stringByReplacingOccurrencesOfString:
                             [NSString stringWithFormat:@"%@ ",st_num] withString:@""];
    }
}

您可以根据您的要求扩展其逻辑。

于 2013-01-25T09:20:51.140 回答