在Swift中,處理字符串有多種方法,包括字符串的創建、修改、拼接、截取等。
let str1 = "Hello, Swift!"
let str2 = """
This is a multi-line string.
It can have multiple lines.
"""
let name = "Alice"
let greeting = "Hello, " + name
let age = 25
let message = "I am \(age) years old."
let str = "Hello"
let length = str.count
print(length) // 輸出 5
let str = "Hello, Swift!"
let range = str.index(str.startIndex, offsetBy: 7)..<str.index(str.endIndex, offsetBy: -1)
let subStr = str[range]
print(subStr) // 輸出 Swift
let str = "Hello, Swift!"
let upperStr = str.uppercased()
let lowerStr = str.lowercased()
print(upperStr) // 輸出 HELLO, SWIFT!
print(lowerStr) // 輸出 hello, swift!
以上是一些常用的字符串處理方法,在實際開發中可以根據需求選擇合適的方法來處理字符串。