美文网首页
Swift 字符串

Swift 字符串

作者: 洱舟 | 来源:发表于2018-11-14 12:30 被阅读13次

Swift 字符串

引用

字符串是值类型- 如果创建新String值,则在将String值传递给函数或方法时,或者将其赋值给常量或变量时,将复制该值.

一、字符串基础

1、字符串初始化
let emptyString = "hello world"
let anthoerEmptyString = String()
print("emptyString = \(emptyString) - anthoerEmptyString = \(anthoerEmptyString)")
//emptyString = hello world - anthoerEmptyString =
2、多行字符串文字
let quotation = """
The White Rabbit put on his spectacles.  "Where shall I begin,
please your Majesty?" he asked.
        
"Begin at the beginning," the King said gravely, "and go on
till you come to the end; then stop."
"""
3、字符串是否为空
if emptyString.isEmpty {
    print("Nothing to see here")
}
4、字符串遍历
for character in emptyString {
    print(character)
}
5、字符串的可变性
var variableString = "Horse"
variableString += " and carriage"
print("variableString = \(variableString)")
//variableString = Horse and carriage

二、访问和修改字符串

引用

//转义的特殊字符\0(空字符),\(反斜杠),\t(水平制表符),\n(换行符),\r(回车符),\"(双引号)和\'(单引号)

//String.Index它对应Character于字符串中每个值的位置,before之前,after之后

let greeting = "Guten Tag!"
print("startIndex:\(greeting[greeting.startIndex])")
//startIndex:G

let index1 = greeting.index(before: greeting.endIndex)
print("endIndex:\(greeting[index1])")
//endIndex:!

let index2 = greeting.index(after: greeting.startIndex)
print("Index1:\(greeting[index2])")
//Index1:u

let index3 = greeting.index(greeting.startIndex, offsetBy: 7)
print("index2: \(greeting[index3])")
//index2: a
1、字符串查找
let searchStr = "hello world"
guard searchStr.range(of: "world") != nil else {
    print("没有找到字符串")
    return
}
2、截取子串
let subStr = "我们一起去飞"
let subIndex = subStr.index(subStr.startIndex, offsetBy: 3)
let sub1 = subStr[subStr.startIndex..<subIndex]
print("sub1 = \(sub1)")
//sub1 = 我们一

let subIndex2 = subStr.index(subStr.startIndex, offsetBy: 3)
let sub2 = subStr[subIndex2..<subStr.endIndex]
print("sub2 = \(sub2)")
//sub2 = 起去飞
3、字符串拆分、转数组
let arrayStr = "11233244255266277"
let array = arrayStr.components(separatedBy: "2")
print("array = \(array)")
//array = ["11", "33", "44", "55", "66", "77"]
4、字符串替换
let arrayStr1 = "11233244255266277"
let comStr = arrayStr1.replacingOccurrences(of: "2", with: " ")
print("comStr = \(comStr)")
//comStr = 11 33 44 55 66 77
5、前缀、后缀、转大写、转小写、首字母大写,其他小写
let arrayStr2 = "11233244255266277"
//前缀
let a = arrayStr2.hasPrefix("11")
//后缀
let b = arrayStr2.hasSuffix("77")
print("a = \(a) - b = \(b)")
//a = true - b = true

let bitStr = "bei JING"
//转大写
print("big = \(bitStr.uppercased())")
//big = BEI JING

//转小写
print("small = \(bitStr.lowercased())")
//small = bei jing

//首字母大写,其他小写
print("small = \(bitStr.capitalized)")
//small = Bei Jing
6、编码、解码
let codeStr = "北京欢迎你"

//编码
if let codeStr1 = codeStr.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
    print("codeStr1 = \(codeStr1)")
    //codeStr1 = %E5%8C%97%E4%BA%AC%E6%AC%A2%E8%BF%8E%E4%BD%A0
    
    //解码
    if let codeStr2 = codeStr1.removingPercentEncoding {
        print("codeStr2 = \(codeStr2)")
        //codeStr2 = 北京欢迎你
    }
}

相关文章

网友评论

      本文标题:Swift 字符串

      本文链接:https://www.haomeiwen.com/subject/findfqtx.html