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());
.......
}
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.
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.
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.
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.
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.
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 ...
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.)
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 '/'.
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());
use single qutes for this like Split('\t'), this way you will be passing a char and not a string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With