String.Split function in C # tab delimiter

I have a function that reads a delimited file.

The separator is passed to the function with the string argument. The problem is that when I pass the delimiter "\t" , it ends up as "\\t" , and therefore Split cannot find this sequence.

How can I solve this problem?

 private void ReadFromFile(string filename, string delimiter) { StreamReader sr = new StreamReader(filename, Encoding.Default); string[] firstLine = sr.ReadLine().Split(t.ToCharArray()); ....... } 
+6
source share
6 answers

I assume that you are using something like

 string sep = @"\t"; 

in this case, sep will hold \\t double slash

use string sep = "\t"

 string content = "Hello\tWorld"; string sep = "\t"; string[] splitContent = content.Split(sep.ToCharArray()); 
+21
source

use single qutes for this type of Split ('\ t'), this way you will pass char, not a string.

+1
source

pass the parameter value as Decimal \ t (tab) and convert Char to it.

  int delimeter =9; // 9 ==> \t // 10 ==> \n // 13 ==> \r char _delimeter = Convert.ToChar(delimeter); string[] rowData = fileContent.Split(_delimeter); 

Happy programming.

+1
source

If you go to "\ t" since the delimiter will not change anything to "\ t". Something else doubles access to 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); } 
0
source

Another way to make your split is to replace TAB (\ t) with empty space as follows:

  if(linea.ToLower().Contains(@"\t")) linea = linea.Replace(@"\t", " "); retVal = linea.Trim().Split(' ')[1]; 

This code works for me.

0
source

Have you tried: Environment.NewLine?

-3
source

Source: https://habr.com/ru/post/925205/


All Articles