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