【问题标题】:How can I add a new word before last three lines in a string in swift?如何快速在字符串的最后三行之前添加一个新单词?
【发布时间】:2019-06-13 11:50:23
【问题描述】:

如何快速在字符串的最后三行之前添加一个新单词,以便该字符串可以从此转换:

"Who composed the\n
popular song "Aati Kya\n
Khandala" from 'Ghulam"?\n
Jatin- Lalit\n
Abhijeet\n
Aamir Khan"

到这里:

"Who composed the\n
popular song "Aati Kya\n
Khandala" from 'Ghulam"?\n
sagarduhanishere\n
Jatin- Lalit\n
Abhijeet\n
Aamir Khan"

【问题讨论】:

  • 使用components(separatedBy:)String 转换为Array,然后在需要的地方插入新字符串,再将其转换回String
  • 你能给我写代码吗?

标签: swift algorithm slice


【解决方案1】:

这是正则表达式的方式。这是另一种选择。

        let s = """
        Who composed the
        popular song "Aati Kya
        Khandala" from 'Ghulam"?
        Jatin- Lalit
        Abhijeet
        Aamir Khan
        """

        let replaceString = "sagarduhanishere"

        let nextS = s.replacingOccurrences(of: "(\n.*){3}$", with: "\n\(replaceString)$0", options: .regularExpression  , range: nil)

        print(nextS)

【讨论】:

    【解决方案2】:

    接受的答案适用于短字符串。为了获得更好的空间(和时间)复杂性,您可以避免不必要地拆分字符串并重新加入数组的所有元素,这种方式:

    让我们从这段文字开始:

    let text = """
    Who composed the
    popular song "Aati Kya
    Khandala" from 'Ghulam"?
    Jatin- Lalit
    Abhijeet
    Aamir Khan
    """
    

    结果将存储在这个变量中:

    var result = ""
    

    使用下面的代码,我们从原始字符串的末尾开始寻找第三个\n

    let start = text.startIndex
    let end = text.endIndex
    var index = text.index(end, offsetBy: -1)
    var linesToGo = 3
    
    while start < index  {
        if text[index] == "\n" {
            linesToGo -= 1
        }
        if linesToGo == 0 {
            index = text.index(index, offsetBy: 1)
            result = text[start..<index] + "sagarduhanishere\n" + text[index..<end]
            break
        }
        index = text.index(index, offsetBy: -1)
    }
    

    然后我们可以检查是否找到了 3 行

    if linesToGo != 0 {
        fatalError("More lines needed")
    }
    

    然后打印结果或在您的代码中使用它:

    print(result)
    

    哪个输出:

    Who composed the
    popular song "Aati Kya
    Khandala" from 'Ghulam"?
    sagarduhanishere
    Jatin- Lalit
    Abhijeet
    Aamir Khan
    

    【讨论】:

      【解决方案3】:

      我想你有这样的字符串

      let string = """
          "Who composed the
          popular song "Aati Kya
          Khandala" from 'Ghulam"?
          Jatin- Lalit
          Abhijeet
          Aamir Khan"
      """
      

      或者像这样

      let string = """
          "Who composed the\npopular song "Aati Kya\nKhandala" from 'Ghulam"?\nJatin- Lalit\nAbhijeet\nAamir Khan"
      """
      

      所以,首先将你的字符串分隔成一个字符串数组

      var separated = string.components(separatedBy: "\n")
      

      然后在需要的索引处插入新字符串

      separated.insert("sagarduhanishere", at: separated.endIndex - 3)
      

      然后将这个数组加入到一个字符串中

      let joined = separated.joined(separator: "\n")
      

      var separated = string.components(separatedBy: "\n")
      separated.insert("sagarduhanishere", at: separated.endIndex - 3)
      let joined = separated.joined(separator: "\n")
      print(joined)
      

      "Who composed the popular song "Aati Kya Khandala" from 'Ghulam"? sagarduhanishere Jatin- Lalit Abhijeet Aamir Khan"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-09-24
        • 1970-01-01
        • 2015-07-10
        • 1970-01-01
        • 2020-12-21
        相关资源
        最近更新 更多