The basic concept of string correlation in Swift language _swift

Source: Internet
Author: User

Swift's character is a single string literal and is a character data type. The following is a simple example of using two characters of constants quantity:

Copy Code code as follows:

Import Cocoa

Let Char1:character = "A"
Let Char2:character = "B"

println ("Value of char1 \ (CHAR1)")
println ("Value of CHAR2 \ (CHAR2)")


When the above code is compiled and executed, it produces the following results:

Value of Char1 A
value of Char2 B

If you try to store a variable or constant of more than one character to a character type, Swift will not allow it. Try typing the following example into Swift playground, and you'll get an error before compiling.

Copy Code code as follows:

Import Cocoa

Following is wrong in Swift
Let Char:character = "AB"

println ("Value of char \ (char)")


NULL character variable
It is not possible to create an empty character variable or constant, which will have a null value. The following syntax is not possible:
Copy Code code as follows:

Import Cocoa

Following is wrong in Swift
Let Char1:character = ""
var char2:character = ""

println ("Value of char1 \ (CHAR1)")
println ("Value of CHAR2 \ (CHAR2)")


accessing characters from strings
When discussing Swift's string, the string represents a collection of character values in the specified order. Therefore, we can iterate through a string to access a single character from a given string through a for-in loop:
Copy Code code as follows:

Import Cocoa

For ch in "Hello" {
println (CH)
}


When the above code is compiled and executed, it produces the following results:

H
e
l
l
o

using character connection strings
The following example shows how Swift characters are connected to strings.

Copy Code code as follows:

Import Cocoa

var vara:string = "Hello"
Let Varb:character = "G"

Vara.append (Varb)

println ("Value of Varc = \ (VarA)")


When the above code is compiled and executed, it produces the following results:

Value of Varc Hello G

Swift strings are ordered collections of characters, such as "Hello, world!", which are represented by Swift's string data type, which also represents a collection of character-type values.

Creating a String
you can create a string by using a string literal or by creating an instance of the string class, as follows:

Copy Code code as follows:

Import Cocoa

string creation using String literal
var Stringa = "Hello, swift!"
println (Stringa)

String creation using String instance
var stringb = String ("Hello, swift!")
println (STRINGB)


When the above code is compiled and executed, it produces the following results:

Hello, swift!
. Hello, swift!.

Empty string
You can create an empty string by using an empty string or by creating an instance of the string class, as shown below. You can also check whether a string is empty and use the Boolean property IsEmpty.

Copy Code code as follows:

Import Cocoa

Empty string creation using string literal
var Stringa = ""

If Stringa.isempty {
println ("Stringa is Empty")
} else {
println ("Stringa is not empty")
}

Empty string creation using string instance
Let STRINGB = String ()

If Stringb.isempty {
println ("StringB is Empty")
} else {
println ("StringB is not empty")
}


When the above code is compiled and executed, it produces the following results:

Stringa is empty
stringb is empty

String constants
You can specify whether a string can be modified (or mutated) by assigning it to a variable, or by assigning it to a constant by using the Let keyword, as shown in the following illustration:

Copy Code code as follows:

Import Cocoa

Stringa can be modified
var Stringa = "Hello, swift!"
Stringa + = "--readers--"
println (Stringa)

STRINGB can not be modified
Let STRINGB = String ("Hello, swift!")
STRINGB + = "--readers--"
println (STRINGB)


When the above code is compiled and executed, it produces the following results:

Playground execution Failed:error: <expr>:10:1: Error: ' String ' isn't convertible to ' @lvalue UInt8 ' stringb
+ = "--readers--"

string interpolation
A string conversion symbol is a combination of constants, variables, literals, and expressions that are constructed by including values in string literals to combine a new string value.

Each item (variable or constant) inserted into the string is wrapped within a pair of parentheses, with a backslash prefix. Here's a simple example:

Copy Code code as follows:

Import Cocoa

var VarA = 20
Let Consta = 100
var varc:float = 20.0

var Stringa = "\ (VarA) times \ (Consta) are equal to \ (VARC * 100)"
println (Stringa)


When the above code is compiled and executed, it produces the following results:

Equal to 2000.0

string concatenation
you can use the + operator to connect two strings or strings and characters, or two characters. The following is a simple example:

Copy Code code as follows:

Import Cocoa

Let Consta = "Hello,"
Let Constb = "world!"

var Stringa = Consta + constb

println (Stringa)


When the above code is compiled and executed, it produces the following results:

hello,world!

String length
the string in Swift has no length property, but you can use the global count () function to calculate the number of characters in the string. The following is a simple example:

Copy Code code as follows:

Import Cocoa

var VarA = "Hello, swift!"

println ("\ (VarA), length is \ (count (VarA))")


When the above code is compiled and executed, it produces the following results:

Hello, swift!, length is 13

String comparisons
you can use the = = operator to compare two or two string variables or constants. The following is a simple example:

Copy Code code as follows:

Import Cocoa

var VarA = "Hello, swift!"
var Varb = "Hello, world!"

if VarA = = Varb {
println ("\ (VarA) and \ (Varb) are equal")
} else {
println ("\ (VarA) and \ (Varb) are not equal")
}


When the above code is compiled and executed, it produces the following results:

Hello, swift!. and Hello, world! are not equal

Unicode string
can be represented by traversing the UTF-8 and UTF-16 of the strings in the UTF8 and UTF16 properties, as in the following example:

Copy Code code as follows:

Import Cocoa

var unicodestring = "Dog‼"

println ("UTF-8 Codes:")
For code in Unicodestring.utf8 {
Print ("\ (code)")
}

Print ("\ n")

println ("UTF-16 Codes:")
For code in UNICODESTRING.UTF16 {
Print ("\ (code)")
}


When the above code is compiled and executed, it produces the following results:

UTF-8 codes: 
128 188, 159 144 
, UTF-16: 
68 111 103 8252 55357 56374

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.