
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Replace Character at Specific Index in Swift
To replace a character at a specified index with a new character Swift support the following methods ?
Using replaceSubrange() method.
Using the append() method.
Using replacingCharacters(in:with:).
Using these methods you can replace any character from the given string with a new character. For Example
Input: String = "Pink Bike"
Index = 5 Character = "O"
Output: "Pink Oike "
Here, we replaced the character present on index 5 which is "B" with "O".
Method 1: Using the replaceSubrange() method
To replace a character at a specified index in the given string we can use the replaceSubrange() method. This method replaces a range of elements with the specified elements in the given sequence or collection.
Syntax
func replaceSubrange(subRange: Range, with: NewElemens)
Here, the replaceSubrange() function takes two parameters ?
subRange ? It is a range of elements which we want to replace. It must contain valid start and end indices.
newElements ? New elements to which the subRange is replaced. Or we can say replacement elements.
Algorithm
Step 1 ? Create a string.
Step 2 ? Find the valid index of the character we want to replace using the index() function.
Step 3 ? Now replace the specified character with the new character using the replaceSubrange() method.
Step 4 ? Display output.
Example
In the following Swift program, we will replace a character at a specific index. So first we create a string. Then we find the valid index of the character we want to replace using the index() function. After that, we use the replaceSubrange() function to replace the original character with the specified new character. This function takes two parameters: range and the new element. So here we only replace one character so we will pass the indexValue?indexValue range in it and the new character that is "B". And finally, display the updated string.
import Foundation import Glibc var iStr = "My car color is yellow" print("Original String:", iStr) let indexValue = iStr.index(iStr.startIndex, offsetBy: 8) iStr.replaceSubrange(indexValue...indexValue, with: "B") print("Modified String:", iStr)
Output
Original String: My car color is yellow Modified String: My car cBlor is yellow
Method 2: Using the append() method
To replace a character at a specified index in the given string we run a for-in loop with enumerated() method. Then check if the current index is equal to the given index or not. If yes, then use the append() function to replace the original character with the new character. If not, then append the original characters for all the indices.
Syntax
func append(NewElement)
Here, the append() function takes only one parameter that is the new character, or replacement character.
Algorithm
Step 1 ? Create a string.
Step 2 ? Create a variable to store the index.
Step 3 ? Create another variable to store a new character.
Step 4 ? Create an empty string to store the resultant string.
Step 5 ? Now run a for-in loop with the enumerated() method to iterate through each character and their respective indexes.
Step 6 ? Check if the current index is equal to the given index. If yes, then replace the character present on that index with a new character using the append() method.
Step 7 ? Else append() all the original characters in the new string.
Step 8 ? Display output.
Example
In the following Swift program, we will replace a character at a specific index. So first we will create a new string, index and the new character. Then create an empty string to store the result. After that, we will run a for-in loop to iterate through each character and their index and check if the current index is equal to the given index. If yes, then replace the original character with a new character and append int the resultant string. If not, then append all the original characters in the resultant string. Finally, display the updated string.
import Foundation import Glibc var inputStr = "Black clouds" let indexVal = 4 let rChar: Character = "X" var newStr = "" for (x, char) in inputStr.enumerated() { if x == indexVal { newStr.append(rChar) } else { newStr.append(char) } } print("Original String:", inputStr) print("Modified String:", newStr)
Output
Original String: Black clouds Modified String: BlacX clouds
Method 3: Using the replacingCharacters() method
To replace a character at a specified index in the given string we will use replacingCharacters() method. This method returns a new string in which the specified character is replaced by the given replacement character.
Syntax
func replacingCharacters(in:InputChar, with: ReplacementChar)
Here, the replacingCharacters() method takes two parameters ?
subRange ? It is a range of characters or a character which we want to replace.
ReplacementChar ? It is the replacement character to which we replace the specified character or the range of characters.
Algorithm
Step 1 ? Create a string.
Step 2 ? Find the valid index of the character which we want to replace using the index() function.
Step 3 ? Now replace the specified character with the new character using the replaceSubrange() method.
Step 4 ? Display output.
Example
In the following Swift program, we will replace a character at a specific index. So first we will create a new string, index and the new character. Then create an empty string to store the result. After that, we will use the index() function to specify the start index and the offset to reach the desired index. Then we will use the replacingCharacters() method to replace the character at the specified index with "@". And display the output.
import Foundation import Glibc var StringVal = "Meeta is cooking Paratha" let rCharacter: Character = "@" let indexValue = 6 let outputStr = "" print("Original String: ", StringVal) let indexChar = StringVal.index(StringVal.startIndex, offsetBy: indexValue)..<StringVal.index(StringVal.startIndex, offsetBy: indexValue + 1) let resultString = StringVal.replacingCharacters(in: indexChar, with: String(rCharacter)) print("Modified String: ", resultString)
Output
Original String: Meeta is cooking Paratha Modified String: Meeta @s cooking Paratha
Conclusion
So this is how we can replace a character at a specified index. All the methods do their job efficiently. While working with the replaceSubrange() and replacingCharacters() methods you can also replace a range of characters with new characters whereas with the append() method you can replace only one character at the specified index.