To remove a character at a specific index in a string in Swift, you can use string manipulation functions like removeSubrange.
In this example,
str1
with a value of "Hello World"
.removeSubrange
method to remove the character at the specified index.var str1 = "Hello World"
let indexToRemove = str1.index(str1.startIndex, offsetBy: 6)
str1.remove(at: indexToRemove)
print("Modified string is:", str1)
Modified string is: Hello orld
In this example,
str2
with a value of "apple"
.removeSubrange
method to remove the character at the specified index.var str2 = "apple"
let indexToRemove = str2.index(str2.startIndex, offsetBy: 2)
str2.remove(at: indexToRemove)
print("Modified string is:", str2)
Modified string is: aple
In this tutorial, we learned How to Remove Character at specific Index in a String in Swift language with well detailed examples.