加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift开发教程--字符串的操作

发布时间:2020-12-14 01:58:04 所属栏目:百科 来源:网络整理
导读:替换 把?替换为/ var url = "http://blog.csdn.net/hello_hwc?viewmode=list" var filtered = url.stringByReplacingOccurrencesOfString( "?" ,withString: "/" ,options: NSStringCompareOptions.LiteralSearch, range : nil ) 结果 "http://blog.csdn.net

替换

把?替换为/

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

结果

"http://blog.csdn.net/hello_hwc/viewmode=list"
 

过滤

过滤掉单个字符/

结果

过滤掉开头和结尾的空白


切割

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

结果是一个数组


var url = "http://blog.csdn.net/hello_hwc?viewmode=list" var filtered = url.stringByReplacingOccurrencesOfString("/",withString: "",range: nil)"http:blog.csdn.nethello_hwc?viewmode=list"var url = " http://blog.csdn.net/hello_hwc?viewmode=list " var newString = url.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())"http://blog.csdn.net/hello_hwc?viewmode=list"var url = "http://blog.csdn.net/hello_hwc?viewmode=list" let splitedarray = split(url){$0 == "/"}"http:" "blog.csdn.net" "hello_hwc?viewmode=list"

拼接

let splitedarray = ["1","2","3"]
let result = join("/",splitedarray)"1/2/3"

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读