This article focuses on the new features of strings in Swift, as well as common knowledge points

Strings support implicit multi-line assignment “”” “””

The string can be wrapped directly, using a pair of three double quotes to include it. Note that such a form must define the string from the second line

//incorrect
let string0 = """this is me
"""

//correct
let string0 = """
this is me
"""

//"this is me"

Copy the code

If you want to wrap a line, just change the line. If you want to wrap one more line, just leave a blank line

//"this is me\n"
let string1 = """
    this is me
    
    """
//"\nthis is me\n"

let string2 = """

this is me

"""
Copy the code

If you don’t want to wrap a line, you can also use \ as a continuation character.

let string4 = """
    
    this is me, ok, this is next \
    line
    """
Copy the code

Another interesting aspect of the alignment logic here is that the whitespace on the other lines of the second “”” aligned symbol is ignored. What does that mean? Let’s take a look at the documentation

There is no second “” in the document. This was discovered after I accidentally wrote the code alignment error. Here is an example:

"\nthis is me,\nok" let string = """ this is me, "\n this is me,\n ok" let string1 = """ this is me, ok" "// Whereas in string1, This and OK are preceded by white space, so I assume that the white space here is ignored based on the second """Copy the code

Special string

In strings, there are many special characters, and Swift also uses/to represent special characters, such as \0(empty character), \(backslash), \ T (horizontal TAB), \n(newline), \r(carriage return), “(double quotation marks), ‘(single quotation marks).

You can also use the Unicode encoding directly as \u{n}(u is lowercase), where n is any one to eight hexadecimal number available in Unicode bitcode.

Because of the “”” newline, there is no need to identify the double quotation mark, as in:

//"this is "me","
let string = """
    this is "me",
    """
Copy the code

What if you want to use special strings? For example, if I want the string to contain \n, I can use ## to contain the string, in which special characters are not escaped

let string = #"\n"#

let string1 = "\\n"
Copy the code

Using strings

Initialize string

I won’t go into details here, just use simple initialization

Var emptyString = "// emptyString literal var anotherEmptyString = String() // initialization methodCopy the code

Modify string

In OC, we use NSString and NSMutableString to indicate whether a string can be modified, whereas in Swift, we use let and var directly

Var variableString = "Horse" variableString += "and carriage" // variableString is now a "Horse and carriage" let ConstantString = "Highlander" constantString += "and another Highlander" // This reports a compile-time error - A constant string cannot be modified.Copy the code

However, when concatenating strings, the painful use of %@ marks in OC is no longer necessary. Instead, variables can be directly embedded, and variables can be included with (), such as

Multiplier = 1 let message = "multipliers "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5) 7.5"Copy the code

You can even perform some function calculations directly in the string, such as

Print (#"6 times 7 is \#(6 * 7)."#)Copy the code

Note: In Swift, String is a value type. When passed as a parameter to a function, it is not modified. It is only used by copy

For example, in OC, if a string is passed as an argument, we can modify the string in the function

NSString a = "string"; [self changeString:a]; NSLog(a); //new string - (void)changeString:(NSString *)string { string = "new string"; }Copy the code

In Swift, you cannot modify parameters directly in a function unless you use a keyword such as inout

var b = "b"

//error
addString(string: b)
func addString(string: String) -> Void {
        string += "aaa"
    }



//correct
addString(string: &b)
func addString(string:inout String) -> Void {
        string += "aaa"
        
    }

Copy the code

Traversal string

for character in "Dog! 🐶" {print(character)} // D // o // g //! / / 🐶Copy the code

Note that strings in Swift include not only the above content, but also things like string indexes, substrings, Unicode, etc. I’ve just picked up some common knowledge to summarize, and looked directly at the tutorial strings and characters for those interested