欢迎来到代码驿站!

Golang

当前位置:首页 > 脚本语言 > Golang

golang 如何替换掉字符串里面的换行符\n

时间:2023-02-27 10:34:03|栏目:Golang|点击:

原始字符串:

-----BEGIN LICENSE KEY-----\nL/+HAwEBB2xpY2Vuc2UB/4gAAQIBB1BheWxvYWQBCgABCVNpZ25hdHVyZQEKAAAA\n/gPo/4gB/gHd/7AQABpsaWNlbnNlLW1nci9jb25maWcuUGF5bG9hZP+BAwEBB1Bh\neWxvYWQB/4IAAQoBBUFwcElkAQwAAQZBcHBLZXkBDAABCVN0YXJ0VGltZQEMAAEE\nRGF5cwEEAAEGQWVzS2V5AQwAAQlIYXNoSWRLZXkBDAABBVRyaWFsAQQAAQVFeHRy\nYQEMAAEKRXhwaXJlVGltZQH/hAABD1Jlc3BvbnNlQ29udHJvbAH/hgAAABD/gwUB\nAQRUaW1lAf+EAAAA/+v/hQMBAQ9SZXNwb25zZUNvbnRyb2wB/4YAAREBBFpvbmUB\nBAABBEFyZWEBBAABCEJ1aWxkaW5nAQQAAQdQb2lOYW1lAQQAAQdBZGRyZXNzAQQA\nAQZTdHJlZXQBBAABCUxvbmdpdHVkZQEEAAEITGF0aXR1ZGUBBAABBUJyYW5kAQQA\nAQlGbG9vck5hbWUBBAABCFByb3ZpbmNlAQQAAQRDaXR5AQQAAQZSZWdpb24BBAAB\nB1BvaUNvZGUBBAABBkNwQ29kZQEEAAELUG9pQ2F0ZWdvcnkBBAABDFBvaUF0dHJp\nYnV0ZQEEAAAALP+CKQEBMQEBMQETMjAxOS0wNS0yOCAyMjowMDowMAECAQExAQEx\nAgExAgAAAf4CAH0RJSpHHmNY/hAQt43pM+qyxZGQ3WzVv1LsaxX55IAqSVHE5w4N\nBmWt3YUt/tG4fy++q1GpWvTsEqAGo1BNTyi/TqK57z5d57GC/gBYlDU8Ke3ZVnyR\n0qWOq5rKdWuMuyNGG9S24U6maaH+sVoumnVSWZohBrOgmjXQ5n2T4c3O+sTjme56\n16UVEXEPRMfAOUaK0Vq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YW5kAQQA\nAQlGbG9vck5hbWUBBAABCFByb3ZpbmNlAQQAAQRDaXR5AQQAAQZSZWdpb24BBAAB\nB1BvaUNvZGUBBAABBkNwQ29kZQEEAAELUG9pQ2F0ZWdvcnkBBAABDFBvaUF0dHJp\nYnV0ZQEEAAAALP+CKQEBMQEBMQETMjAxOS0wNS0yOCAyMjowMDowMAECAQExAQEx\nAgExAgAAAf4CAH0RJSpHHmNY/hAQt43pM+qyxZGQ3WzVv1LsaxX55IAqSVHE5w4N\nBmWt3YUt/tG4fy++q1GpWvTsEqAGo1BNTyi/TqK57z5d57GC/gBYlDU8Ke3ZVnyR\n0qWOq5rKdWuMuyNGG9S24U6maaH+sVoumnVSWZohBrOgmjXQ5n2T4c3O+sTjme56\n16UVEXEPRMfAOUaK0Vqte4zpuOuhqQBao0u/nUWBz5zOdlNQYhM5nLWnElmPET8O\namG6FJO16N3UaYaHP+yxlEN4ma1hJUvxm1i5GCEZbOxJY1ke1Dsj3Em3nVg52w4p\ncpAfb5ep9I1xkbavIQ2gxNCjmQG34aDhciVXkEBepVtQZ8ay6b96EiEnhnF72TEy\n/wtxx2p8KoN6wK60PQpW1ZdebROmGOBxQngJkTjjP48gkz+8XaYTpbCMRf4QIbwe\n+QdcnRHm/lgcTY2z98YUaVKmC/TuNzmmaOs/0lPEpq4Z1zgktsCLaMjq6fRjfCOW\n6s4loZhCOh57IlY0AmDuBi2fVwtV7kEyqqxF+2VIOXR/QQakdutfOzimrD3UbZZm\nJOLcbyq1EJjwq+yEs8fXNAhg8lOJMbY8EJ1kNrKjRGUZ82I7WDMYiuZopQNId/HE\nnQDUcLqDQ6x3CCeCVFL+KW5SfVrqwelSaZaeogkHX3RftdIVHhltCwegAA==\n-----END LICENSE KEY-----`
strings.Replace(license, "\n", "", -1)

直接使用\n根本无法替换成功,正确的做法是对\n再进行转义:\\n

strings.Replace(license, "\\n", "", -1)

补充:golang 字符串去除空格和换行符

字符串去除空格和换行符

package main  
import ( 
  "fmt" 
  "strings" 
) 
 
func main() { 
  str := "这里是 www\n.jb51\n.net" 
  fmt.Println("-------- 原字符串 ----------") 
  fmt.Println(str) 
  // 去除空格 
  str = strings.Replace(str, " ", "", -1) 
  // 去除换行符 
  str = strings.Replace(str, "\n", "", -1) 
  fmt.Println("-------- 去除空格与换行后 ----------") 
  fmt.Println(str) 
}

输出结果为:

-------- 原字符串 ----------
这里是 www
.jb51
.net
-------- 去除空格与换行后 ----------
这里是www.jb51.net

上一篇:golang如何操作csv文件详解

栏    目:Golang

下一篇:golang快速实现网页截图的方法

本文标题:golang 如何替换掉字符串里面的换行符\n

本文地址:http://www.codeinn.net/misctech/226526.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有