0

I am trying to remove from an NSString link , some characters that are not letters, that may appear at the start and and the end. Is there a simple, but safe way to do that ?

example :

NSString *link= @" www.something.com. "  //removing the `.`
NSString *link= @" [www.something.com] "  //removing the `[ ]`
NSString *link= @" www.something.com/ "  //removing the `/`

Thanks a lot.

2 Answers2

1

Just use this:

    NSCharacterSet *notAllowed = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"] invertedSet];
    NSString *resultString = [[yourString componentsSeparatedByCharactersInSet:notAllowed] componentsJoinedByString:@""];
leonmaia
  • 11
  • 1
  • Thanks, but is this removes only the first and the last at the string? –  Apr 29 '14 at 12:17
  • this is not a good answer. you remove all the non letters from the whole string .... –  Apr 29 '14 at 12:20
1

To remove unwanted characters from the start and end of the given string, use stringByTrimmingCharactersInSet:

NSString *link= @" www.something.com. ";
NSCharacterSet *charset = [[NSCharacterSet letterCharacterSet] invertedSet];
NSString *stripped = [link stringByTrimmingCharactersInSet:charset];
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382