03*:字符串

问题

 

目录

 

预备

 

正文

func testString() {
        /* 复习大纲
            1:多行字符串          '''  '''
            2:多行字符串缩进       末尾'''
            3:取消多行字符串换行    \
            4:扩展字符串分隔符      #
         */
        
        
        // 1.1:多行字符串
        let singleLineString = "These are the same."
        let multilineString = """
        These are the same.
        """
        
        print("1.1:单行:\(singleLineString), 多行:\(multilineString), 相等:\(singleLineString == multilineString)\n")
        
        // 1.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."
        """
        print("1.2:多行普通字符串:\n\(quotation)")
        
        // 1.3:多行字符串 开始换行 结束换行
        let multilineString1 = """
        
            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."
        
        """
        print("1.3:多行开始换行字符串:\n\(multilineString1)")
        
        // 1.4:多行字符串 缩进
        // 多行字符串可以缩进以匹配周围的代码。双引号( """ )前的空格会告诉 Swift 其他行前应该有多少空白是需要忽略的。比如说,尽管下面函数中多行字符串字面量缩进了,但实际上字符串不会以任何空白开头。
        // 在上面的例子中,尽管整个多行字符串字面量被缩进了,字符串中的第一行和最后一行不会有任何空白。中间的行如果有比结束引号有更多的缩进,那么它就会有额外的四个空格的缩进。
        let multilineString2 = """
            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."
            """
        print("1.4:多行缩进字符串:\n\(multilineString2)")

        // 5:扩展字符串分隔符 过把字符串放在双引号( " )内并由井号( # )包裹。 你可以在字符串字面量中放置扩展分隔符来在字符串中包含特殊字符而不让它们真的生效。通过把字符串放在双引号( " )内并由井号( # )包裹。比如说,打印字符串字面量 #"Line 1\nLine 2"# 会打印出换行符 \n 而不是打印出两行。
        let mutilineString3 = #"Line 1\#nLine 2"#  // Line 1\#nLine 2
        let mutilineString6 = #"Line 5\nLine 6"#
        let mutilineString4 = ##"Line 3\##nLine 4"##
        let mutilineString5 = ###"Line 5\###nLine 6"###
        print("3:扩展字符串分隔符:\n\(mutilineString3)\n\(mutilineString6)\n\(mutilineString4)\n\(mutilineString4)\n")
        
        
        // 6: 用末尾\去掉换行符号
        let softWrappedQuotation = """
        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."
        """
        
        print(softWrappedQuotation)
    }

 

注意:

引用

1:

posted on 2021-01-03 21:38  风zk  阅读(179)  评论(0编辑  收藏  举报

导航