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