Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift: how can I delete a specific character?

a string such as ! !! yuahl! ! , I want delete ! and , when I code like this

for index in InputName.characters.indices {
    if String(InputName[index]) == "" || InputName.substringToIndex(index) == "!" {
        InputName.removeAtIndex(index)
    }
}

have this error " fatal error: subscript: subRange extends past String end ", how should I do? THX :D

like image 340
thirtyyuan Avatar asked Aug 28 '16 01:08

thirtyyuan


People also ask

How to remove specific characters from a string in Swift?

How to Remove Specific Characters from a String in Swift? To remove specific set of characters from a String in Swift, take these characters to be removed in a set, and call the removeAll (where:) method on this string str, with the predicate that if the specific characters contain this character in the String.

How to remove a specific object from an element in Swift?

To remove a specific object from an element in swift, we can use multiple ways of doing it. Let’s see this in the playground with help of an example. First, let’s create an array of String. We’ll do it with the following methods as shown below: Method 1 − Using the filter method of the array.

How to remove a character from a string in Java?

The remove () method removes a character in the string at the specified position. Here, string is an object of the String class. The remove () method takes only one parameter. var message = "Hello, World!" print("Before Removing:", message) // position of the character to remove var i = message.index (message.startIndex, offsetBy: 12)

How to remove character at position i=6 in string in Java?

where str1 is the string and i is the index. i is of type String.Index. remove () method removes character at index i in the string str1 and returns this removed character. In the following program, we will take a string str1, and remove the character at position i=6 using String.remove () method.


1 Answers

Swift 5+

let myString = "aaaaaaaabbbb"
let replaced = myString.replacingOccurrences(of: "bbbb", with: "") // "aaaaaaaa"
like image 22
Ahmadreza Avatar answered Oct 25 '22 15:10

Ahmadreza