简体   繁体   中英

How to remove some character from NSString in iPhone?

NSString *Address = [placemark.addressDictionary objectForKey:@"FormattedAddressLines"];
NSLog(@"Address:%@", Address);
Address: ("Hooker Alley","San Francisco, CA 94108",USA)

I want to remove some character from Address string like Hooker Alley, San Francisco, CA 94108,USA. How to remove like this? please help me

Thanks in Advance

I tried this:

NSString *removeCharacter = [Address stringByReplacingOccurrencesOfString:@"(" withString:@""];

But the error comes in First throw call stack:

Error Message is,

-[__NSArrayM stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x81afd00 

Try this,

NSArray *addressArray = [placemark.addressDictionary objectForKey:@"FormattedAddressLines"];
NSString *address = [addressArray componentsJoinedByString:@", "];
NSLog(@"Address:%@", address);

In your case, [placemark.addressDictionary objectForKey:@"FormattedAddressLines"] is returning an array and not a single string. You can try to join these array components as a single string as shown above. Alternatively you can also check

id object = [placemark.addressDictionary objectForKey:@"FormattedAddressLines"];
if([object isKindOfClass[NSArray class]]) {
  //handle as above
} else if([object isKindOfClass[NSString class]]) {
  //use your code
}

you can remove like:-

 NSString *s = @"$$$hgh$g%k&fg$$tw/-tg";
    NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"-/:;()$&@\".,?!\'[]{}#%^*+=_|~<>€£¥•."];
    s = [[s componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""];

    NSLog(@"String is: %@", s);

Check what output does addressDictionary objectForKey provides using isKindOfClass like this:

if([[placemark.addressDictionary objectForKey:@"FormattedAddressLines"] isKindOfClass[NSArray class]])
{

  NSArray *address=[placemark.addressDictionary objectForKey:@"FormattedAddressLines"];
  NSLog(@"Address:%@",Address);
  NSString *str = [address componentsJoinedByString: @","]
  NSLog(@"str:%@",str);
}

Try this once....It will help you

 NSString *unfilteredString = @"!@#$%^&*()_+|abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
NSCharacterSet *notAllowedChars = [NSCharacterSet characterSetWithCharactersInString:@"1234567890"];
NSString *resultString = [[unfilteredString componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:@""];
NSLog (@"Result: %@", resultString);
    NSArray *Address=[placemark.addressDictionary objectForKey:@"FormattedAddressLines"];
     NSLog(@"Address:%@",Address);

     NSMutableString *myString = [[NSMutableString alloc]init];

    for(int i = 0 ; i < [Address count] ; i++){
    [myString appendString:[Address objectAtIndex:i]];


    if (i < [Address count]  ){
    [myString appendString:@","];
       }
 }

NSLog(@"%@",myString);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM