-3

I've got:

NSString *urlString = @"+11 111 111 111";
NSString *trimmedUrlString = [urlString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", trimmedUrlString]];

However when I check them, trimmedUrlString is same as urlString and url is nil. Does anybody have an idea why it trimmedUrlString isn't trimmed..?

Nat
  • 12,032
  • 9
  • 56
  • 103
  • I don't get it. Why downvoted? Each SOF question is because someone did something wrong or didn't know something. Now this is downvoted, because I was wrong, what happens in each question. – Nat Apr 03 '15 at 11:40
  • It is down voted because you should search before open a thread. This is a common question which answer you can find it everywhere.How To Ask: http://stackoverflow.com/help/how-to-ask – Luca D'Alberti Apr 03 '15 at 11:59
  • @LucaD I've read the description of the method in xCode, and browsed SOF. Didn't find the solution so I've asked. Vote to close as duplicate in case I've missed something, that's how SOF works. Such statement that "you should search" can be applied to almost all questions. I've written ok code, but misunderstood the intention, so it wasn't so easy to find similar problem. – Nat Apr 03 '15 at 12:26
  • 1
    You know, the down vote is not mine, I only explained you why someone may down voted your question. `A new string made by removing from both ends of the receiver characters contained in set. If the receiver is composed entirely of characters from set, the empty string is returned.` from Apple Doc – Luca D'Alberti Apr 03 '15 at 12:31
  • @LucaD And my mistake was that I've misunderstood it, that it will remove from both ends of each word not everything. I know I could do better, but still won't remove the answer as it may be useful for other people, who like me can't read ;). – Nat Apr 03 '15 at 12:36
  • 1
    You are downvoted for not reading the documentation. – Hot Licks Apr 03 '15 at 12:46
  • Hey, easy buddy, you should always ask for an upvote to dasblinkenlight who's gained points by stating the obvious :D – HepaKKes Apr 04 '15 at 01:21

2 Answers2

6

You misunderstood the meaning of "trimming": it means "removing leading and trailing characters matching NSCharacterSet". Your string, on the other hand, does not have leading (before the first 1) or trailing (after the last 1) whitespace, so it remains unchanged.

Look at this answer for a way to remove all matching characters from the string.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

Here is the simplest way to remove all whitespaces inside the string:

NSString *trimmedUrlString = [urlString stringByReplacingOccurrencesOfString:@" " withString:@""];
Ponf
  • 1,190
  • 1
  • 12
  • 28