StringBuilder,在值之间添加制表符

发布于 2025-01-03 18:17:00 字数 758 浏览 2 评论 0原文

我有一个小问题:

我有一个字段列表,有 3 个值。我想用这三个值构建我的字符串, 由 "TAB" 分隔。

代码:

StringBuilder stringBuilder = new StringBuilder();
foreach (string field in fields)
{
    stringBuilder.Append(field).Append("\t");
}
stringBuilder.AppendLine();
return stringBuilder.ToString();

制表符仅位于第三个和第二个值之间(第一个和第二个值之间是一个空格?!)

所以我尝试了这个:

StringBuilder stringBuilder = new StringBuilder();
foreach (string field in fields)
{
    stringBuilder.Append(field + "\t").Append("\t");
}
stringBuilder.AppendLine();
return stringBuilder.ToString();

然后我在第三个值之间有 2 个制表符第二个,第一个和第二个之间有一个制表符,还有一个空格 在第一和第二之间..

(空间总是存在的,如何避免这种情况?)

那么我必须做什么?只需(没有空格..)这些值之间有一个制表符。

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"..

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 ?!)

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..

(The Space is always there, how to avoid that?)

So what I have to do? Need only (without spaces..) a Tab between these Values..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

冰之心 2025-01-10 18:17:00

尝试

StringBuilder stringBuilder = new StringBuilder();
foreach (string field in fields)
{
    stringBuilder.Append(field.Trim()).Append("\t");
}
stringBuilder.AppendLine();
return stringBuilder.ToString();

基本上我只会使用 return string.Join ( "\t",字段);

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 ); .

我做我的改变 2025-01-10 18:17:00

我认为您对制表符和空间感到困惑。

您是否期望在每个单词的末尾添加固定数量的空格?

\t->只是一个制表符

以下是由您给出的代码生成的。

Java    StackOverflow   Banyan
Javasun StackOverflow   Banyan

上面两行的第一行和第二行具有相同的黑白制表符。第二句话。

如果您在“Javasun”末尾再输入一个字符,它将像下面这样扩展

Javaasunk   StackOverflow   Banyan

I think you are confusing with tab char and sapces.

Are you expecting fixed no of white spaces to be added in the end of every word?

\t -> is just a tab char

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

Javaasunk   StackOverflow   Banyan
梦幻之岛 2025-01-10 18:17:00

我原以为你会想要

StringBuilder stringBuilder = new StringBuilder();
foreach (string field in fields)
{
    stringBuilder.Append(field);
    stringBuilder.Append("\t");
}
stringBuilder.AppendLine();
return stringBuilder.ToString();

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();
爱的那么颓废 2025-01-10 18:17:00

您可以使用 string.Join

StringBuilder stringBuilder = new StringBuilder();

stringBuilder.AppendLine(string.Join("\t", fields));

请注意,您也可以将字符串直接传递给AppendLine

Instead of looping, you can use string.Join:

StringBuilder stringBuilder = new StringBuilder();

stringBuilder.AppendLine(string.Join("\t", fields));

Note that you can pass in the string directly to AppendLine as well.

对你再特殊 2025-01-10 18:17:00
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文