Swift Learning Note (4): string

Source: Internet
Author: User

Directory:

    • Initialization
    • Common methods or properties
    • String index
Initialization

Create an empty string as the initial value:

""                // empty string literal var anotheremptystring = string ()   //  initialization method, two strings are empty and equivalent. 

Common methods or properties
1var empty = Emptystring.isempty//determines whether a string is empty2var welcome ="string1"+ string2//Use + or + = to stitch strings3Welcome.append ("character")//append a character to the end of a string using append ()4 5 //string interpolation using \ (variable)6Let multiplier =37Let message ="\ (multiplier) times 2.5 is \ (Double (multiplier) * 2.5)" 8 9 //string comparison using = = or! =Ten ifQuotation = =samequotation { OnePrint"these strings is considered equal") A } -  - //use Hasprefix () and Hassuffix () to determine if prefixes or suffixes the ifScene.hasprefix ("Act 1") { -Printthe stringhas the prefix of Act 1 ") -}

Attention:

? You cannot add a string or character to a character variable that already exists because the character variable can contain only one character.
An expression written in parentheses in an interpolated string cannot contain a non-escaped backslash (\) and cannot contain a carriage return or line feed.

String index

It can be accessed and modified through string subscripts or indexed properties and methods, and String.index corresponds to the character position in the string.

1Samplestring.startindex.//gets the index of the first character2Samplestring.endindex//gets the index of the last character3 4Let greeting ="Guten tag!"5Greeting[greeting.startindex]//G use subscript to get characters6Greeting[greeting.index (Before:greeting.endIndex)]// !7Greeting[greeting.index (After:greeting.startIndex)]//u8 9Let index = Greeting.index (Greeting.startindex, Offsetby:7)TenGreeting[index]//a One  A /*  - Greeting[greeting.endindex]//Error index out of bounds - Greeting.index (After:endindex)//Error index out of bounds the */ -  - //Use the Characters.indices property to create a range containing all indexes to traverse a single character in a string -  forIndexinchgreeting.characters.indices { +Print"\ (Greeting[index])", Terminator:"")//output "G u t e n T a G!" - } +  Avar welcome ="Hello" atWelcome.insert ("!", At:welcome.endIndex)//welcome equals "Hello!" -Welcome.remove (At:welcome.index (Before:welcome.endIndex))//welcome equals "Hello"

Attention:

? The extensible character clouds set can form one or more Unicode scalars. This means that different characters and different representations of the same characters may require a different amount of memory space to store. Therefore, the characters in Swift do not necessarily occupy the same memory space in a string. Therefore, when you do not get the range of the string extensible character clouds set, the number of characters in the string cannot be computed, you must traverse the entire Unicode scalar of the string to determine the number of characters.

Statement: This series of content is from the network or electronic books, only to do learning summary!

Swift Learning Note (4): string

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.