I have a function which reads a delimited file.
The delimiter is passed to the function by string argument. The problem is, when I pass the "\\t"
delimiter, it ends up like "\\\\t"
and therefore, Split is unable to find this sequence.
How can I resolve this issue?
private void ReadFromFile(string filename, string delimiter)
{
StreamReader sr = new StreamReader(filename, Encoding.Default);
string[] firstLine = sr.ReadLine().Split(t.ToCharArray());
.......
}
I guess you are using something like
string sep = @"\t";
in this case sep will hold \\\\t
double back slash
use string sep = "\\t"
string content = "Hello\tWorld";
string sep = "\t";
string[] splitContent = content.Split(sep.ToCharArray());
像Split('\\ t')一样使用单一的qutes,这样你就会传递一个char而不是一个字符串。
pass parameter value as Decimal number of \\t (tab) and convert to it Char.
int delimeter =9;
// 9 ==> \t
// 10 ==> \n
// 13 ==> \r
char _delimeter = Convert.ToChar(delimeter);
string[] rowData = fileContent.Split(_delimeter);
Happy Programming.
If you pass in "\\t" as the delimiter nothing will change it to "\\t". Something else is double escaping your tab.
Blah("\t");
private static void Blah(string s)
{
var chars = s.ToCharArray();
Debug.Assert(chars.Length == 1);
var parts = "blah\tblah\thello".Split(chars);
Debug.Assert(parts.Length == 3);
}
Another way to do your split is replacing the TAB(\\t) by a blank space on this way:
if(linea.ToLower().Contains(@"\t"))
linea = linea.Replace(@"\t", " ");
retVal = linea.Trim().Split(' ')[1];
This code works for me.
你试过吗:Environment.NewLine?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.