I am googling NSSting manipulation methods from last few hours. and found many on stack overflow like this here
I have a String “1800 Ellis St, San Francisco, CA 94102, USA”. String may have any number of “,” . I have to take the third last(San Franncisco) and last(USA) substring after “,”.
and output should be “San Franncisco USA“.
I have logic how to do this in my mind that but I am struggling to implement it.
I try this code for getting the position of last three “,” in string. but it’s not work for me
NSInteger commaArr[3];
int index=0;
int commaCount=0;
for(unsigned int i = [strGetCityName length]; i > 0; i--)
{
if([strGetCityName characterAtIndex:i] == ',')
{
commaArr[index]=i;
index++;
++commaCount;
if(commaCount == 3)
{
break;
}
}
}
Thanks
You can do it like this:
The
componentsSeparatedByString:will split the string at,, andstringWithFormat:will put the parts back together.