HomeSearch

Swift Replace String Example

Call replaceSubrange to replace strings. Use replacingOccurrences and replacingCharacters.
Replace. It seems like our world will last forever. But when the wind blows, the sand shifts. It changes shape and is replaced with something new.
Method notes. A string has a range of characters we want to change. With replaceSubrange we can replace them with another string. We use index() to create a range.
Example program. In this program we first declare a string. This string has a substring "red" in it. We next call replaceSubrange with a range.

Argument: We pass a range to replaceSubrange. We use index() on the string to get the bounds. We advance 6 chars.

End: We use the "..<" operator to advance to 9 chars into the string. We base the end also on the startIndex.

Range: half-open

Result: ReplaceSubrange changes the string "red" in the string to "yellow." The string remains colorful.

Swift program that replaces string with replaceSubrange var value = "green red blue" print(value) // Replace range at positions 6 through 9. // ... Specify a replacement string. let start = value.index(value.startIndex, offsetBy: 6); let end = value.index(value.startIndex, offsetBy: 6 + 3); value.replaceSubrange(start..<end, with: "yellow") print(value) Output green red blue green yellow blue
Change chars. This program loops through the chars in a string. It then uses an if-else block to change some chars in the string. The value "1" is changed.For

Tip: This approach can be used to translate chars in a string. A more complex data structure (dictionary) could be used.

Dictionary

Append: This method adds a char to a string. We build up a new string in the for-loop.

Swift program that replaces characters var value = "123abc123" var result = String() // Replace individual characters in the string. // ... Append them to a new string. for char in value.characters { if char == "1" { let temp: Character = "9" result.append(temp) } else { result.append(char) } } print(result) Output 923abc923
ReplacingOccurrences. In Foundation we find some string replacement methods. With replacingOccurrences we replace all occurrences of one substring with another.

Here: We change the word "cats" to "birds." This might make the "dogs" part happier.

Swift program that uses replacingOccurrences import Foundation let text = "cats and dogs" // Replace cats with birds. // ... Use Foundation method. let result = text.replacingOccurrences(of: "cats", with: "birds") // Print result. print(result) Output birds and dogs
ReplacingCharacters. Here we use replacingCharacters. This may have the same effect as the replaceSubrange method in Swift. It replaces a range with a substring.
Swift program that uses replacingCharacters import Foundation let letters = "zzzy" // Replace first three characters with a string. let start = letters.startIndex; let end = letters.index(letters.startIndex, offsetBy: 3); let result = letters.replacingCharacters(in: start..<end, with: "wh") // The result. print(result) Output why
A review. Strings in Swift are accessed through ranges, along with startIndex and endIndex. To replace chars, we access a range and invoke replaceSubrange.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls