C#CSV Streamreader将\t转换为\\t [英] C# CSV Streamreader converts \t to \\t

查看:96
本文介绍了C#CSV Streamreader将\t转换为\\t的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常小的问题,但它让我感到悲伤,我正在尝试使用C#创建一个CSV / TSV阅读器,它可以接受,和\t作为分离器。

此行正常工作但当流读取器将文本转换为字符串时,它将\t转换为\\t

Very small problem but its causing me some grief, I'm trying to create a CSV/TSV reader using C# which can accept both "," and "\t" as splitters.
This line works fine but when the streamreader converts the text to a string it converts \t into \\t

using (StreamReader sr = File.OpenText(filepath))
{
   while ((line = sr.ReadLine()) != null) 

etc





在进入streamreader之前: test1,test2 \t test3

之后: test1,test2 \\t test3



我使用的分割器在下面,如果我在字符串中有\t它工作正常但如果它转换为\\t它不起作用。



Before it goes into the streamreader : test1 , test2 \t test3
After: test1 , test2 \\t test3

The splitters I use are below, if i have \t in the string it works fine but if its converted to \\t it doesnt work.

char[] delimiterChars = { ',',  '\t'};
string[] text = line.Split(delimiterChars);





我如何允许我的程序使用\\t作为char文字以及其他分隔符或停止streamreader将文本从\t转换为\\t



我将这个例子用于多个分隔符,但是分割需要char或char数组,所以\\t不会计算,因为它的字符串http://msdn.microsoft.com/en-us/library/ms228388.aspx [ ^ ]

推荐答案

是,简单。只需使用此string.split重载:



String.Split(String [],StringSplitOptions)



[ ^ ]
Yes, simple. Just use this string.split overload instead:

String.Split(String[], StringSplitOptions)

[^]


我想这个问题是以字符串形式读取的。我可能错了,但我会看到的选项是作为字节数组读取的。第二个选项被读作一个字符串,就像你用\t替换\\t然后拆分一样。
I guess this issue is due reading as a string. I might be wrong but options I would look at is read as array of byte. Second options is read as a string same as you are doing replace \\t with \t and then split.


这篇关于C#CSV Streamreader将\t转换为\\t的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆