One: String overview
Unlike NSString and nsmutablestring in Objective-c, in swift, storing string data can change, depending on whether the definition data is VAR or let,var is a variable, can change the length of a string, add, modify, Delete Each of these characters, and let is a constant, and once defined, it is no longer possible to modify its contents.
Swift's string type is a value type, and if you create a new string type of data, the value is copied when it is assigned, resulting in a copy.
II: Character overview
The string is actually used as a collection of character types, and each of its character data can be obtained by traversing each element of the string.
In Swift, a character is represented by a double quotation mark, meaning that character is interpreted as a string of length equal to 1 in swift, and the character type must contain only one character.
The character (Character) type must be represented by double quotation marks, if the compilation error is reported in single quotation marks
Let Exclamation:character = "!"
You can also use the character array to create a string
Let catcharacter: [Character] = ["C", "a", "T", "!"] Let catstring = String (catcharacter)
Three: String operations
Let string1 = "Hello" let string2 = "There" var welcome = string1 + string2 //string concatenation, provided that all elements of the + operation must be guaranteed to be not an optional variable, must be a value of
string type, cannot be
String? Optional type, optional type not able to participate in + operation
The insert operation of the string, in the form "test string \(varinstance)", must be guaranteed that varinstance is not an optional type, varinstance must have a definite value
var instruction = "Look over" instruction + = String2let Exclamation:character = "!" Welcome.append (exclamation) //Note string type and character type cannot use the + operator directly because inconsistent data types on both sides cause the compiler to error
Swift Learning Notes (grammar)--string and character types