美文网首页Swift4学习
Swift4-字符串和字符

Swift4-字符串和字符

作者: wingsrao | 来源:发表于2018-08-09 10:53 被阅读146次

    字符串和字符

    1.Swift 的 String类型桥接到了基础库中的 NSString类。Foundation 同时也扩展了所有 NSString 定义的方法给 String 。也就是说,如果你导入 Foundation ,就可以在 String 中访问所有的 NSString 方法,无需转换格式。

    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.字符串是值类型:
    每一次赋值和传递,现存的 String值都会被复制一次,传递走的是拷贝而不是原本。

    4.Swift 编译器优化了字符串使用的资源,实际上拷贝只会在确实需要的时候才进行。

    5.String值可以通过传入 Character值的字符串作为实际参数到它的初始化器来构造:

    let catCharacters: [Character] = ["C", "a", "t", "!", "?"]
    let catString = String(catCharacters)
    print(catString)
    // prints "Cat!?"
    

    6.你不能把 String或者 Character追加到已经存在的 Character变量当中,因为 Character值能且只能包含一个字符。

    7.Swift 的 String和 Character类型是完全 Unicode 兼容的。

    8.字符串字面量中的特殊字符

    let wiseWords = "\"Imagination is more important than knowledge\" - Einstein"
    // "Imagination is more important than knowledge" - Einstein
    let dollarSign = "\u{24}" // $, Unicode scalar U+0024
    let blackHeart = "\u{2665}" // ♥, Unicode scalar U+2665
    let sparklingHeart = "\u{1F496}" // ?, Unicode scalar U+1F496
    

    9.字符串统计:testStr.count
    通过 count属性返回的字符统计并不会总是与包含相同字符的 NSString中 length属性相同。 NSString中的长度是基于在字符串的 UTF-16 表示中16位码元的数量来表示的,而不是字符串中 Unicode 扩展字形集群的数量。

    var word = "cafe"
    print("the number of characters in \(word) is \(word.count)")
    // Prints "the number of characters in cafe is 4"
    word += "\u{301}"    // COMBINING ACUTE ACCENT, U+0301
    print("the number of characters in \(word) is \(word.count)")
    // Prints "the number of characters in café is 4"
    

    10.字符串索引:

    let greeting = "Guten Tag!"
    greeting[greeting.startIndex]
    // G
    greeting[greeting.index(before: greeting.endIndex)]
    // !
    greeting[greeting.index(after: greeting.startIndex)]
    // u
    let index = greeting.index(greeting.startIndex, offsetBy: 7)
    greeting[index]
    // a
    

    使用 indices属性来访问字符串中每个字符的索引。

    for index in greeting.indices {
        print("\(greeting[index]) ", terminator: "")//terminator把换行去掉
    }
    

    你可以在任何遵循了 Indexable 协议的类型中使用 startIndex 和 endIndex 属性以及 index(before:) , index(after:) 和 index(_:offsetBy:) 方法。这包括这里使用的 String ,还有集合类型比如 Array , Dictionary 和 Set 。

    11.插入和删除
    insert(_:at:):

    var welcome = "hello"
    welcome.insert("!", at: welcome.endIndex)
    // welcome now equals "hello!"
    

    insert(contentsOf:at:):

    welcome.insert(contentsOf: " there", at: welcome.index(before: welcome.endIndex))
    // welcome now equals "hello there!"
    

    remove(at:):

    welcome.remove(at: welcome.index(before: welcome.endIndex))
    // welcome now equals "hello there"
    

    removeSubrange(_:)

    let range = welcome.index(welcome.endIndex, offsetBy: -6)..<welcome.endIndex
    welcome.removeSubrange(range)
    // welcome now equals "hello"
    

    你可以在任何遵循了 RangeReplaceableIndexable 协议的类型中使用 insert(_:at:) , insert(contentsOf:at:) , remove(at:) 方法。这包括了这里使用的 String ,同样还有集合类型比如 Array , Dictionary 和 Set 。

    12.子字符串并不适合长期保存——因为它们重用了原字符串的内存,只要这个字符串有子字符串在使用中,那么这个字符串就必须一直保存在内存里。

    let newString = String(beginning)
    //newString 是字符串——当它从子字符串创建时,它就有了自己的内存。
    

    13.两个 String值(或者两个 Character值)如果它们的扩展字形集群是规范化相等,则被认为是相等的。

    // "Voulez-vous un café?" using LATIN SMALL LETTER E WITH ACUTE
    let eAcuteQuestion = "Voulez-vous un caf\u{E9}?"
    // "Voulez-vous un café?" using LATIN SMALL LETTER E and COMBINING ACUTE ACCENT
    let combinedEAcuteQuestion = "Voulez-vous un caf\u{65}\u{301}?"
    if eAcuteQuestion == combinedEAcuteQuestion {
        print("These two strings are considered equal")
    }
    // prints "These two strings are considered equal"
    

    14.hasPrefix(:)和 hasSuffix(:)

    相关文章

      网友评论

        本文标题:Swift4-字符串和字符

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