📜  Swift字符串

📅  最后修改于: 2021-01-11 07:43:02             🧑  作者: Mango

雨燕弦

Swift 4字符串是有序字符集合,例如“ Hello,World!”。它们由Swift 4数据类型String表示,该数据类型又表示字符类型的值的集合。

如何创建一个字符串?

的字符串可通过使用一个<字符串>或创建字符串类的实例被创建。

请参阅以下示例:

// String creation using String literal
var stringA = "Hello world!"
print( stringA )

// String creation using String instance
var stringB = String("This is JavaTpoint")
print( stringB )

//Multiple line string

let stringC = """
This is an
example of multiple line
string by JavaTpoint

"""
print(stringC)

输出:

Hello world!
This is JavaTpoint
This is an
example of multiple line
string by JavaTpoint

空字符串

要创建空字符串,可以使用空字符串字面量或创建字符串类的实例。

要检查字符串是否为空,可以使用布尔属性isEmpty。

例:

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

if stringA.isEmpty {
   print( "stringA is empty" )
} else {
   print( "stringA is not empty" )
}

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

if stringB.isEmpty {
   print( "stringB is empty" )
} else {
   print( "stringB is not empty" )
}

输出:

stringA is empty
stringB is empty

字符串操作

我们可以对Swift字符串进行很多操作。

字符串串联

+运算符用于在Swift中连接两个字符串或一个字符串和一个字符,或两个字符。

例:

let constA = "Hello "
let constB = "JavaTpoint"

var stringA = constA + constB
print( stringA )

输出:

Hello JavaTpoint

字符串插值

字符串插值用于通过混合常量,变量,字面量和表达式的值来构造新的字符串值,并将它们包括在字符串字面量。插入字符串字面量的变量和常量的值用一对括号括起来,并以反斜杠为前缀。

例:

var varA = 10
let constA = 1000
var varC:Float = 10.0

var stringA = "\(varA) times \(constA) is equal to \(varC * 1000)"
print( stringA 

输出:

10 times 1000 is equal to 10000.0

弦长

字符串4不支持length属性,但是我们可以使用全局count()函数对字符串的字符数进行计数。

例:

var varA = "Hello JavaTpoint"
print( "\(varA), string length is \((varA.count))" )

输出:

Hello JavaTpoint, string length is 16

字符串比较

==运算符用于比较两个字符串变量或常量。

例:

var varA = "Hello, JavaTpoint"
var varB = "Hello, World!"

if varA == varB {
   print( "\(varA) and \(varB) are equal" )
} else {
   print( "\(varA) and \(varB) are not equal" )
}

输出:

Hello, JavaTpoint and Hello, World! are not equal

字符串迭代

在Swift 4中,字符串是值的集合,因此我们可以使用循环对字符串进行迭代:

例:

for chars in "WelcometoJavaTpoint" {
   print(chars, terminator: " ")
}

输出:

W e l c o m e t o J a v a T p o i n t 

Unicode字符串的迭代

我们可以通过utf8和utf16属性访问Unicode字符串的UTF-8和UTF-16表示形式。

例:

var unicodeString = "JavaTpoint"

print("UTF-8 Codes: ")
for code in unicodeString.utf8 {
   print("\(code) ")
}

print("\n")

print("UTF-16 Codes: ")
for code in unicodeString.utf16 {
   print("\(code) ")
}

输出:

UTF-8 Codes: 
74 
97 
118 
97 
84 
112 
111 
105 
110 
116 


UTF-16 Codes: 
74 
97 
118 
97 
84 
112 
111 
105 
110 
116 

Swift 4字符串函数和运算符

与Swift 4中的String相关的函数和运算符的列表:

Index Functions/Operators Usage
1) isEmpty It is used to check whether a string is empty or not. It specifies a Boolean value.
2) hasPrefix(prefix: String) It is a function to check whether a given parameter string exists as a prefix of the string or not.
3) hasSuffix(suffix: String) It is a function to check whether a given parameter string exists as a suffix of the string or not.
4) toInt() It is a function to convert numeric String value into Integer.
5) count() It is a global function to count the number of Characters in a string.
6) utf8 It specifies a property to return a UTF-8 representation of a string.
7) utf16 It specifies a property to return a UTF-16 representation of a string.
8) unicodeScalars It specifies a property to return a Unicode Scalar representation of a string.
9) + It is an operator to concatenate two strings, or a string and a character, or two characters.
10) += It is an operator to append a string or character to an existing string.
11) == It is an operator to determine the equality of two strings.
12) < It is an operator to perform a lexicographical comparison to determine whether one string evaluates as less than another.
13) startIndex It is used to get the value at starting index of string.
14) endIndex It is used to get the value at ending index of string.
15) Indices It is used to access the indices one by one. i.e. all the characters of string one by one.
16) insert(“Value”, at: position) It is used to insert a value at a position.
17) remove(at: position)
removeSubrange(range)
It is used to remove a value at a position, or to remove a range of values from string.
18) reversed() It is used to return the reverse of a string.