最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift分割字符串为字符串数组

Swift crifan 2783浏览 0评论
swift中,想要把:
“wang ba”
分割为:
字符串数组
“wang”, “ba”
搜:
swift split string to array
参考:
最后自己实现了:
//split string into string array, normally seperator is white space
//if origin string not containing white space, then pass the empty string here, will split to every single char as string
func splitSingleStrToStrArr(strToSplit:String, seperatorStr:String) -> [String] {
    print("strToSplit=\(strToSplit), seperatorStr=\(seperatorStr)")

    var splitedStrArr:[String] = [String]()
    if !seperatorStr.isEmpty {
        let seperatorChar:Character = splitSingleStrToCharArr(seperatorStr)[0]
        splitedStrArr = strToSplit.characters.split(seperatorChar).map(String.init)
    }
    else
    {
        //split string(without space) into every single char as string
        let splitedCharArr:[Character] = splitSingleStrToCharArr(strToSplit)
        for eachChar in splitedCharArr {
            splitedStrArr.append(String(eachChar))
        }
    }

    print("splitedStrArr=\(splitedStrArr)")

    return splitedStrArr
}
调用效果:
    /*
    * splitSingleStrToStrArr demo
    */
    let noneSeperatorStr:String = ""
    let strWithoutSpace:String = "中间没有空格单个字符串"
    let splitedStrArr_withoutSpace:[String] = splitSingleStrToStrArr(strWithoutSpace, seperatorStr: noneSeperatorStr)
    print("splitedStrArr_withoutSpace=\(splitedStrArr_withoutSpace)") //["中", "间", "没", "有", "空", "格", "单", "个", "字", "符", "串"]

    let spaceSeperatorStr:String = " "
    let stringContainingSpace:String = "中 间  有  空    格 的 字  符 串 "
    let splitedStrArr_containingSpace:[String] = splitSingleStrToStrArr(stringContainingSpace, seperatorStr: spaceSeperatorStr)
    print("splitedStrArr_containingSpace=\(splitedStrArr_containingSpace)") //["中", "间", "有", "空", "格", "的", "字", "符", "串"]
最后演化为:
extension String {

    //split string into string array, normally seperator is white space
    //if origin string not containing white space, then pass the empty string here, will split to every single char as string
    func splitToStrArr(seperatorStr:String = "") -> [String] {
        guard self.notEmpty else {
            return [String]()
        }
        
        //print("strToSplit=\(strToSplit), seperatorStr=\(seperatorStr)")
        
        var splitedStrArr:[String] = [String]()
        if seperatorStr.notEmpty {
            let seperatorChar:Character = seperatorStr.splitToCharArr()[0]
            splitedStrArr = self.characters.split(seperatorChar).map(String.init)
        } else {
            //split string(without space) into every single char as string
            let splitedCharArr:[Character] = self.splitToCharArr()
            for eachChar in splitedCharArr {
                splitedStrArr.append(String(eachChar))
            }
        }
        
        //print("splitedStrArr=\(splitedStrArr)")
        
        return splitedStrArr
    }
    
}
示例代码:
    /*
    * splitToStrArr demo
    */
    //let noneSeperatorStr:String = ""
    let strWithoutSpace:String = "中间没有空格单个字符串"
    //let splitedStrArr_withoutSpace:[String] = strWithoutSpace.splitToStrArr(noneSeperatorStr)
    let splitedStrArr_withoutSpace:[String] = strWithoutSpace.splitToStrArr()
    print("splitedStrArr_withoutSpace=\(splitedStrArr_withoutSpace)") //["中", "间", "没", "有", "空", "格", "单", "个", "字", "符", "串"]

    let spaceSeperatorStr:String = " "
    let stringContainingSpace:String = "中 间   有  空     格 的 字   符 串 "
    let splitedStrArr_containingSpace:[String] = stringContainingSpace.splitToStrArr(spaceSeperatorStr)
    print("splitedStrArr_containingSpace=\(splitedStrArr_containingSpace)") //["中", "间", "有", "空", "格", "的", "字", "符", "串"]

注:这类函数已整理至我的库:

https://github.com/crifan/CrifanString

转载请注明:在路上 » [已解决]swift分割字符串为字符串数组

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.188 seconds, using 22.05MB memory