1. String Definitions
var s = "Aaaaaa"
The two strings are both empty and equivalent. var emptystring = "" var anotheremptystring = String ()
String literals can contain the following special characters:Escape character\0
(null character), \ \
(backslash), \ t
(Horizontal tab), \ n
(line break), \ r
(carriage return), \ "
(double quotes), \ '
(single quotation mark).
A single-byte Unicode scalar, written \xnn
in a nn
two-bit hexadecimal number.
A double-byte Unicode scalar, written \unnnn
in a nnnn
four-bit hexadecimal number.
A four-byte Unicode scalar, written \Unnnnnnnn
with nnnnnnnn
a eight-bit hexadecimal number.
2. string concatenation
string concatenation var x = "X" var y = "Y" var z = x + yvar m = z + "M" var n = "n" n + = m//use \ () to complete the interpolation of the string var result = "\ (x) ==\ (y) ==\ (z) ==\ (m) ==\ (n) "println (result) Let Plus1 = S1 + C1
3. string Traversal
var myString = "This was a string" for character in mystring{println (character)}
4. statistics of the number of characters
var myString = "Here is my string, lets count the characters!" println (Count (myString))
5. string comparison & whether the prefix is included
var check1:string = "Swift is good!" var check2:string = "Swift is good!" Whether the strings are equal if Check1 = = Check2 {println ("Check1 = Check2")} else {println ("Check1! = Check2")}//contains the prefix var PreCheck = "Sw IFT "If Check1.hasprefix (PreCheck) {println (" include prefix ")}//contains suffix var sufcheck =" good! " If Check1.hassuffix (Sufcheck) {println ("contains suffix")}
6. Case Conversion
Let myString = "Wait for a moment, please." Let newstring = Mystring.uppercasestringlet myString = "Wait a moment, please." Let newstring = mystring.lowercasestring
7. whether the string contains other strings
var myString = "This is a string test" if mystring.rangeofstring ("test") {println ("exists")}
8. string Interception
The identity bit of the Intercept string starts at 1 count let myString = "Abcdefghi"//1. The remaining string is truncated from the target identity bit to the next character let mysubstring = (Mystring.substringfromindex (2 )) println (mysubstring) Let mySubstring2 = (Mystring.substringtoindex (4)) println (mySubstring2)
9. string to go to space
var myString = "Let's trim the whitespace" var newstring = Mystring.stringbytrimmingcharactersinset (nscharacterset . Whitespacecharacterset ())
dividing a string into a string array
Single delimiter: only one separator var myString = "Berlin, Paris, New York, San Francisco"//1. Implicitly defined string array var MyArray = Mystring.componentssepar Atedbystring (",")//2. Display definition string array var myarray2:string[] = mystring.componentsseparatedbystring (",") for EM in MyArray2 {PR Int (EM)}//Multi-separator: There are 2 or two separators var myString2 = "One-two-three-1 2 3" var array2:string[] = Mystring2.componentsseparatedbyc Haractersinset (Nscharacterset (charactersinstring: "-")//["one", "twin", "three", "1", "2", "3"]
11. String conversion to NSData
Let myString = ' String to encode ' let string:nsstring = mystringlet data = string.datausingencoding (nsutf8stringencoding)
String common operations in Swift