美文网首页闻道丶iOS(尝鲜版)iOS Developer
字符串的分割截取和去掉空格

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

作者: 简简单单写书 | 来源:发表于2017-03-27 10:45 被阅读17次

分割字符串

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))

相关文章

网友评论

    本文标题:字符串的分割截取和去掉空格

    本文链接:https://www.haomeiwen.com/subject/epnlottx.html