Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string.Split function in c# tab delimiter

Tags:

string

c#

split

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());

        .......
 }
like image 968
Dimitar Tsonev Avatar asked Sep 12 '12 09:09

Dimitar Tsonev


People also ask

What is string split () and give its syntax?

The string split() method breaks a given string around matches of the given regular expression. After splitting against the given regular expression, this method returns a string array.

How do I split a string into string?

Use the Split method when the substrings you want are separated by a known delimiting character (or characters). Regular expressions are useful when the string conforms to a fixed pattern. Use the IndexOf and Substring methods in conjunction when you don't want to extract all of the substrings in a string.

What is split () function used for?

The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.

What is strtok function in C?

Apr 07, 2007. The C function strtok() is a string tokenization function that takes two arguments: an initial string to be parsed and a const -qualified character delimiter. It returns a pointer to the first character of a token or to a null pointer if there is no token.

What happens when you split a string in C++?

If the split ( offset) is greater than the string’s length, the function returns 0, failure. (The function can “split” a string the same length as the original string, in which case you end up with a copy of the original string and an empty string.) Second, storage is allocated for the two string buffers.

What is the use of split () method in C?

In C#, Split() is a string class method. The Split() method returns an array of strings generated by splitting of original string separated by the delimiters passed as a parameter in Split() method. The delimiters can be a character or an array of characters or an array of strings. Or you can also say that it returns a string array which ...

How does the split () function work in Python?

The first thing the split () function does is to obtain the length of the original string: If the split ( offset) is greater than the string’s length, the function returns 0, failure. (The function can “split” a string the same length as the original string, in which case you end up with a copy of the original string and an empty string.)

How do you split a string into two parts?

The Split () method returns a string array containing the substrings. Here, we split the string at ::. Since the count parameter is not passed, the returned array contains all the substrings. Here, we split the string at characters ' ', ',', '.' , and '/'.


2 Answers

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());
like image 113
Vignesh.N Avatar answered Sep 19 '22 09:09

Vignesh.N


use single qutes for this like Split('\t'), this way you will be passing a char and not a string.

like image 23
MichaelT Avatar answered Sep 18 '22 09:09

MichaelT