C# StringBuilder,在值之间添加制表符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9191756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
StringBuilder, add Tab between Values
提问by eMi
I have a small problem:
我有一个小问题:
I have a List of fields, with 3 Values. I want to build my String with these three Values, delimited by a "TAB"..
我有一个字段列表,有 3 个值。我想用这三个值构建我的字符串,用“TAB”分隔。
Code:
代码:
StringBuilder stringBuilder = new StringBuilder();
foreach (string field in fields)
{
stringBuilder.Append(field).Append("\t");
}
stringBuilder.AppendLine();
return stringBuilder.ToString();
The Tab is only between the 3rd and 2nd Value (Between 1st and 2nd is a Space ?!)
选项卡仅在第 3 个和第 2 个值之间(第 1 个和第 2 个值之间是空格?!)
So I tried this:
所以我试过这个:
StringBuilder stringBuilder = new StringBuilder();
foreach (string field in fields)
{
stringBuilder.Append(field + "\t").Append("\t");
}
stringBuilder.AppendLine();
return stringBuilder.ToString();
Then I have 2 Tabs between 3rd and 2nd, one Tab between 1st and 2nd and also a Space between 1st and 2nd..
然后我在第 3 个和第 2 个之间有 2 个标签,第 1 个和第 2 个之间有一个标签,第 1 个和第 2 个之间还有一个空格。
(The Space is always there, how to avoid that?)
(空间总是在那里,如何避免?)
So what I have to do? Need only(without spaces..) a Tab between these Values..
那我该怎么办?只需要(没有空格..)这些值之间的制表符..
采纳答案by Yahia
try
尝试
StringBuilder stringBuilder = new StringBuilder();
foreach (string field in fields)
{
stringBuilder.Append(field.Trim()).Append("\t");
}
stringBuilder.AppendLine();
return stringBuilder.ToString();
Basically I would just use return string.Join ( "\t", fields );.
基本上我只会使用return string.Join ( "\t", fields );.
回答by ColWhi
I would have thought you would want
我以为你会想要
StringBuilder stringBuilder = new StringBuilder();
foreach (string field in fields)
{
stringBuilder.Append(field);
stringBuilder.Append("\t");
}
stringBuilder.AppendLine();
return stringBuilder.ToString();
回答by Oded
Instead of looping, you can use string.Join:
您可以使用string.Join:
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine(string.Join("\t", fields));
Note that you can pass in the string directly to AppendLineas well.
请注意,您也可以直接将字符串传递给AppendLine。
回答by Prakash P
I think you are confusing with tab char and sapces.
我认为您对 tab char 和 sapces 感到困惑。
Are you expecting fixed no of white spaces to be added in the end of every word?
您是否希望在每个单词的末尾添加固定的空格?
\t -> is just a tab char
\t -> 只是一个制表符
The following is generated by the code given by you.
以下是由您提供的代码生成的。
Java StackOverflow Banyan
Javasun StackOverflow Banyan
The above two lines have same tab char b/w the 1st & 2nd Word.
上面两行的第一个和第二个字具有相同的制表符。
if you type one more char in the end of "Javasun" it will extend like the following
如果在“Javasun”的末尾再输入一个字符,它将像下面这样扩展
Javaasunk StackOverflow Banyan
回答by Gurbachan Singh
Sub WarningWindow(ByVal content As String)
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
Dim gridHTML As String = sw.ToString().Replace("""", "'").Replace(System.Environment.NewLine, "")
Dim sb As New StringBuilder()
Dim a As String = "Welcomeback"
'GridView1.RenderControl(hw)
sb.Append("<script type = 'text/javascript'>")
sb.Append("window.onload = new function(){")
sb.Append("var printWin = window.open('', '', 'left=0")
sb.Append(",top=0,width=1400,height=500,resize=yes,scrollbars =yes');")
sb.Append("printWin.document.write(""")
sb.Append("<INPUT Type=Button Name=Close Size=40 Value='Clsose(GSS)' onclick='self.close()'; / > ")
sb.Append("</br><INPUT Type=Button Name=fu Size=40 Value='Alert' onclick=javascript:window.alert('" & a & "'); / > ")
sb.Append("<INPUT Type=Text Name=gss Size=40 Value=300 ; / > ")
sb.Append(" <P>")
sb.Append(content)
sb.Append(""");")
sb.Append("printWin.focus();")
sb.Append("printWin.show;};")
sb.Append("</script>")
ClientScript.RegisterStartupScript(Me.GetType(), "suvesh", sb.ToString())
End Sub

