C#问题自动将标签char转换为空间

发布于 2025-01-25 18:35:14 字数 907 浏览 1 评论 0原文

我正在遇到一个问题,即tab char似乎会自动转换为随机转换为空间。

char Delimiter = '\t'; // \t is tab
string headers = $"Column1{Delimiter}Column2{Delimiter}Column3{Delimiter}Column4{Delimiter}Column5{Delimiter}Column6{Delimiter}Column7";
string outputFilePathAndName = "C:/MyOutputFile";
TextWriter writer = new StreamWriter(outputFilePathAndName, append: true);
writer.WriteLine(headers);
writer.Flush();

预期输出:

Column1    Column2    Column3    Column4    Column5    Column6    Column7

实际输出:

Column1 Column2 Column3    Column4    Column5    Column6    Column7

任何想法为什么前2个总是以空格而不是标签出现?

我尝试过的东西:

  • 使用字符串串联而不是
  • 在Tab字符中插值粘贴而不是'\ t '
  • 打开输出文件,使用不同的文本编辑器
  • 检查字符串标题在调试时看起来正确。
  • 用力编码该行作为column1 \ tcolumn2 \ tcolumn3 \ t ...

这是流动者的错误吗?

I'm experiencing a problem where tab char seems to be automatically converted to a space randomly.

char Delimiter = '\t'; // \t is tab
string headers = 
quot;Column1{Delimiter}Column2{Delimiter}Column3{Delimiter}Column4{Delimiter}Column5{Delimiter}Column6{Delimiter}Column7";
string outputFilePathAndName = "C:/MyOutputFile";
TextWriter writer = new StreamWriter(outputFilePathAndName, append: true);
writer.WriteLine(headers);
writer.Flush();

Expected output:

Column1    Column2    Column3    Column4    Column5    Column6    Column7

Actual output:

Column1 Column2 Column3    Column4    Column5    Column6    Column7

Any idea why the first 2 tabs always come out as spaces instead of tabs?

Things I have tried:

  • Using string concatenation instead of interpolation
  • Pasting in a tab character instead of '\t'
  • Opening the output file with different text editors
  • Examining the string headers while debugging, it looks correct.
  • Hard-coding the line as Column1\tColumn2\tColumn3\t...

Is this a bug with StreamWriter?

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

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

发布评论

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

评论(1

冷默言语 2025-02-01 18:35:14

我本可以发誓要看到空间,但这实际上是一个视觉故障,即使在Notepad等基本文本编辑器中,Tab角色在视觉上出现时也出现。我不知道为什么它们是不同长度的原因,但是我发现的最好的方法是确定存在的字符是 使用记事本++,然后选择“选项”视图>显示符号>显示白色空间和标签。当您这样做时,它将像箭头一样显示标签,并将空格作为点。

通常我会删除这样的帖子,因为实际上没有错误,这是我的错误,但是我会在这里留下我的答案,因为,正如<<< a href =“ https://stackoverflow.com/users/1410664/caius-jard”> caius jard 的评论,其他人也有同样的问题,我敢肯定,更多的人会有同样的问题将来。

感谢那些帮助我通过评论理解这一点的人。我只是代表他们发布了这个答案,因为没有人发布正式答案。

I could have sworn I was seeing spaces, but this was actually a visual glitch where tab characters appeared visually as just spaces, even in basic text editors like Notepad. I don't know the reason why they appear as different lengths, but the best way I found to tell for sure what characters are present is to use Notepad++ and select the option View >Show symbol >Show white space and tab. When you do that it will show tabs like an arrow and spaces as a dot.

visual of tab characters that look different

Normally I would delete a post like this since there was actually no bug, it was a mistake on my part, but I will leave it up with my answer here because, as evidenced by Caius Jard's comment, other people have had the same question and I'm sure more people will have the same question in the future.

Credit to those who helped me understand this with their comments. I'm just posting this answer on their behalf since no one posted an official answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文