StringBuilder,在值之间添加制表符
我有一个小问题:
我有一个字段列表,有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试
基本上我只会使用
return string.Join ( "\t",字段);
。try
Basically I would just use
return string.Join ( "\t", fields );
.我认为您对制表符和空间感到困惑。
您是否期望在每个单词的末尾添加固定数量的空格?
\t->只是一个制表符
以下是由您给出的代码生成的。
上面两行的第一行和第二行具有相同的黑白制表符。第二句话。
如果您在“Javasun”末尾再输入一个字符,它将像下面这样扩展
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.
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
我原以为你会想要
I would have thought you would want
您可以使用
string.Join
:请注意,您也可以将字符串直接传递给
AppendLine
。Instead of looping, you can use
string.Join
:Note that you can pass in the string directly to
AppendLine
as well.