Home>

Swift's character is a single string literal and is a character data type.Here is a simple example,Two character constants are used:

import cocoa
</p>
<p>
let char1:character="a"
let char2:character="b"
</p>
<p>
println ("value of char1 \ (char1)")
println ("value of char2 \ (char2)")

When the above code is compiled and executed,It produced the following results:

value of char1 a
value of char2 b

If you try to store multiple characters into a variable or constant of character type,Then swift will not allow it.Try typing the following example into the swift playground. Before compiling,You will already get an error.

import cocoa
</p>
<p>
//following is wrong in swift
let char:character="ab"
</p>
<p>
println ("value of char \ (char)")

Null character variableIt is not possible to create an empty character variable or constant,This will have a null value.The following syntax is not possible:

import cocoa
</p>
<p>
//following is wrong in swift
let char1:character=""
var char2:character=""
</p>
<p>
println ("value of char1 \ (char1)")
println ("value of char2 \ (char2)")

Accessing characters from a stringWhen discussing swift strings,A collection of character values ​​represented by a string in a specified order.Therefore, we can iterate through a string from a given string to access a single character:

import cocoa
</p>
<p>
for ch in "hello" {
 println (ch)
}

When the above code is compiled and executed,It produced the following results:

h
e
l
l
o

Concatenate strings using charactersThe following example demonstrates how swift characters are concatenated with strings.

import cocoa
</p>
<p>
var vara:string="hello"
let varb:character="g"
</p>
<p>
vara.append (varb)
</p>
<p>
println ("value of varc=\ (vara)")

When the above code is compiled and executed,It produced the following results:

value of varc hello g

swift strings are an ordered collection of characters,Like "hello, world!", They are represented by swift's string data type,This also represents a collection of character type values.

Create stringYou can create a string by using a string literal or by creating an instance of the string class,as follows:

import cocoa
</p>
<p>
//string creation using string literal
var stringa="hello, swift!"
println (stringa)
</p>
<p>
//string creation using string instance
var stringb=string ("hello, swift!")
println (stringb)

When the above code is compiled and executed,It produced the following results:

hello, swift!
hello, swift!

Empty stringYou can create an empty string by using an empty string or by creating an instance of the string class,As follows.You can also check if a string is empty,Use the boolean attribute isempty.

import cocoa
</p>
<p>
//empty string creation using string literal
var stringa=""
</p>
<p>
if stringa.isempty {
 println ("stringa is empty")
} else {
 println ("stringa is not empty")
}
</p>
<p>
//empty string creation using string instance
let stringb=string ()
</p>
<p>
if stringb.isempty {
 println ("stringb is empty")
} else {
 println ("stringb is not empty")
}

When the above code is compiled and executed,It produced the following results:

stringa is empty
stringb is empty

String constantYou can specify whether a string can be modified (or mutated), by assigning it to a variable,Or by assigning it as a constant using the let keyword,As shown below:

import cocoa
</p>
<p>
//stringa can be modified
var stringa="hello, swift!"
stringa +="--readers--"
println (stringa)
</p>
<p>
//stringb can not be modified
let stringb=string ("hello, swift!")
stringb +="--readers--"
println (stringb)

When the above code is compiled and executed,It produced the following results:

playground execution failed:error:<expr>:10:1:error:"string" is not convertible to "@lvalue uint8"
stringb +="--readers--"

String interpolationA string conversion symbol is a constant constructed by including values ​​within a string literal,Variables, text, and expressions to combine a new string value.

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

import cocoa
</p>
<p>
var vara=20
let consta=100
var varc:float=20.0
</p>
<p>
var stringa="\ (vara) times \ (consta) is equal to \ (varc * 100)"
println (stringa)

When the above code is compiled and executed,It produced the following results:

20 times 100 is equal to 2000.0

String concatenationYou can use the + operator to concatenate two strings or strings and characters,Or two characters.Here is a simple example:

import cocoa
</p>
<p>
let consta="hello,"
let constb="world!"
</p>
<p>
var stringa=consta + constb
</p>
<p>
println (stringa)

When the above code is compiled and executed,It produced the following results:

hello, world!

String lengthStrings in swift do not have a length property, but you can use the global count () function to count the number of characters in a string.Here is a simple example:

import cocoa
</p>
<p>
var vara="hello, swift!"
</p>
<p>
println ("\ (vara), length is \ (count (vara))")

When the above code is compiled and executed,It produced the following results:

hello, swift !, length is 13

String comparisonYou can use the == operator to compare two string variables or constants.Here is a simple example:

import cocoa
</p>
<p>
var vara="hello, swift!"
var varb="hello, world!"
</p>
<p>
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 produced the following results:

hello, swift! and hello, world! are not equal

unicode stringYou can access the utf-8 and utf-16 representations of the string by iterating over the utf8 and utf16 attributes, as in the following example:

import cocoa
</p>
<p>
var unicodestring="dog‼"
</p>
<p>
println ("utf-8 codes:")
for code in unicodestring.utf8 {
 print ("\ (code)")
}
</p>
<p>
print ("\ n")
</p>
<p>
println ("utf-16 codes:")
for code in unicodestring.utf16 {
 print ("\ (code)")
}

When the above code is compiled and executed,It produced the following results:

utf-8 codes:
68 111 103 226 128 188 240 159 144 182
utf-16 codes:
68 111 103 8252 55357 56374
  • Previous sqlserver rounding using round function and cast and convert functions
  • Next Java implementation of timing task based on thread pool and reflection mechanism