Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace a substring with another substring

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

like image 645
Sagiftw Avatar asked Aug 31 '10 23:08

Sagiftw


People also ask

How do you replace a substring in another substring in Python?

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.

Which function is used to replace substring in a string?

The easiest way to replace all occurrences of a given substring in a string is to use the replace() function.

How do you replace all occurrences of substring in a string?

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.

How do you replace a substring in a string in Java without using replace () method?

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.


1 Answers

Try stringByReplacingOccurrencesOfString:withString:.

NSString* foo = @"axvc abc amamam dff abc kjd abc"; NSString* bar = [foo stringByReplacingOccurrencesOfString:@"abc" withString:@"def"]; NSLog("%@", bar); 
like image 128
zneak Avatar answered Sep 19 '22 20:09

zneak