I want to replace the string "abc" to "def" each time it appears in my NSString object: "axcd abc amamam dff abc kdj abc"
How do I do that??
Thanks,
Sagiftw
Python String | replace() replace() is an inbuilt function in the Python programming language that returns a copy of the string where all occurrences of a substring are replaced with another substring. Parameters : old – old substring you want to replace. new – new substring which would replace the old substring.
The easiest way to replace all occurrences of a given substring in a string is to use the replace() function.
To replace all occurrences of a substring in a string by a new one, you can use the replace() or replaceAll() method: replace() : turn the substring into a regular expression and use the g flag.
To replace a character in a String, without using the replace() method, try the below logic. Let's say the following is our string. int pos = 7; char rep = 'p'; String res = str. substring(0, pos) + rep + str.
Try stringByReplacingOccurrencesOfString:withString:
.
NSString* foo = @"axvc abc amamam dff abc kjd abc"; NSString* bar = [foo stringByReplacingOccurrencesOfString:@"abc" withString:@"def"]; NSLog("%@", bar);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With