0

I have to get city name from a user and send a URL. How URLs will not accept blank characters, so I need to:

  Detecting a whitespace in on a NSString, and then insert a cacaractere this space.

1 Answers1

0

Try this

NSString * oldString = @"http://www.domain.com/my city";
NSString * finalString = [oldString stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
Deepak
  • 1,421
  • 1
  • 16
  • 20
  • This only deals with spaces. What of the value has other special characters like `&` and others? – rmaddy Nov 13 '14 at 04:20
  • @rmaddy: My answer is based on Tiago Amaral question. He just need to find and replace the whitespace from an NSString because whitespace does not allowed in URL. – Deepak Nov 13 '14 at 04:28
  • Thank you so much! I will test and tell if it worked!   But it's just to put something in the empty space. This URL is to send some data to my database. It need not be something with variations. –  Nov 13 '14 at 04:36
  • 1
    @TiagoAmaral Don't use this solution because it won't handle other possible special characters in the city name. See the duplicate question and its answers for a better, more general solution that will handle other possible values. User's can type in just about anything. – rmaddy Nov 13 '14 at 04:38
  • 1
    It worked perfectly !! Thanks again !!! @rmaddy I believe and know that there are broader solutions to this question, but did some testing here on my server, and this response has met that need perfectly. But thanks for your observation and for sure will make me more attentive to broader projects!   Thank you both! –  Nov 13 '14 at 04:48
  • Hopefully nobody will enter the name of my city in your app: Köln. – Amin Negm-Awad Nov 13 '14 at 05:19