gpt4 book ai didi

iphone - 如何根据需要格式化字符串

转载 作者:行者123 更新时间:2023-11-28 22:41:02 25 4
gpt4 key购买 nike

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

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

我怎么能这样做或任何显示字符串格式的链接比请建议。是的,我在这里写的字符串只是我得到的字符串格式,每次都会更改字符串。

最佳答案

地址字符串被传递给这个方法,然后它被转换成一个包含 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]);


}

关于iphone - 如何根据需要格式化字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14517458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com