字符串的分割截取和去掉空格

分割字符串

var a = "a, b, c, d, e, f"

let array = a.components(separatedBy:",")

最后结果:

array = ["a"," b"," c"," d"," e"," f"]

去掉空格

let newArray = []

for string in array {

let whitespace = NSCharacterSet.whitespaces

let str: String = string.trimmingCharacters(in: whitespace)

newArray.append(str)

}

最后结果:newArray = ["a",“b","c","d","e","f"]

字符串截取

三种方式: 1) subString(from:50)

                    2) substring(to:50)

                    3)substring(with:NSMakeRange(50, 8))

你可能感兴趣的:(字符串的分割截取和去掉空格)