如何从swift中删除字符串中的特定字符或单词?

时间:2018-01-23 07:08:37

标签: ios swift

var myString = "43321 This is example hahaha 4356-13"

我想要结果

var resultString = "This is example"

换句话说,我想删除某些单词和数字。

怎么可能?

我已经完成了搜索,但我找不到我想要的内容。

请回答我。感谢

好的,确切地说,我想删除所有数字并删除我想要的某些单词。

我犯了一个大错。我很抱歉。确切地说,删除数字和某些单词是正确的。但是,我不应该总是删除所有数字,但如果数字位于字符串旁边则保留它们。

例)

let testString1 = "123123123123"
let testString2 = "apple 65456876"
let testString3 = "apple banana3 horse5"
let testString4 = "44 apple banana1banana 5horse"
let testString5 = "123 banana123 999"

我想删除单词“apple”。

结果是

let resultString1 = ""
let resultString2 = ""
let resultString3 = "banana3 horse5"
let resultString4 = "banana1banana 5horse"
let resultString5 = "banana123"

5 个答案:

答案 0 :(得分:2)

您可以尝试应用以下代码,

var sentence = "43321 This is example hahaha 4356-13"
sentence = (sentence.components(separatedBy: NSCharacterSet.decimalDigits) as NSArray).componentsJoined(by: "")
let wordToRemove = "hahaha"


if let range = sentence.range(of: wordToRemove) {
   sentence.removeSubrange(range)
}

print(sentence) // This is example -

答案 1 :(得分:0)

你可以试试这个:

Attribute

答案 2 :(得分:0)

使用此字符串扩展名。

extension String

{
 func removeNumbersAString(str:String)->String
 {

    var aa = self.components(separatedBy: CharacterSet.decimalDigits).joined(separator: " ")

    let regexp = " \\d* "
    var present = self
    while present.range(of:regexp, options: .regularExpression) != nil  {
        if let range = present.range(of:regexp, options: .regularExpression) {
            let result = present.substring(with:range)

           present = present.replacingOccurrences(of: result, with: "")

           // print("before \(result)")
        }
    }

    return present
 }

}

测试

 var str = "dsghdsghdghdhgsdghghdghds 12233 apple"

 print("before \(str)")   /// before dsghdsghdghdhgsdghghdghds 12233 apple

 print("after \(str.removeNumbersAString(str: "apple"))")    /// after dsghdsghdghdhgsdghghdghds

答案 3 :(得分:0)

尝试以下代码:

var myString = "43321 This is example hahaha 4356-13"
        //to remove numbers
        myString = (myString.components(separatedBy: NSCharacterSet.decimalDigits) as NSArray).componentsJoined(by: "")

        // to remove specific word
        if let range = myString.range(of: "hahaha") {
            myString.removeSubrange(range)
        }
        print(myString)

答案 4 :(得分:0)

我认为使用正则表达式解决方案会更快:

//use NSMutableString so the regex.replaceMatches later will work
var myString:NSMutableString = "43321 This is example hahaha 4356-13"

//add more words to match by using | operator e.g. "[0-9]{1,}|apple|orange"
//[0-9]{1,} simply removes all numbers but is more efficient than [0-9]
let regexPattern = "[0-9]{1,}|apple"

//caseInsensitive or the regex will be much longer
let regex = try NSRegularExpression(pattern: regexPattern, options: .caseInsensitive) 

var matches = regex.matches(in: myString as String, options: .withoutAnchoringBounds, range: range)
regex.replaceMatches(in: myString, options: .withoutAnchoringBounds, range: range, withTemplate: "")

print(myString) // This is example hahaha -

后续字符串

var testString3: NSMutableString = "apple banana horse"
matches = regex.matches(in: testString3 as String, options: .withoutAnchoringBounds, range: range)
regex.replaceMatches(in: testString3, options: .withoutAnchoringBounds, range: range, withTemplate: "")
print(testString3) // banana horse