Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace blank spaces with %20 in Objective-C [closed]

I need to validate a UITextField to replace a blank space ' ' with '%20' and was wondering how this is possible?

like image 205
iphoneapplelover123 Avatar asked Jan 17 '13 20:01

iphoneapplelover123


2 Answers

Check out the method stringByReplacingOccurrencesOfString:withString: on NSString if you are just looking to replace the characters in a string with another value.

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

NSString *originalString = @"Sample text with spaces";

NSString *newString = [originalString stringByReplacingOccurancesOfString:@" " withString:@"%20"];

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

Returns a representation of the receiver using a given encoding to determine the percent escapes necessary to convert the receiver into a legal URL string.

NSString *originalString = @"Sample text with spaces";

NSString *newString = [originalString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
like image 117
Dan Avatar answered Sep 25 '22 12:09

Dan


NSString* string = @"Daylight by Maroon 5" ;
NSString* encodedString = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ;
like image 42
John Sauer Avatar answered Sep 23 '22 12:09

John Sauer