Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace whitespaces by a plus "+" sign

hey all i want a code the replace whitespaces by a + sign in objective-c

like image 548
Bobj-C Avatar asked Aug 14 '10 13:08

Bobj-C


People also ask

How do you replace characters in space?

Use the replaceAll() method to replace a character with a space. The method takes the character to be replaced and the replacement string as parameters, e.g. str. replaceAll('_', ' ') . The method returns a new string with all occurrences of the character replaced by the provided replacement.

How do you replace all spaces in a string?

Use the String. replace() method to replace all spaces in a string, e.g. str. replace(/ /g, '+'); . The replace() method will return a new string with all spaces replaced by the provided replacement.


2 Answers

In case you are asking this because you need to encode URLs, use this

NSString* escapedUrlString =
  [unescapedString stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];

If you just need space to +, use

[str stringByReplacingOccurrencesOfString:@" " withString:@"+"];
like image 176
Lou Franco Avatar answered Nov 15 '22 06:11

Lou Franco


return [thatString stringByReplacingOccurrencesOfString:@" " withString:@"+"];

If your real target is to escape URL component, use the -stringByAddingPercentEscapesUsingEncoding: method instead.

like image 21
kennytm Avatar answered Nov 15 '22 06:11

kennytm