Search and Replace in a String
·
201 words
·
1 minute read
As programmers we often need to take a string and replace parts of it with something else. The code below has three examples, first of which is a basic ‘find all’ and replace, the second changes only the first occurrence of ‘sound’ and finally the third example demonstrates how to change a string containing quotes to use escaped quotes. These are changed by using the 4th argument to define how many times to replace, with -1 being every time.
All this functionality is managed by the strings
package and the Replace
function (view docs).
|
|