user1796282
user1796282

Reputation:

How to format string as we need

I have three type of string and i want to format that string to get street number, street name, city name.

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

How can i do this or any link that show string formatting like this than please suggest.And yes the string i write here is just a format of string i get,string will be changed every time.

Upvotes: 0

Views: 128

Answers (7)

Anoop Vaidya
Anoop Vaidya

Reputation: 46563

The address string is passed to this method and then it is converted into an array having 3 string objects containing streetNumber, streetName and cityName. Then the array is returned to the caller.

-(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]);


}

Upvotes: 1

Pochi
Pochi

Reputation: 13459

1) Separate with:

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

2) The last component will always be your city name

3) Then check the (Last - 1) component with

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

2) Take the FIRST element of the array and use this

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

3) If it has only digits then, the FIRST element is your street number, and just make a new string by appending the remaining elements to get the street name. Else the past (last -1) from the previous array is the street name.

This is the basic idea, the logic can obviously be improved.

Edit: since you mentioned that this string is provided by google api it means you are probably getting a JSON response. You should use the complete JSON response to get your textfields filled. There is a JSON to NSDictionary Class you can use:see here

Upvotes: 1

Nameet
Nameet

Reputation: 650

Here is a piece of code that might help you.

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:@""];
    }
}

You can extend its logic for your requirement.

Upvotes: 0

lakshmen
lakshmen

Reputation: 29094

You need to use Regular Expressions. Have a look at this: Regular Expressions

Upvotes: 0

Exploring
Exploring

Reputation: 935

Try in the following way and parse the response whatever you need related to that address

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

HOpe it may help you.

Upvotes: 0

edzio27
edzio27

Reputation: 4140

Try this:

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

It will give you an array wsth all strings separated by " ";

Upvotes: 0

Sagrian
Sagrian

Reputation: 1048

May be first you can implement code to break the address string into different variables such as _streetNum,_streetName,_cityName then you can use following code line for formatting the string

NSString* formattedString = [[NSString alloc] initWithFormat:@"street number : %@\nstreet name :%@\ncity name :%@",_streetNum,_streetName,_cityName];

Upvotes: 0

Related Questions