Replace characters of a string in Swift
In this tutorial, you will see how we can replace characters or part of a string in Swift program with examples.
You will learn 3 methods of replacing string characters using Swift. Below are given the methods that you will use for replacing string characters in this tutorial:
- replacingCharacters
- replacingOccurrences
- replaceSubrange
Using replacingOccurrences method
Before we go forward, I want to let you know that the replacingOccurrences method is not mutating. That means, this method returns a new string and it doesn’t make any change in the original string. The original string will be as is and create a new string.
Now let’s see the example below:
import UIKit let myString = "This is good and that is also so good" var usingreplacingOccurrences = myString.replacingOccurrences(of: "good",with: "bad") print(usingreplacingOccurrences)
In the above code, we take a string that contains “This is good and that is also so good“. After we replace the word “good” with “bad” in the string using the replacingOccurrences() method, we can see the output given below:
This is bad and that is also so bad
If you notice in the above code, you can see that we have the word “good” twice in the original string. After replacing it, we can see that it has changed in both occurrences of “good” in the string myString
.
Let’s see another example:
import UIKit let myString = "This is amazing" var usingreplacingOccurrences = myString.replacingOccurrences(of: "is",with: "was") print(usingreplacingOccurrences)
Output:
Thwas was amazing
In the above example, we can see that both occurrences of “is” changed into “was” and the word “this” also changed into “Thwas” which is unexpected. But it should only change the stand-alone words “is“.
Below is the example, where we have solved the problem:
import UIKit let myString = "This is amazing" var usingreplacingOccurrences = myString.replacingOccurrences(of: #"\bis"#, with: "was", options: .regularExpression, range: nil) print(usingreplacingOccurrences)
And the output for the above code will be:
This was amazing
From the output, you can see that the standalone word “is” is only replaced with “was” just like we accepted.
Using replacingCharacters method
The replacingCharacters method in Swift returns a new string and doesn’t replace the original string provided. That means it doesn’t mutate the string that you provide to this method just like the replacingOccurrences method.
In this method, you have to pass in a range that will be the characters to be replaced. After that, you will pass the string that will be used as a replacement.
Now see the example below:
import UIKit let myString = "He is a good boy" if let range = myString.range(of: "is") { let replacedString = myString.replacingCharacters(in: range,with: "was") print(replacedString) }
And the output will be:
He was a good boy
In the above example, we have changed the “is” into “was“.
Using replaceSubrange method
In the above examples, all the examples will not mutate the original string. But if you use the replaceSubrange method to replace string characters in Swift programming, then it will mutate the original string also.
Now have a look at the example code below:
import UIKit var myString = "Replace a substring" let endIndex = myString.index(myString.startIndex,offsetBy: 8) myString.replaceSubrange(...endIndex,with: "Replaced the") print(myString)
Output:
Replaced the substring
In the above example, you can see that, we take the store the string in a variable, not in a constant (let). This is because the replaceSubrange is a muting method.
After that, we get the end index that is using the start index and we offset it by 8.
In the end, we have passed it through the range and then through the replacement string.
Leave a Reply