Swift 字符串替换不带NSString API的子字符串

Swift 字符串替换不带NSString API的子字符串,swift,Swift,我希望能够找到并替换本机Swift字符串中出现的子字符串,而无需桥接到NS类。我怎样才能做到这一点 这不是重复的,因为问题是替换单个字符。这个问题是关于查找和替换可能包含许多字符的子字符串。通用和纯Swift方法 func splitBy<T: RangeReplaceableCollection>(_ s:T, by:T)->[T] where T.Iterator.Element:Equatable { var tmp = T() var res = [T]

我希望能够找到并替换本机Swift字符串中出现的子字符串,而无需桥接到NS类。我怎样才能做到这一点


这不是重复的,因为问题是替换单个字符。这个问题是关于查找和替换可能包含许多字符的子字符串。

通用和纯Swift方法

func splitBy<T: RangeReplaceableCollection>(_ s:T, by:T)->[T] where T.Iterator.Element:Equatable {
    var tmp = T()
    var res = [T]()
    var i:T.IndexDistance = 0
    let count = by.count

    var pc:T.Iterator.Element {
        get {
            i %= count
            let idx = by.index(by.startIndex, offsetBy: i)
            return by[idx]
        }
    }

    for sc in s {
        if sc != pc {
            i = 0
            if sc != pc {
            } else {
                i = i.advanced(by: 1)
            }
        } else {
            i = i.advanced(by: 1)
        }
        tmp.append(sc)
        if i == count {
            tmp.removeSubrange(tmp.index(tmp.endIndex, offsetBy: -i)..<tmp.endIndex)
            res.append(tmp)
            tmp.removeAll()
        }
    }

    res.append(tmp)
    return res
}

func split(_ s:String, by:String)->[String] {
    return splitBy(s.characters, by: by.characters).map(String.init)
}


extension RangeReplaceableCollection where Self.Iterator.Element: Equatable {
    func split(by : Self)->[Self]  {
        return splitBy(self, by: by)
    }
}

通用和纯Swift方法

func splitBy<T: RangeReplaceableCollection>(_ s:T, by:T)->[T] where T.Iterator.Element:Equatable {
    var tmp = T()
    var res = [T]()
    var i:T.IndexDistance = 0
    let count = by.count

    var pc:T.Iterator.Element {
        get {
            i %= count
            let idx = by.index(by.startIndex, offsetBy: i)
            return by[idx]
        }
    }

    for sc in s {
        if sc != pc {
            i = 0
            if sc != pc {
            } else {
                i = i.advanced(by: 1)
            }
        } else {
            i = i.advanced(by: 1)
        }
        tmp.append(sc)
        if i == count {
            tmp.removeSubrange(tmp.index(tmp.endIndex, offsetBy: -i)..<tmp.endIndex)
            res.append(tmp)
            tmp.removeAll()
        }
    }

    res.append(tmp)
    return res
}

func split(_ s:String, by:String)->[String] {
    return splitBy(s.characters, by: by.characters).map(String.init)
}


extension RangeReplaceableCollection where Self.Iterator.Element: Equatable {
    func split(by : Self)->[Self]  {
        return splitBy(self, by: by)
    }
}

基础的方法

extension String {
    func replacing(_ oldString: String, with newString: String) -> String {

        guard !oldString.isEmpty, !newString.isEmpty else { return self }

        let charArray = Array(self.characters)
        let oldCharArray = Array(oldString.characters)
        let newCharArray = Array(newString.characters)

        var matchedChars = 0
        var resultCharArray = [Character]()

        for char in charArray {
            if char == oldCharArray[matchedChars] {
                matchedChars += 1
                if matchedChars == oldCharArray.count {
                    resultCharArray.append(contentsOf: newCharArray)
                    matchedChars = 0
                }
            } else {
                for i in 0 ..< matchedChars {
                    resultCharArray.append(oldCharArray[i])
                }
                if char == oldCharArray[0] {
                    matchedChars = 1
                } else {
                    matchedChars = 0
                    resultCharArray.append(char)
                }
            }
        }

        return String(resultCharArray)

    }
}
输出:

Hi World HelHiello Hi HellHioo

使用
基础的方法

extension String {
    func replacing(_ oldString: String, with newString: String) -> String {

        guard !oldString.isEmpty, !newString.isEmpty else { return self }

        let charArray = Array(self.characters)
        let oldCharArray = Array(oldString.characters)
        let newCharArray = Array(newString.characters)

        var matchedChars = 0
        var resultCharArray = [Character]()

        for char in charArray {
            if char == oldCharArray[matchedChars] {
                matchedChars += 1
                if matchedChars == oldCharArray.count {
                    resultCharArray.append(contentsOf: newCharArray)
                    matchedChars = 0
                }
            } else {
                for i in 0 ..< matchedChars {
                    resultCharArray.append(oldCharArray[i])
                }
                if char == oldCharArray[0] {
                    matchedChars = 1
                } else {
                    matchedChars = 0
                    resultCharArray.append(char)
                }
            }
        }

        return String(resultCharArray)

    }
}
您可以在
String
结构上使用
replacingOccurrences
方法

let myString = "Hello World"
let newString = myString.replacingOccurrences(of: "World", with: "Everyone")
print(newString) // prints "Hello Everyone"

基础的方法

extension String {
    func replacing(_ oldString: String, with newString: String) -> String {

        guard !oldString.isEmpty, !newString.isEmpty else { return self }

        let charArray = Array(self.characters)
        let oldCharArray = Array(oldString.characters)
        let newCharArray = Array(newString.characters)

        var matchedChars = 0
        var resultCharArray = [Character]()

        for char in charArray {
            if char == oldCharArray[matchedChars] {
                matchedChars += 1
                if matchedChars == oldCharArray.count {
                    resultCharArray.append(contentsOf: newCharArray)
                    matchedChars = 0
                }
            } else {
                for i in 0 ..< matchedChars {
                    resultCharArray.append(oldCharArray[i])
                }
                if char == oldCharArray[0] {
                    matchedChars = 1
                } else {
                    matchedChars = 0
                    resultCharArray.append(char)
                }
            }
        }

        return String(resultCharArray)

    }
}
输出:

Hi World HelHiello Hi HellHioo

使用
基础的方法

extension String {
    func replacing(_ oldString: String, with newString: String) -> String {

        guard !oldString.isEmpty, !newString.isEmpty else { return self }

        let charArray = Array(self.characters)
        let oldCharArray = Array(oldString.characters)
        let newCharArray = Array(newString.characters)

        var matchedChars = 0
        var resultCharArray = [Character]()

        for char in charArray {
            if char == oldCharArray[matchedChars] {
                matchedChars += 1
                if matchedChars == oldCharArray.count {
                    resultCharArray.append(contentsOf: newCharArray)
                    matchedChars = 0
                }
            } else {
                for i in 0 ..< matchedChars {
                    resultCharArray.append(oldCharArray[i])
                }
                if char == oldCharArray[0] {
                    matchedChars = 1
                } else {
                    matchedChars = 0
                    resultCharArray.append(char)
                }
            }
        }

        return String(resultCharArray)

    }
}
您可以在
String
结构上使用
replacingOccurrences
方法

let myString = "Hello World"
let newString = myString.replacingOccurrences(of: "World", with: "Everyone")
print(newString) // prints "Hello Everyone"

真的有可能复制吗?为什么所有的反对票?这不是一个重复,并根据答案,非特里维利没有投票,但我很好奇,知道为什么不使用基金会的原因?当然这不是纯粹的Swift,但是有没有理由用一行代码换20行呢?@Adrian避免了
NSString–String
二元性,如果除了“如果我不是在苹果平台上开发的话,我宁愿不导入大量的传统苹果库”以外的任何原因真的需要可能的复制品吗?为什么所有的反对票?这不是一个重复,并根据答案,非特里维利没有投票,但我很好奇,知道为什么不使用基金会的原因?当然,这不是纯粹的Swift,但是有没有理由用一行代码换20行呢?@Adrian避免了
NSString–String
二元性,如果除了“如果我不是在苹果平台上开发的话,我宁愿不导入大量的传统苹果库”之外的任何理由都是必要的