swift/81195.html">swift 字符串String的使用方法
1、声明
var str = "Hello, playground"//var str: String = "Hello, playground"// 空字符串let emptyString = ""let emptyString2 = String()// 使用String()初始化字符串let str2 = String("Hello, swift")
2、判断字符串是否为空
// 判断空字符串str.isEmpty //falseemptyString.isEmpty //true
3、字符串拼接
// String的粘合let mark = "!!!"str + mark //Hello, playground!!!str //Hello, playground// 常量String和变量Stringstr += mark str //Hello, playground!!!//str2 += mark //let 会报错
4、字符串插值
// 字符串插值let name = "lxy"let age = 18let height = 1.78let 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、特殊字符
// 特殊字符let s = "//" =>/let s = "/"" =>"let s = "/n" =>换行
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
新闻热点
疑难解答