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

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:#""]);

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:#""];

string modifications are not working?

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"];

stringByReplacingOccurrencesOfString: doesn't work

I want my output to be "23", but it still shows "123".
NSString *valorTextField = [[NSString alloc] initWithFormat:#"123"];
[valorTextField stringByReplacingOccurrencesOfString:#"1" withString:#""];
NSLog(#"%#", valorTextField);
stringByReplacingOccurrencesOfString returns a new string:
NSString *valorTextField = [[NSString alloc] initWithFormat:#"123"];
NSString *replaced = [valorTextField stringByReplacingOccurrencesOfString:#"1" withString:#""];
NSLog(#"%#", replaced);
Alternatively, you can work with a NSMutableString and use the
replaceOccurrencesOfString:withString:options:range: method.
You are not assigning returned string to valorTextField again. You are just executing function. Actual string is unchanged. Instead write:
valorTextField = [valorTextField stringByReplacingOccurrencesOfString:#"1" withString:#""];
NSLog(#"%#", valorTextField);

Incompatible pointer type error when using NSMutableString and stringByReplacingOccurrencesOfString

The purpose of the code shown below is to take a string from a textfield and remove commas and left parentheses, to prepare the contents for conversion to a float. For example, it takes the number 1,234,567 and changes it to 1234567.
The code snippet works, but returns an informational error "Incompatible pointer types assigning to NSMutableString * from String *"
When I use NSString instead of NSMutableString, I get no errors, but the returned value is an empty string.
Using the NSMutableString methodology, what is the problem and how can I change this to eliminate the 'Incompatible pointer type" error.
NSMutableString *cleanedDataCellTest = [[NSMutableString alloc] initWithString:datacellR2C2.text];
cleanedDataCellTest = [cleanedDataCellTest stringByReplacingOccurrencesOfString:#"," withString:#""];
cleanedDataCellTest = [cleanedDataCellTest stringByReplacingOccurrencesOfString:#")" withString:#""];
cleanedDataCellTest = [cleanedDataCellTest stringByReplacingOccurrencesOfString:#"(" withString:#"-"];
cleanedDataCellTest = [cleanedDataCellTest stringByReplacingOccurrencesOfString:#" " withString:#""];
The stringByReplacingOccurrencesOfString is a method of NSString, and the return value is also a NSString. So you can't assign the return value to a NSMutableString variable.
Instead you can use the method replaceOccurrencesOfString:withString:options:range: from NSMutableString:
NSMutableString *cleanedDataCellTest = [[NSMutableString alloc] initWithString:#"1,234,567"];
[cleanedDataCellTest replaceOccurrencesOfString:#"," withString:#"" options:0 range:NSMakeRange(0, cleanedDataCellTest.length)];
[cleanedDataCellTest replaceOccurrencesOfString:#")" withString:#"" options:0 range:NSMakeRange(0, cleanedDataCellTest.length)];
[cleanedDataCellTest replaceOccurrencesOfString:#"(" withString:#"-" options:0 range:NSMakeRange(0, cleanedDataCellTest.length)];
[cleanedDataCellTest replaceOccurrencesOfString:#" " withString:#"" options:0 range:NSMakeRange(0, cleanedDataCellTest.length)];
But, for the consideration of performance, I think use NSString and its method stringByReplacingOccurrencesOfString is more efficient.
UPDATE: Sorry, yesterday I didn't test the "performance". I did a test just now, by replacing some thing in a string (about 26KB), use NSMutableString with replaceOccurrencesOfString:withString:options:range: is a little bit more efficient.
NSMutableString *cleanedDataCellTest = [[NSMutableString alloc] initWithString:datacellR2C2.text];
[cleanedDataCellTest replaceOccurrencesOfString:#"," withString:#"" options:0 range:NSMakeRange(0, cleanedDataCellTest.length)];
Ref: https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/nsmutablestring_Class/Reference/Reference.html#//apple_ref/occ/instm/NSMutableString/replaceOccurrencesOfString:withString:options:range:
Switching to NSString worked for me...:
NSString *cleanedDataCellTest = [[NSString alloc] initWithString:#"1,234,098"];
cleanedDataCellTest = [cleanedDataCellTest stringByReplacingOccurrencesOfString:#"," withString:#""];
cleanedDataCellTest = [cleanedDataCellTest stringByReplacingOccurrencesOfString:#")" withString:#""];
cleanedDataCellTest = [cleanedDataCellTest stringByReplacingOccurrencesOfString:#"(" withString:#"-"];
cleanedDataCellTest = [cleanedDataCellTest stringByReplacingOccurrencesOfString:#" " withString:#""];
NSLog(#"cleanedDataCellTest = %#", cleanedDataCellTest);
Shows:
cleanedDataCellTest = 1234098

Resources