[Swift] The pit of a string

Source: Internet
Author: User

    1. Gets the character of a location in the string, the difference between the swift versions
      • Swift 3.0
Let text = "abc" Let INDEX2 = Text.index (Text.startindex, offsetby:2)//will call succ 2 timeslet lastchar:character = Te XT[INDEX2]//now we can index!let characterIndex2 = Text.characters.index (Text.characters.startIndex, Offsetby:2) let LASTCHAR2 = Text.characters[characterindex2]//will do the samelet range:range<string.index> = Text.range (of: "B") !let index:int = text.distance (From:text.startIndex, To:range.lowerBound)
      • Swift 2.x
Let text = "abc" Let INDEX2 = Text.startIndex.advancedBy (2)//will call succ 2 Timeslet lastchar:character = Text[index2] Now we can index!let Lastchar = text.characters[index2]//will do the samelet range:range<string.index> = Text.ra Ngeofstring ("B")!let Index:int = Text.startIndex.distanceTo (range.startindex)//will call Successor/predecessor Several times until the indices match
      • Swift 1.x
Let text = "abc" Let INDEX2 = Advance (Text.startindex, 2)//will call succ 2 Timeslet lastchar:character = Text[index2]// Now we can index!let range = text.rangeofstring ("b") Let Index:int = Distance (Text.startindex, Range.startindex)//will CA ll Succ/pred several times

[Swift] The pit of a string

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.