Talking about the common methods of Swift's string

Source: Internet
Author: User
Tags string methods

Below, let's enumerate some of the commonly used swift string methods

First, let's define some string variables and constants.

" Hello World " // The value is Hello you string " Hai " "! "

String connection, in fact, the string of connection words, directly with var i:string = "abc" + "Def" can be done, incredibly we talk about the method of string,

Let's try it with append.

// The value is Hello world! // appendcontentsof () is the connection of a string welcome.appendcontentsof ("CCC"//  The value is Hello world! C c c

Turn the first letter into uppercase

// The value is Hello world

Displays the length of the string

Str1.endindex//value is one//     value of one

We'll define a string again.

var a:string =  "asdf"

The first subscript of a string

A.startindex

Inserts a character in the first item of a string

" C " ,  atindex:a.startindex)    //casdf

Displays the first character of a string

Here, it is necessary to explain, here the brackets are not allowed to fill in the numbers, a[0] is wrong, A[a.startindex] is correct, pay attention!!

Get the next character. Successor () and the previous character. predecessor ()

/ Get the next character. Successor () A[a.startindex.successor ()]    //a//  Gets the last character. predecessor ()A[a.startindex.successor (). predecessor ()]  //C

If you want to get a value from a string, you can add a Advancedby () on top of it.

A[a.startindex.advancedby (2)]  //s

Intercepts a string that specifies the subscript to the last character

A.substringfromindex (A.startindex.advancedby (2))  //s D F

Intercepts a string that specifies the subscript to the first character

A.substringtoindex (A.startindex.advancedby (3))  //cas

Determines whether the first character is a character, and returns a true if correct

A.hasprefix ("C")  //true

Determines whether the last character is a character, and returns a true if correct

A.hassuffix ("F")  //true

To cut a string into an array of strings by specifying a character

A.componentsseparatedbystring ("a")  //["C", "SDF"]

Well, that's the way we use strings,

Talking about the common methods of Swift's 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.