Swift Learning Notes (18)--string string.index and range __swift

Source: Internet
Author: User




In the search for swift strings, it is sometimes possible to return a range rather than a specific location. This will use the concept of range in Swift. We'll take a detailed explanation of this.






(1) Retrieving a Word method in a string: rangeofstring










var str = "Hello Apple.hello Swift"

str.rangeofstring ("Hello")//forward retrieval;

str.rangeofstring ("Hello", Options: Nsstringcompareoptions.backwardssearch)//reverse retrieval;



str.rangeofstring ("swift")/Case exact match

str.rangeofstring (" Swift ", options:NSStringCompareOptions.CaseInsensitiveSearch)//Ignore case



Output results:




















Results Analysis:






You can see that the rangeofstring return value is a range range. If empty, returns nil. Rangeofstring () method has a lot of parameters to choose from, we can try.












(2) Find a specific string from the specified range of strings






Sometimes, according to our requirements, it is not necessary to retrieve a string from the entire length of the string, possibly in a range, where String.index is used.










var str = "Hello apple.hello Swift" let

startIndex:String.Index = Str.startindex let

endIndex:String.Index = Adva NCE (startIndex) let

strrange = range<string.index> (start:startindex,end:endindex)


Str.rangeofstring ("App", Options:NSStringCompareOptions.CaseInsensitiveSearch, Range:strrange)



Output results:




















(3) Intercept substring






Swift supports intercepting a string that intercepts a specified length from a location.










var str = "Hello apple.hello Swift"

var toindex = advance (Str.startindex, 4)

Str.substringtoindex (toindex)// Intercepts a string from the beginning to the 4th position;


var fromindex = advance (Str.startindex, 7)

Str.substringfromindex (fromindex)// To intercept from the 7th position to the final string;


Str.substringwithrange (Range<string.index> (Start:toindex, End:fromindex))// Intercepts a string from the 4th position to the 7th position;











Output results:

















Result analysis: After interception, the original string is unchanged.












(4) Insert character: Insert ()






Swift can insert a character at the specified position of the string. The code is as follows:










var str = "Hello apple.hello Swift"


var insertindex = advance (Str.startindex, 5)//insert Str.insert at 5th position

("!", ATINDEX:INSERTINDEX)//insert an exclamation point










Output results:

















(5) Remove character: Remove ()






Swift can delete a character at a location specified in the string. The code is as follows:










var str = "Hello apple.hello Swift"


var insertindex = advance (Str.startindex, 5)//insert Str.insert at 5th position

("!", ATINDEX:INSERTINDEX)//Insert an exclamation point


str.removeatindex (insertindex)

str



Output results:




















Result Analysis: Experiments show that the Insert () method and the Removeatindex () method modify the original string.












(6) Replacement string






In Swift, you can use one string to replace another string, as in the following code:










var str = "Hello Apple.hello Swift"//replace Hello with hi

var replaceendindex = advance (Str.startindex, 5)// From the start character to the fifth character (5 is the open interval)

str.stringbyreplacingcharactersinrange (range<string.index>) (Start:str.startIndex, End:replaceendindex), withstring: "HI")//Replace with HI



Output results:




















GitHub home: https://github.com/chenyufeng1991. You are welcome to visit.










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.