swift string String usage


swift string String usage

1, the statement

var str = "Hello, playground"
//var str: String = "Hello, playground"


//  An empty string
let emptyString = ""
let emptyString2 = String()


//  use String() Initializing string
let str2 = String("Hello, swift")

2. Determine if the string is empty

//  Determine empty string
str.isEmpty //false
emptyString.isEmpty //true

3. String concatenation

// String The glue
let mark = "!!!"
str + mark //Hello, playground!!!
str //Hello, playground


//  constant String And variables String
str += mark
str //Hello, playground!!!

//str2 += mark //let  complains

4. String interpolation

//  String interpolation
let name = "lxy"
let age = 18
let height = 1.78
let s = "My name is \(name), I'm \(age) years old. I'm \(height) meters tall."
print(s) //My name is lxy, I'm 18 years old. I'm 1.78 meters tall.

5. Special characters

//  Special characters
let s = "\\" =>\
let s = "\"" =>"
let s = "\n" => A newline

Thank you for reading, I hope to help you, thank you for your support to this site!