20

I am looking for a simple way to remove the 4 characters in the tilesColored String "ment" from the shuffledWord1.

var word1: String = "employment" var shuffledWord1: String = "melpyoemtn" var tilesColored: String = "ment" var characters = Array(tilesColored) // gives ["m","e","n","t"] let newWord1 = word1.StringByReplacingOccurencesOfString("\(characters[0])", withString:"") // gives "elpyoetn" 

stringByReplacingOccurencesOfString only allows 1 character to be checked and removes BOTH m's, so how can I check against all 4 and only remove ONE instance of each to return "melpyo"?

Thanks in advance for any help possible

2
  • Are you trying to remove the last 4 characters or are you trying to remove one instance of m, e, n and t, but not in any particular order? Commented Sep 1, 2015 at 19:54
  • hi keith. I am trying to remove the instance of the coloured tiles m,e,n,t Commented Sep 1, 2015 at 20:52

5 Answers 5

24

Swift 3+ version with better performance than the previous top answers. (Because we don't separate into arrays with substrings, which all would need seperate allocations.)

This here just works on the unicode scalar level. You can paste it right into a playground.

import Foundation extension String { func removeCharacters(from forbiddenChars: CharacterSet) -> String { let passed = self.unicodeScalars.filter { !forbiddenChars.contains($0) } return String(String.UnicodeScalarView(passed)) } func removeCharacters(from: String) -> String { return removeCharacters(from: CharacterSet(charactersIn: from)) } } let str = "n1o d2i3g4i5t6s!!!789" let t1 = str.removeCharacters(from: CharacterSet.decimalDigits.inverted) print(t1) // will print: 123456789 let t2 = str.removeCharacters(from: "0123456789") print(t2) // will print: no digits!!! 
Sign up to request clarification or add additional context in comments.

1 Comment

Works perfectly
11

Swift 3 version of Macondo2Seattle's answer, which, I think, is the most elegant solution.

extension String { func removing(charactersOf string: String) -> String { let characterSet = CharacterSet(charactersIn: string) let components = self.components(separatedBy: characterSet) return components.joined(separator: "") } } 

Comments

9

Swift 5:

var phrase = "The rain in Spain stays mainly in the plain." let vowels: Set<Character> = ["a", "e", "i", "o", "u"] phrase.removeAll(where: { vowels.contains($0) }) // phrase == "Th rn n Spn stys mnly n th pln." 

1 Comment

doesn't work, says type of expression needs more context4
1
extension String { func removeCharacters(characters: String) -> String { let characterSet = NSCharacterSet(charactersInString: characters) let components = self.componentsSeparatedByCharactersInSet(characterSet) let result = components.joinWithSeparator("") return result } } 

Comments

0

Swift 2.0:

extension String { func stringByRemovingOnce(chars: String) -> String { var cs = Set(chars.characters) let fd = characters.filter { c in cs.remove(c).map { _ in false } ?? true } return String(fd) } } "melpyoemtn".stringByRemovingOnce("ment") // "lpyoem" 

Swift 1.2:

extension String { func stringByRemovingOnce(chars: String) -> String { var cs = Set(chars) let fd = filter(self) { c in cs.remove(c).map { _ in false } ?? true } return String(fd) } } "melpyoemtn".stringByRemovingOnce("ment") // "lpyoem" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.