Swift string replacement/filtering/cutting/splicing

Source: Internet
Author: User

Original blog, reproduced please indicate the source
Blog.csdn.net/hello_hwc

Previously wrote a swift string foundation, want to understand the classmate can look down.
http://blog.csdn.net/hello_hwc/article/details/39853023

Replace

To replace with A/

var"http://blog.csdn.net/hello_hwc?viewmode=list"var filtered = url.stringByReplacingOccurrencesOfString("?""/"rangenil)

Results

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

Filter out individual characters/

var"http://blog.csdn.net/hello_hwc?viewmode=list"var filtered = url.stringByReplacingOccurrencesOfString("/"""rangenil)

Results

"http:blog.csdn.nethello_hwc?viewmode=list"

Filter out the opening and closing blanks

var"   http://blog.csdn.net/hello_hwc?viewmode=list   "var newString = url.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())

Results

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

Use/as delimiter for string to cut, no empty string allowed
Using the Split function

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

The result is an array

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

Use/as delimiter for string to cut, allow empty string

var"http://blog.csdn.net/hello_hwc?viewmode=list"let arrayresult = split(url, maxSplit:url.lengthOfBytesUsingEncoding(NSUTF8StringEncoding), allowEmptySlices: true) { (char:Character) ->in    return"/"}

Results

"http:""""blog.csdn.net""hello_hwc?viewmode=list"
Stitching
let splitedarray = ["1","2","3"]letjoin("/", splitedarray)

Results

"1/2/3"

Swift string replacement/filtering/cutting/splicing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.