Replace the empty space% 20 in Objective-C

I need to check the UITextField to replace the empty space '' with "% 20" and wondered how is this possible?

+7
source share
2 answers

Check the stringByReplacingOccurrencesOfString: withString: on NSString method if you just want to replace the characters in the string with a different value.

Returns a new line in which all occurrences of the target line in the receiver are replaced by another specified line.

NSString *originalString = @"Sample text with spaces"; NSString *newString = [originalString stringByReplacingOccurancesOfString:@" " withString:@"%20"]; 

If you are trying to encode a URL, use stringByAddingPercentEscapesUsingEncoding: on NSString .

Returns the representation of the recipient using the specified encoding to determine the percentage screens required to convert the recipient to the legal URL string.

 NSString *originalString = @"Sample text with spaces"; NSString *newString = [originalString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
+38
source
 NSString* string = @"Daylight by Maroon 5" ; NSString* encodedString = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ; 
+19
source

All Articles