string modifications are not working? - ios

This is my code and in my url string white spaces are not encoded by the code
NSString *str = item.fileArtPath;
NSCharacterSet *set = [NSCharacterSet URLQueryAllowedCharacterSet];
[str stringByAddingPercentEncodingWithAllowedCharacters:set];
[str stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSURL *urlString = [NSURL URLWithString:str];
for below string:
http://xx.xx.xx.xxx:xx/xx/xx/xx/xxx/Audio Adrenaline.jpg
^^^^^^^^^^^^^^^^^^^^
The white space after Audio is not converted to %20 after I used string replacement. And in Debugging urlString is nil why so?

From the NSString Class Reference
Returns a new string made from the receiver by replacing all
characters not in the specified set with percent encoded characters.
Meaning it doesn't permute the instance it's called on. You need to say something like str = [str stringByAddingPercentEncodingWithAllowedCharacters:set]; Same with [str stringByReplacingOccurrencesOfString:#" " withString:#"%20"];

Recall that NSString is immutable. Calling methods stringBy... returns the modified string, rather than modifying the original one. Therefore your code should be rewritten as follows:
str = [str stringByAddingPercentEncodingWithAllowedCharacters:set];
str = [str stringByReplacingOccurrencesOfString:#" " withString:#"%20"];

NSString *search = [searchBar.text stringByReplacingOccurrencesOfString:#" " withString:#"%20"];

Related

Remove formatting from a string: “(123) 456-7890” => “1234567890” in objective c

I have a string of phone number with format (123)-(456)-7890 but how can i convert to the following form 1234567890?
Try this
NSString *numberString = [[mixedString componentsSeparatedByCharactersInSet:
[[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
componentsJoinedByString:#""];
It will give digits from a string.
stringByReplacingOccurrencesOfString supports also regular expression
NSString *phoneNumber = #"(123)-(456)-7890";
NSString *filteredPhoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:#"[ ()-]"
withString:#""
options:NSRegularExpressionSearch
range:NSMakeRange(0, phoneNumber.length)];
NSLog(#"%#", filteredPhoneNumber);
Put all characters to be ignored between the brackets in the first parameter.
An alternative regex is #"[^\\d+]" which means ignore all non-digit characters
NSString *str = #"(123)-(456)-7890";
str = [str stringByReplacingOccurrencesOfString: #"(" withString:#""];
str = [str stringByReplacingOccurrencesOfString: #")" withString:#""];
str = [str stringByReplacingOccurrencesOfString: #"-" withString:#""];
NSLog(#"Output = %#",str);
Output = 1234567890
You also replacing more character in Single Line of coding using stringByReplacingOccurrencesOfString
NSString *YourString = #"(123)-(456)-7890";
YourString = [[[YourString stringByReplacingOccurrencesOfString:#"(" withString:#""] stringByReplacingOccurrencesOfString:#")" withString:#""] stringByReplacingOccurrencesOfString:#"-" withString:#""];
NSLog(#"YourString == > %#",YourString);
YourString == > 1234567890
Use this code,
NSString *valString = #"(123)456-7890";
NSString* phoneStr=[[[valString stringByReplacingOccurrencesOfString:#"(" withString:#""] stringByReplacingOccurrencesOfString:#")" withString:#""] stringByReplacingOccurrencesOfString:#"-" withString:#""];
NSLog(#"phone String %#",phoneStr); // finally you get phone String 1234567890
its working for me, hope its helpful
If you want your code to work outside the USA, use Google's telephony library. Formatting and extracting phone numbers is difficult. I wouldn't even try doing it myself by hand.

How do I remove new line characters from a string?

I need to strip away any new line characters from a string. I tried the stringByReplacingOccurencesOfString method below. What is the correct method to do this in Objective C?
[textLine stringByReplacingOccurrencesOfString:#"\n" withString:#""];
NSString *textWithNewLinesRemoved = [textLine stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
stringByReplacingOccurrencesOfString does not modify textLine
NSString *strippedTextLine = [textLine stringByReplacingOccurrencesOfString:#"\n" withString:#""];
Or
NSString *textLine = #"My cool text\n";
textLine = [textLine stringByReplacingOccurrencesOfString:#"\n" withString:#""];

Replace occurrences of NSString with `'` to `\'`

I have a NSString in which I m trying to replace special characters. Part of my string looks like this:
I have replace ' to \'
If you want to replace ' with \' use
NSString *str = #"ladies'";
str = [str stringByReplacingOccurrencesOfString:#"'" withString:#"\\'"];
\ is an escape character so you'll have to use it twice.
Note: \ is escape character in objective c.
NSString *s = #"This is Testing Mode and we are testing just Details of this Ladies' Market place";
NSString *r = [s stringByReplacingOccurrencesOfString:#"'" withString:#"\\'"];
NSString *str = #"'Hello'";
str = [str stringByReplacingOccurrencesOfString:#"'" withString:#"\\"];
NSString *originalString = "a rand'om st'ring"
NSString *resultString = [originalString stringByReplacingOccurrencesOfString:#"'" withString:#"\'"];
// "a random string" (output of "po resultString" in LLDB)
using the escape character "\", we can remove unwanted characters. The character next to escape character("\") will be ignored/escaped.

objective-c regex to remove NSString characters if they start with spaces and commas

My app uses addresses. I am not sure how to create a regex that strips away an address that reads like these.
" , Paxton, TX"
" , Dallas TX"
Need the above to be stripped so they read like this.
"Paxton, TX"
"Dallas TX"
Use the NSString API stringByTrimmingCharactersInSet:
NSString *test = #" , Paxton, TX";
NSMutableCharacterSet *charset = [[NSCharacterSet whitespaceCharacterSet] mutableCopy];
[charset addCharactersInString:#","];
NSString *final = [test stringByTrimmingCharactersInSet:charset];
NSLog(#"final = [%#]",final);
You can also do this without using regular expressions
NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:#" ,"];
NSString *trimmed = [#" , Payton, TX" stringByTrimmingCharactersInSet:charSet];
NSLog(#"%#", trimmed); //prints "Payton, TX"

Replacing characters in an NSString after converting an NSDictionary to an NSString

I've searched around and couldn't find an answer, so forgive me if I've created a duplicate question.
I've converted an NSDictionary into an NSString and would like to take out all the occurrences of "{", "}", and ";". When I use stringByReplacingOccurrencesOfString nothing appears different in the NSLog.
The code I've written:
NSString *myString = [myDictionary description];
[myString stringByReplacingOccurrencesOfString: #"{" withString:#""];
[myString stringByReplacingOccurrencesOfString: #"}" withString:#""];
[myString stringByReplacingOccurrencesOfString: #";" withString:#""];
NSLog(#"%#", myString);
The reason that nothing appears different is that the stringByReplacingOccurencesOfString returns a new string that you need to assign to myString, it doesn't mutate the string in place, so you could use
NSString *myString = [myDictionary description];
myString=[myString stringByReplacingOccurrencesOfString: #"{" withString:#""];
myString=[myString stringByReplacingOccurrencesOfString: #"}" withString:#""];
myString=[myString stringByReplacingOccurrencesOfString: #";" withString:#""];
NSLog(#"%#", myString);
But, generally the use of description for anything other that debugging/logging should be avoided. You should iterate the dictionary directly - For example.
NSMutableString *myString=[NSMutableString new];
for (NSString *key in myDictionary.allKeys) {
[myString appendString:#"%# = %#\r\n",key,myDictionary[key]];
}
NSLog(#"%#",myString");
You need to use stringByTrimmingCharactersInSet and stringByReplacingOccurrencesOfString api both. Refer the below sample code:-
NSString *myString = [myDictionary description];
NSString *str=[myString stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"{}"]];
NSLog(#"%#", [str stringByReplacingOccurrencesOfString:#";" withString:#""]);

Resources