doutonghang2761 2019-05-25 13:21
浏览 39
已采纳

将行尾转换为“ ”文字

I have an interesting problem to solve. Because of a tool that I have to talk to, I need to convert newlines to the literal string I have the following data

{"name": 2019-05-25,
"tracker": {
"project": {
  "uri": "/project/87",
  "name": "Allen's Test"
},
"uri": "/tracker/57483",
"name": "Tasks"
        },
"description": "[{Table

||Stack
||Current Version
||New Version
||Changes
|common
|1.0.214
|1.0.214
|* blah - [#345|https://blah.com/345]

|workflow
|2.2.23
|2.2.23
|* [ES-12345] blah - [#1087|https://blah.com/1087]

}]",
"descFormat": "Wiki"}

so basically instead of a multiline string, I need to convert it to a single line string with 's where the tool on the backend will convert it. I'm using go and not quite sure where to start. I asssume I need raw strings, but many of the source bits are api calls that have the newlines built in.

Any help would be appreciated.

  • 写回答

2条回答 默认 最新

  • doubi2145 2019-05-25 15:52
    关注

    For example,

    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
        s := `"project": {
      "uri": "/project/87",
      "name": "Allen's Test"
    },`
        fmt.Println(s)
        s = strings.ReplaceAll(s, "
    ", `
    `)
        fmt.Println(s)
    }
    

    Playground: https://play.golang.org/p/lKZw78yOuMc

    Output:

    "project": {
      "uri": "/project/87",
      "name": "Allen's Test"
    },
    "project": {
      "uri": "/project/87",
      "name": "Allen's Test"
    },
    

    For before Go 1.12, write:

    s = strings.Replace(s, "
    ", `
    `, -1)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类