(迅速)删除" \"字符串中的字符?

时间:2015-05-12 06:16:17

标签: string swift backslash

我的字符串包含很少\,我想删除所有这些字符串。任何人都可以告诉我我该怎么做。我已经尝试了stringByReplacingOccurrencesOfString,但它对我不起作用。

3 个答案:

答案 0 :(得分:11)

var str = "\\dagdahughuad\\dajughdaug"
var str2 = str.stringByReplacingOccurrencesOfString("\\", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
print(str2)

这将输出

dagdahughuaddajughdaug

答案 1 :(得分:5)

将以上代码转换为SWIFT 3

function balanceParens(string) {
    let max = 0;
    let res = string.split("").reduce((counter, char) => {
        // Handle if parens open and close out of order
        if (counter < 0) { 
          return counter;
        }
        // Add 1 for each open in order
        if (char === "(") {
          if(++counter > max) {
            max = counter;
          }
          return counter;
        }
        // subtract 1 for each close in order
        if (char === ")") {
          return --counter;
        }
        // handle use case if char is not a paren
        return counter;
    }, 0);
    console.log("Max depth was :", max);
    return !res;
}
console.log(balanceParens("((()(((())))))((((()))))"));

答案 2 :(得分:4)

迅速5

对于某些unicode复杂值,可能-使用文字

join

通常

let str2 = str1.replacingOccurrences(of: "\\", with: "", options: .literal, range: nil)