Swift 字符串替换/过滤/切割/拼接

原创blog,转载请注明出处 blog.csdn.net/hello_hwc

之前写过一篇Swift String的基础,想了解的同学可以看下。

替换

把?替换为/

var url = “http://blog.csdn.net/hello_hwc?viewmode=list”var filtered = url.stringByReplacingOccurrencesOfString(“?”, withString: “http://blog.csdn.net/”, options: NSStringCompareOptions.LiteralSearch, range: nil)

结果

“http://blog.csdn.net/hello_hwc/viewmode=list”过滤

过滤掉单个字符/

var url = “http://blog.csdn.net/hello_hwc?viewmode=list”var filtered = url.stringByReplacingOccurrencesOfString(“http://blog.csdn.net/”, withString: “”, options: NSStringCompareOptions.LiteralSearch, range: nil)

结果

“http:blog.csdn.nethello_hwc?viewmode=list”

过滤掉开头和结尾的空白

var url = ” ?viewmode=list “var newString = url.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())

结果

“http://blog.csdn.net/hello_hwc?viewmode=list”切割

对字符串使用/作为分隔符来切割,不允许空字符串 使用split函数

var url = “http://blog.csdn.net/hello_hwc?viewmode=list”let splitedarray = split(url){$0 == “http://blog.csdn.net/”}

结果是一个数组

对字符串使用/作为分隔符来切割,允许空字符串

char == “http://blog.csdn.net/”}

结果

拼接let splitedarray = [“1″,”2″,”3”]let result = join(“http://blog.csdn.net/”, splitedarray)

结果

“1/2/3”

,玩坏了可以选择重来,

Swift 字符串替换/过滤/切割/拼接

相关文章:

你感兴趣的文章:

标签云: