Swift 2.0 Learning Notes (day 14)-INSERT, delete, and replace strings

Source: Internet
Author: User

Original articles, welcome reprint. Reprint Please specify: Dongsheng's Blog

The corresponding mutable string can be inserted, deleted, and replaced, and string provides several ways to help implement these operations. These methods are as follows:

Splice (_:atindex:). Inserts a string at the index position.

Insert (_:atindex:). Inserts a character at the index position.

Removeatindex (_:). Deletes a character at the index location.

RemoveRange (_:). Deletes a string within the specified range.

Replacerange (_:, with:string). Replaces a string in a specified range with a string or character.

Code:

var str = "Swift"

Print ("Raw string: \ (str)")

Str.splice ("Objective-c and". Characters, AtIndex:str.startIndex)

Print ("After inserting string: \ (str)")

Str.insert (".", AtIndex:str.endIndex)

Print ("insert. Word specifier: \ (str)")

Str.removeatindex (Str.endIndex.predecessor ())

Print ("delete. Word specifier: \ (str)")

var startIndex = Str.startindex

var endIndex = advance (StartIndex, 9)

var range = Startindex...endindex

Str.removerange (Range)

Print ("After delete range: \ (str)")

StartIndex = Str.startindex

EndIndex = advance (startIndex, 0)

Range = Startindex...endindex

Str.replacerange (range, with: "C + +")

Print ("Replace range: \ (str)")

Output Result:

Original string: Swift

After inserting a string: Objective-c and Swift

Insert. Word specifier: objective-c and Swift.

Delete. Word specifier: objective-c and Swift

After the scope is deleted: C and Swift

After replace range: C + + and Swift

Welcome to follow Dongsheng Sina Weibo @tony_ Dongsheng.

Learn about the latest technical articles, books, tutorials and information on the public platform of the smart Jie classroom

More Products iOS, Cocos, mobile design courses please pay attention to the official website of Chi Jie Classroom: http://www.zhijieketang.com

Luxgen Classroom Forum Website: http://51work6.com/forum.php

Swift 2.0 Learning Notes (day 14)-INSERT, delete, and replace strings

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.