Learning Swift from scratch (day 14)--Inserting, deleting, and replacing strings

Source: Internet
Author: User

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

corresponding variable strings can be inserted, deleted, and replaced, String Several methods are available 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-cand " .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.startindexendindex =advance (startindex, 0) range  =startindex...endindex        str.replacerange (range,with:  "C + +")    print ("After replace range: \(str) ") 


Output Result:

Original string: Swift

after inserting the string: Objective-c and Swift

Insert . word specifier: Objective-cand Swift.

Delete . word specifier: Objective-cand Swift

after the scope is deleted: C and Swift

after the range is replaced: 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
650) this.width=650; "title=" 00.png "src=" http://s2.51cto.com/wyfs02/M01/7C/A1/wKioL1bUDhGBEhTQAAAs2MBEZnc313.png "alt=" Wkiol1budhgbehtqaaas2mbeznc313.png "/>
More ProductsIOS,Cocos, mobile Design course please pay attention to the official website of Chi Jie Classroom:http://www.zhijieketang.com
Smart-Jie Classroom Forum Website:http://51work6.com/forum.php


This article is from the "Dongsheng-ios Technical Consultant" blog, make sure to keep this source http://tonyguan.blog.51cto.com/701759/1746108

Learning Swift from scratch (day 14)--Inserting, deleting, and replacing strings

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.