Swift创建和初始化字符串

2021-05-30  本文已影响0人  一个栗

初始化空串

var emptyString = ""
var anotherEmptyString = String()

if emptyString.isEmpty {
    print("Nothing to see here")
}

字面量

let str = "Hello, playground"

多行字面量

let softWrappedQuotation = """
The white Rabbit put on his spectacles. "where shall I began, please your Majesty ?" he asked.

"Began at the baginning," the King said gravely, "and go on till you come to the end; then stop."
"""
print(softWrappedQuotation)

打印结果如下:
The white Rabbit put on his spectacles. "where shall I began, please your Majesty ?" he asked.

"Began at the baginning," the King said gravely, "and go on till you come to the end; then stop."

使用反斜杠

let softWrappedQuotation = """
The white Rabbit put on his spectacles. "where shall I began,\
please your Majesty ?" he asked.

"Began at the baginning," the King said gravely, "and go on \
till you come to the end; then stop."
"""
print(softWrappedQuotation)

打印结果如下:
The white Rabbit put on his spectacles. "where shall I began,please your Majesty ?" he asked.

"Began at the baginning," the King said gravely, "and go on till you come to the end; then stop."

多行字面量

字符串里的特殊字符串

扩展字符串分隔符(Raw String)

Swift 5 新增的特性

上一篇 下一篇

猜你喜欢

热点阅读