在”|”字符前面添加反斜杠”\”。

huangapple go评论77阅读模式
英文:

Go: add backslash in front of '|' characters

问题

我正在尝试将字符串中的所有|替换为|,但是当我尝试执行以下操作时:

strings.Replace(f, "|", "\|", -1)

我得到了一个错误:expected selector or type assertion, found 'CHAR' '|'。

英文:

I'm trying to replace all the instances of | in a string with | but when
I try to do:

strings.Replace(f, "|". '\|', -1)

I get: expected selector or type assertion, found 'CHAR' ' | ' error.

答案1

得分: 4

使用这个:

strings.Replace(f, "|", \|, -1)

示例:

http://play.golang.org/p/EQFR2EX5Bi

英文:

use this:

strings.Replace(f ,"|", `\|`, -1)

example:

http://play.golang.org/p/EQFR2EX5Bi

huangapple
  • 本文由 发表于 2015年6月5日 21:06:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/30667337.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定