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 Fluent udf 编写问题
  • ¥15 求合并两个字节流VB6代码
  • ¥15 Pyqt 如何正确的关掉Qthread,并且释放其中的锁?
  • ¥30 网站服务器通过node.js部署了一个项目!前端访问失败
  • ¥15 WPS访问权限不足怎么解决
  • ¥15 java幂等控制问题
  • ¥15 海湾GST-DJ-N500
  • ¥15 氧化掩蔽层与注入条件关系
  • ¥15 Django DRF 如何反序列化得到Python对象类型数据
  • ¥15 多数据源与Hystrix的冲突