📅  最后修改于: 2020-12-25 04:27:51             🧑  作者: Mango
Swift 4中的字符串是字符的有序集合,例如“ Hello,World!”。它们由Swift 4数据类型String表示,该数据类型又表示字符类型的值的集合。
您可以通过使用字符串字面量或创建String类的实例来创建字符串,如下所示:
// String creation using String literal
var stringA = "Hello, Swift 4!"
print( stringA )
// String creation using String instance
var stringB = String("Hello, Swift 4!")
print( stringB )
//Multiple line string
let stringC = """
Hey this is a
example of multiple Line
string by tutorialsPoint
"""
print(stringC)
编译并执行上述代码后,将产生以下结果
Hello, Swift 4!
Hello, Swift 4!
Hey this is a
example of multiple Line
string by tutorialsPoint
您可以通过使用空字符串字面量或创建String类的实例来创建空字符串,如下所示。您还可以使用布尔属性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
您可以通过将String分配给变量来指定是对其进行修改(或变异),还是使用let关键字将其分配给常量来将其保持不变,如下所示-
// stringA can be modified
var stringA = "Hello, Swift 4!"
stringA + = "--Readers--"
print( stringA )
// stringB can not be modified
let stringB = String("Hello, Swift 4!")
stringB + = "--Readers--"
print( stringB )
编译并执行上述代码后,将产生以下结果-
Playground execution failed: error: :10:1: error: 'String' is not
convertible to '@lvalue UInt8'
stringB + = "--Readers--"
字符串插值是通过将常量,变量,字面量和表达式的值包含在字符串字面量来构造新的String值的方法。
您插入字符串字面量的每个项目(变量或常量)都用一对括号括起来,并以反斜杠为前缀。这是一个简单的例子-
var varA = 20
let constA = 100
var varC:Float = 20.0
var stringA = "\(varA) times \(constA) is equal to \(varC * 100)"
print( stringA )
编译并执行上述代码后,将产生以下结果-
20 times 100 is equal to 2000.0
您可以使用+运算符来连接两个字符串或一个字符串和一个字符或两个字符。这是一个简单的例子-
let constA = "Hello,"
let constB = "World!"
var stringA = constA + constB
print( stringA )
编译并执行上述代码后,将产生以下结果-
Hello,World!
Swift 4字符串没有length属性,但是您可以使用global count()函数对字符串的字符数进行计数。这是一个简单的例子-
var varA = "Hello, Swift 4!"
print( "\(varA), length is \((varA.count))" )
编译并执行上述代码后,将产生以下结果-
Hello, Swift 4!, length is 15
您可以使用==运算符比较两个字符串变量或常量。这是一个简单的例子-
var varA = "Hello, Swift 4!"
var varB = "Hello, World!"
if varA == varB {
print( "\(varA) and \(varB) are equal" )
} else {
print( "\(varA) and \(varB) are not equal" )
}
编译并执行上述代码后,将产生以下结果-
Hello, Swift 4! and Hello, World! are not equal
字符串再次是swift 4中值的集合,因此我们可以使用循环在字符串进行迭代。 –
for chars in "ThisString" {
print(chars, terminator: " ")
}
编译并执行上述代码后,将产生以下结果-
T h i s S t r i n g
您可以通过迭代字符串的utf8和utf16属性来访问字符串的UTF-8和UTF-16表示形式,如以下示例所示-
var unicodeString = "Dog???"
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:
68
111
103
63
63
63
UTF-16 Codes:
68
111
103
63
63
63
Swift 4支持与字符串有关的各种方法和运算符-
Sr.No | Functions/Operators & Purpose |
---|---|
1 |
isEmpty A Boolean value that determines whether a string is empty or not. |
2 |
hasPrefix(prefix: String) Function to check whether a given parameter string exists as a prefix of the string or not. |
3 |
hasSuffix(suffix: String) Function to check whether a given parameter string exists as a suffix of the string or not. |
4 |
toInt() Function to convert numeric String value into Integer. |
5 |
count() Global function to count the number of Characters in a string. |
6 |
utf8 Property to return a UTF-8 representation of a string. |
7 |
utf16 Property to return a UTF-16 representation of a string. |
8 |
unicodeScalars Property to return a Unicode Scalar representation of a string. |
9 |
+ Operator to concatenate two strings, or a string and a character, or two characters. |
10 |
+= Operator to append a string or character to an existing string. |
11 |
== Operator to determine the equality of two strings. |
12 |
< Operator to perform a lexicographical comparison to determine whether one string evaluates as less than another. |
13 |
startIndex To get the value at starting index of string. |
14 |
endIndex To get the value at ending index of string. |
15 |
Indices To access the indeces one by one. i.e all the characters of string one by one. |
16 |
insert(“Value”, at: position) To insert a value at a position. |
17 |
remove(at: position) removeSubrange(range) to remove a value at a position, or to remove a range of values from string. |
18 |
reversed() returns the reverse of a string |