我有三种类型的字符串,我想格式化该字符串以获取街道号码,街道名称,城市名称。
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
我怎么能这样做或任何显示这样的字符串格式的链接,而不是建议。是的,我在这里写的字符串只是我得到的字符串格式,字符串每次都会被更改。
答案 0 :(得分:1)
1)分开:
NSArray *arrayOfComponents = [yourString componentsSeparatedByString:@","];
2)最后一个组件将始终是您的城市名称
3)然后用
检查(Last-1)组件NSArray *array = [yourString componentsSeparatedByString:@" "];
2)获取数组的FIRST元素并使用此
NSCharacterSet* notDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
if ([newString rangeOfCharacterFromSet:notDigits].location == NSNotFound)
{
// newString consists only of the digits 0 through 9
}
3)如果它只有数字,则FIRST元素是您的街道号码,只需通过附加其余元素来创建一个新字符串以获取街道名称。另外,前一个数组的过去(最后-1)是街道名称。
这是基本思想,逻辑显然可以改善。
编辑:因为您提到此字符串由google api提供,这意味着您可能正在获得JSON响应。您应该使用完整的JSON响应来填充文本字段。您可以使用JDS到NSDictionary类:请参阅here
答案 1 :(得分: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]);
}
答案 2 :(得分:0)
可能首先你可以实现代码将地址字符串分解为不同的变量,例如_streetNum,_streetName,_cityName
,然后你可以使用下面的代码行来格式化字符串
NSString* formattedString = [[NSString alloc] initWithFormat:@"street number : %@\nstreet name :%@\ncity name :%@",_streetNum,_streetName,_cityName];
答案 3 :(得分:0)
试试这个:
NSArray *array = [yourString componentsSeparatedByString:@" "];
它会给你一个数组,所有字符串用“”;
分隔答案 4 :(得分:0)
尝试以下方式并根据需要解析与该地址相关的响应
http://maps.googleapis.com/maps/api/geocode/json?address=34%20Ellis%20Street,%20San%20Francisco,+CA&sensor=true
它可以帮助你。
答案 5 :(得分:0)
您需要使用正则表达式。看看这个:Regular Expressions
答案 6 :(得分: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:@""];
}
}
您可以根据您的要求扩展其逻辑。