Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a string that contains \tab instead of \t. i want to split string with \tab.

I tried to split my string with \tab, but am facing "too many characters in character literal" error.

What I have tried:

string[] Record = value.Split('\tab');
Posted
Updated 27-Mar-18 21:34pm
v2
Comments
Jon McKee 28-Mar-18 3:30am    
Why? Escape sequences in string literals are standardized for a reason. If you really must (I vehemently recommend not), you can use the Split overload taking a string array. string[] Record = value.Split(new string[] {"\\tab"}, StringSplitOptions.None);

EDIT: Also the error is because '' indicates a single character but '\tab' doesn't translate to a single character. It translates to "<tab>ab".
Patrice T 28-Mar-18 3:41am    
Show a sample input and output

1 solution

'\tab' is not a character, so that will never work - a character hold just a single letter, or an escaped letter such as '\n' or '\\'

If your string is separated by TAB characters, then '\t' will work fine - that is the C# character for a TAB.

If it's separated by the sequence '\', 't', 'a', 'b', then you would need to use a string array:
C#
string[] stringSeparators = new string[] {@"\tab"};
string[] result = source.Split(stringSeparators, StringSplitOptions.None);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900