String common operations in Swift

Source: Internet
Author: User
Tags scalar

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 character),\"(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 divider var mystring =  "berlin, paris, new  York, san francisco "//1. Implicitly defines a string array var myarray = mystring.componentsseparatedbystring (" , ")//2. Displays a definition string array var myarray2 : string[] = mystring.componentsseparatedbystring (",   ") For em in myarray2 {  print (EM)    }//multiple delimiters: 2 or more than two delimiter Var  myString2 =  "One-two-three-1 2 3" var array2:string[] =  Mystring2.componentsseparatedbycharactersinset (nscharacterset  (charactersinstring:  "- "))//[" One ", " and ", " 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

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.