SoFunction
Updated on 2025-04-07

How to use swift string String

How to use swift string String

1. Statement

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


// Empty stringlet emptyString = ""
let emptyString2 = String()


// Initialize strings with String()let str2 = String("Hello, swift")

2. Determine whether the string is empty

//Judge empty string //false
 //true

3. String stitching

// String's bondinglet mark = "!!!"
str + mark //Hello, playground!!!
str //Hello, playground


// Constant String and variable Stringstr += mark 
str //Hello, playground!!!

//str2 += mark //let will report an error

4. String interpolation

// String interpolationlet 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 characterslet s = "\\" =>\
let s = "\"" =>"
let s = "\n" =>Line break

Thank you for reading, I hope it can help you. Thank you for your support for this site!