SoFunction
Updated on 2025-04-11

Analysis of basic concepts related to strings in Swift language

Swift characters are a single string literal and are character data types. Here is a simple example,useTwo character constants:

Copy the codeThe code is 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 multiple characters to a variable or constant of a character type, Swift will not allow it. Try typing the following example to Swift Playground, you will get an error before compiling.

Copy the codeThe code is as follows:

import Cocoa

// Following is wrong in Swift
let char: Character = "AB"

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


Empty character variable
It is impossible to create an empty character variable or constant, which will have a null value. The following syntax is impossible:
Copy the codeThe code is 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)")


Access characters from string
When discussing Swift strings, the string represents a collection of character values ​​in the specified order. Therefore, we can loop through a for-in loop from a given string to access a single character:
Copy the codeThe code is 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

useCharacter connection string
The following example demonstrates how Swift characters are concatenated with strings.

Copy the codeThe code is as follows:

import Cocoa

var varA:String = "Hello "
let varB:Character = "G"

( 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.

Create a string
Can be passeduseA string literal or create an instance of the String class to create a string, as follows:

Copy the codeThe code is 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
Can be passeduseAn empty string or create an instance of the String class to create an empty string as shown below. You can also check whether a string is empty.useBoolean property isEmpty.

Copy the codeThe code is as follows:

import Cocoa

// Empty string creation using String literal
var stringA = ""

if {
   println( "stringA is empty" )
} else {
   println( "stringA is not empty" )
}

// Empty string creation using String instance
let stringB = String()

if {
   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 byuseThe let keyword is assigned as a constant, as shown in the figure below:

Copy the codeThe code is 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' is not convertible to '@lvalue UInt8'
stringB + = "--Readers--"

String interpolation
A string conversion symbol is a constant, variable, text, and expression constructed by including values ​​within a string literal to combine a new string value.

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

Copy the codeThe code is as follows:

import Cocoa

var varA   = 20
let constA = 100
var varC:Float = 20.0

var stringA = "\(varA) times \(constA) is equal to \(varC * 100)"
println( stringA )


When the above code is compiled and executed, it produces the following results:
20 times 100 is equal to 2000.0

String connection
CanuseThe + operator is used to concatenate two strings or strings and characters, or two characters. Here is a simple example:

Copy the codeThe code is 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
Strings in Swift do not have a length attribute, but canuseThe global count() function calculates the number of characters in a string. Here is a simple example:

Copy the codeThe code is 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 comparison
Canuse== Operators compare two string variables or constants. Here is a simple example:

Copy the codeThe code is 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 strings
You can access the UTF-8 and UTF-16 representations of strings by traversing the UTF8 and UTF16 properties, as in the following example:

Copy the codeThe code is 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: 
68 111 103 226 128 188 240 159 144 182 
UTF-16 Codes: 
68 111 103 8252 55357 56374