stringByReplacingOccurrencesOfString: doesn't work - ios

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);

Related

stringByReplacingOccurrencesOfString is not working for space i.e, " " in objective c

- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty;
{
[self dismissViewControllerAnimated:YES completion: nil];
CNLabeledValue *phoneNumberValue = contactProperty.value;
NSString *contactString = [phoneNumberValue valueForKey:#"_stringValue"];
contactString = [contactString stringByReplacingOccurrencesOfString:#"-" withString:#""];
contactString = [contactString stringByReplacingOccurrencesOfString:#" " withString:#""]; // This line of code is not working properly
contactString = [contactString stringByReplacingOccurrencesOfString:#"(" withString:#""];
contactString = [contactString stringByReplacingOccurrencesOfString:#")" withString:#""];
txtRecipient.text = contactString;
}
This is my code. I am using contactPicker to pick a contact from phonebook. Then I am storing it in to a string variable. After that I am removing dashes, brackets and spaces from string value by using stringByReplacingOccurrencesOfString. Every thing is working fine except for this line:
contactString = [contactString stringByReplacingOccurrencesOfString:#" " withString:#""];
After this line of code contactString remains the same i.e, spaces didn't get removed from the string. I also tried componentsSeparatedByString function but its returning only 1 character. i.e,
NSArray *components = [dateString componentsSeparatedByString:#" "];
components.length is returning 1.
Hope you understand my question. Is there any other way of removing spaces from a string? Any kind of help would be appreciable. Thanks.
Look like this is not a standard space.
Try this:
NSMutableCharacterSet* set = [NSMutableCharacterSet whitespaceCharacterSet];
[set addCharactersInString:#"()-"];
NSMutableString * contactString = [[phoneNumberValue valueForKey:#"_stringValue"] mutableCopy];
NSRange range;
while ((range = [contactString rangeOfCharacterFromSet:set]).location!=NSNotFound) {
[contactString deleteCharactersInRange:range];
}

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

\n for NSString not work for string from NSData

I retrieve NSData from a server containing \n. When I convert it into an NSString, \n is not working. It prints "\n".
My code is like this:
NSString *st=[NSString stringWithFormat:#"%#",data];
Thanks
Try with this:
NSString * string = [[NSString alloc] initWithData:yourData encoding:NSUTF8StringEncoding];
That may help you.
myString = [myString stringByReplacingOccurrencesOfString:#"\n" withString:#" "];

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

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