String 字符串变量的漂亮格式,带有多个“quot\";
我在格式化包含多个String 字符串变量的漂亮格式,带有多个“quot\";,string,go,String,Go,我在格式化包含多个\n(回车)的字符串变量时遇到一些问题。我有一个字符串变量,其中存储了以下内容: “1.检查连接\n2.是否已检查防火墙设置\n3.检查交换机/网络连接\n4.与管理员联系” 现在,我想打印行,使其看起来像这样: Remedy: 1. Check the connections 2. Have the firewall setting been checked
\n
(回车)的字符串变量时遇到一些问题。我有一个字符串变量,其中存储了以下内容:
“1.检查连接\n2.是否已检查防火墙设置\n3.检查交换机/网络连接\n4.与管理员联系”
现在,我想打印行,使其看起来像这样:
Remedy: 1. Check the connections
2. Have the firewall setting been checked
3. Check switch/network connections
4. Contact admin
remedy: 1. Check the connections
2. Have the firewall settings been checked
3. Check switch/network connections
4. Contact admin
但是,在我的程序中,当我运行输出行时:
fmt.Println("remedy:\t\t\t\t" + alt.Remedy)
结果是这样的:
Remedy: 1. Check the connections
2. Have the firewall setting been checked
3. Check switch/network connections
4. Contact admin
remedy: 1. Check the connections
2. Have the firewall settings been checked
3. Check switch/network connections
4. Contact admin
我如何获得它,以便通过发出该命令,所有4个选项都列在同一列中?在本例中,我希望选项2、3和4列在#1选项下。没有什么可以为您做的 相反,您可以将字符串拆分为行,并相应地设置其格式 以下是一个例子:
package main
import (
"fmt"
"strings"
)
func main() {
str := "1. Check the connections \n2. Have the firewall settings been checked \n3. Check switch/network connections \n4. Contact admin"
// Split the string into lines.
parts := strings.Split(str, "\n")
// Iterate over the lines.
for i, s := range parts {
if i == 0 {
// First line: start with "Remedy"
fmt.Printf("Remedy:\t\t\t%s\n", s)
} else {
fmt.Printf(" \t\t\t%s\n", s)
}
}
}
这将产生:
Remedy: 1. Check the connections
2. Have the firewall settings been checked
3. Check switch/network connections
4. Contact admin
注意:可能还有其他几种方法可以做到这一点,我刚刚选择了这一种。由于您将“补救措施”存储在一个字符串中,因此除了在字符串变量中包含空格之外,您没有什么其他方法可以做
"1. Check the connections\n\t\t\t\t2. Have the firewall settings been checked\n\t\t\t\t3. Check switch/network connections\n\t\t\t\t4. Contact admin"
我建议将它们保存为一个切片并迭代打印:
package main
import "fmt"
var remedy = []string{
"Check the connections",
"Have the firewall settings been checked",
"Check switch/network connections",
"Contact admin",
}
func main() {
fmt.Print("remedy:")
for i := range remedy {
if i == 0 {
fmt.Printf("%13d. %s\n", i+1, remedy[i])
continue
}
fmt.Printf("%20d. %s\n", i+1, remedy[i])
}
}
字符串文字表示从中获取的字符串常量
连接字符序列。有两种形式:生的
字符串文字和解释的字符串文字
原始字符串文字是反引号之间的字符序列,如中所示
foo
。在引号内,除后引号外,任何字符都可以出现。
原始字符串文本的值是由
字符之间的未解释(隐式UTF-8编码)字符
引用;特别是,反斜杠没有特殊的含义
字符串可能包含换行符。内部的回车符('\r')
原始字符串文字将从原始字符串值中丢弃
使用一个。比如说,
package main
import (
"fmt"
)
func main() {
message := `
Remedy: 1. Check the connections
2. Have the firewall setting been checked
3. Check switch/network connections
4. Contact admin
`
fmt.Print(message[1:])
}
游乐场:
输出:
Remedy: 1. Check the connections
2. Have the firewall setting been checked
3. Check switch/network connections
4. Contact admin
与马克的答案相似,但我会这样做。它更简单、更短、更干净: 用换行符加上非第一行所需的缩进替换源
字符串中的每个换行符。你可以用这个
要格式化的源字符串:
s:=“1.检查连接\n2.检查防火墙设置\n3.检查交换机/网络连接\n4.联系管理员”
格式是一个简单的行:
fmt.Println("Remedy: ", strings.Replace(s, "\n", "\n ", -1))
输出(在上尝试):
如果将它们都保存在一个字符串变量中是一项要求,那么Marc的答案在我看来是最好的。看看peterSO的建议。是的,但我不喜欢这个答案的原因是因为它修改了op试图使用的变量。Marc的答案是唯一适用于他的字符串的答案。任何解决方案都需要修改某些内容,因为当前行为不是所需的行为。显然,某些更改需要的重构比其他更改少。由于所有op都给出了一个带有\n
的字符串变量,因此应该假设这是输入,需要对其进行修改以提供所需的输出。答案应该是“用xyz修改”,而不是回答“更改您的输入”。请看peterSO在中的建议。当然,如果您正在构建自己的字符串,为什么需要格式化函数?我以为这是从别处传来的。不过,OP还是有点不清楚字符串的起源。很高兴听到这个消息,但请澄清你在问题中的情况。如果这是针对硬编码字符串,那么@peterSO的解决方案肯定更可取。