String whether Object C or Swift or Java and other languages, can be said to be the focus, although it is not difficult, but the use of the place is very many, almost all of the string processing, this article we will talk about strings:

Create a mutable string:

var stringA = "Hello, World!"
Copy the code

Create an immutable string:

let stringB = "Hello, World!"
Copy the code

Here we introduce a noun: literals. The two above are strings created from literals. Create an initialization string:

var stringC = String("Hello, World!" )Copy the code

Create an empty string:

Var stringD = "" //"" is not the same thing as nil, but they both belong to empty, IsEmpty {print("stringD isEmpty ")} else {print("stringD is not empty ")} // instantiate the String class to create an empty String let stringE = String() if stringe. isEmpty {print("stringE isEmpty ")} else {print("stringE is not empty ")}Copy the code

Var = variable; let = immutable; let = immutable; var = variable;

String insertion:

Var varA = 1 let constA = 2 var varC:Float = 3 var varC:Float = 3 To insert a variable or constant into a string, it must also be converted to a string, \ () means to convert the argument in parentheses to a string, Var stringA = "\(varA) + \(constA) = \(varC)" print(stringA)Copy the code

String concatenation:

Let strA = "Hello" let strB = "World" Append var strC = strA + strB //var strC = stra. append(strB) print(strC)Copy the code

String length:

Var STR = "Hello World" gets the STR length: str.characters.countCopy the code

String comparison: use “= =” to indicate equality, “! Object-C = isEqual

Check prefixes and suffixes:

HasPrefix (prefix: String) Checks whether the String has a specific prefix hasSuffix(suffix: String) Checks whether the String has a specific suffix. Var firstStr:String = "Hello,World!" if firstStr.hasPrefix("Hello") { }Copy the code

Type conversion:

let string1: String = "100"
let stringForInt: Int? = Int(string1)
Copy the code

One last word:

Var hello = "hello world" var can use Chinese characters = "z and not wrong? "print(hello) print(can use Chinese characters) yes, you read correctly, Swift supports Chinese namesCopy the code

In the end, you may see that there are still some methods not listed, yes, there are too many, the usage is similar to OC, the blogger went to look at several methods, last year’s method, in the new Xcode is an error, has been deprecated, Swift stability still needs to improve. I don’t know if it will be replaced again, so I won’t write it.