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