9.24. Swift string

发布时间 : 2025-10-25 13:33:51 UTC      

Page Views: 10 views

A Swift string is a collection of characters. For example, “Hello, World!”a collection of values of such an ordered character type, whose data type is String .

9.24.1. Create a string #

You can use the literal amount of a string or String class to create a string:

import Cocoa // Using string literals var stringA = "Hello, World!" print( stringA ) // String instantiate var stringB = String("Hello, World!") print( stringB ) 

The output of the above program execution is as follows:

Hello, World! Hello, World! 

9.24.2. Empty string #

You can use empty string literals to assign values to variables or initialize a String class to initially value an empty string. We can use string properties isEmpty to determine whether the string is empty:

import Cocoa // Create an empty string using string literals var stringA = "" if stringA.isEmpty { print( "StringA is empty" ) } else { print( "StringA is not empty" ) } // Instantiating the String class to create an empty string let stringB = String() if stringB.isEmpty { print( "stringB is empty" ) } else { print( "stringB is not empty" ) } 

The output of the above program execution is as follows:

stringA is empty stringB is empty 

9.24.3. String constant #

You can assign a string to a variable or constant. Variables are modifiable and constants are immutable.

import Cocoa // stringA can be modified var stringA = "Rookie Tutorial:" stringA += "http://www.runoob.com" print( stringA ) // stringB can not be modified let stringB = String("Rookie Tutorial:") stringB += "http://www.runoob.com" print( stringB ) 

The output of the above program will report an error, because stringB being a constant cannot be modified:

error: left side of mutating operator isn't mutable: 'stringB' is a 'let' constant stringB += "http://www.runoob.com" 

9.24.4. Insert a value into a string #

String interpolation is a way to build new strings that can include constants, variables, literals, and expressions. Each item of the literal amount of the string you insert is in parentheses prefixed with a backslash:

import Cocoa var varA = 20 let constA = 100 var varC:Float = 20.0 var stringA = "\(varA) by \(constA) equal to \(varC * 100)" print( stringA ) 

The output of the above program execution is as follows:

20 by 100 equal to 2000.0 

9.24.5. String concatenation #

Strings can be concatenated with the + sign. Examples are as follows:

import Cocoa let constA = "Rookie Tutorial:" let constB = "http://www.runoob.com" var stringA = constA + constB print( stringA ) 

The output of the above program execution is as follows:

Rookie Tutorial:http://www.runoob.com 

9.24.6. String length #

String length using String.count property, an example is as follows:

The Swift 3 version uses the String.characters.count

import Cocoa var varA = "www.runoob.com" print( "\(varA), the length is \(varA.count)" ) 

The output of the above program execution is as follows:

www.runoob.com, the length is 14 

9.24.7. String comparison #

You can use == to compare whether two strings are equal:

import Cocoa var varA = "Hello, Swift!" var varB = "Hello, World!" if varA == varB { print( "\(varA) and \(varB) is equal" ) } else { print( "\(varA) and \(varB) is not equal" ) } 

The output of the above program execution is as follows:

Hello, Swift! and Hello, World! is unequal 

9.24.8. Unicode String #

Unicode is an international standard for text encoding, Swift String the type is based on Unicode it was built. You can iterate through the string. UTF-8 and UTF-16 an example of the code is as follows:

import Cocoa var unicodeString = "Rookie Tutorial" print("UTF-8 code: ") for code in unicodeString.utf8 { print("\(code) ") } print("\n") print("UTF-16 code: ") for code in unicodeString.utf16 { print("\(code) ") } 

The output of the above program execution is as follows:

UTF-8 code: 232 143 156 233 184 159 230 149 153 231 168 139 UTF-16 code: 33756 40479 25945 31243 

9.24.9. String functions and operators #

Swift supports the following string functions and operators:

Serial number

Function / operator & description

1

isEmpty

Determines whether the string is empty and returns a Boolean value

2

hasPrefix(prefix: String)

Check whether the string has a specific prefix

3

hasSuffix(suffix: String)

Check whether the string has a specific suffix.

4

Int(String)

Convert string numbers to integers. Example:

let myString: String = "256" let myInt: Int? = Int(myString) 

5

String.count

The Swift 3 version uses String.characters.count

Calculate the length of a string

6

utf8

You can access the UTF-8 encoding of String by traversing its utf8 property

7

utf16

You can access the utf16 encoding of String by traversing its utf8 property

8

unicodeScalars

You can access its Unicode scalar encoding by iterating through the unicodeScalars property of the string value.

9

+

Concatenate two strings and return a new string

10

+=

Concatenate the strings on both sides of the operator and assign the new string to the operator variable on the left

11

==

Judge whether two strings are equal

12

<

Compare two strings and compare the letters of the two strings one by one.

13

!=

Compare whether two strings are not equal.

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。