Click here to Skip to main content
15,920,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to split a string retrieved from db. The string looks as below. The string "details" is fetched from database, which cannot be updated. The changes can be made only in the C# side.

details = "Test\u001eA01";


Please help.

Thanks,
Divya

What I have tried:

I tried 2 methods, but both didnt work out
a) details.Split('\\');
b) details.Substring(details.IndexOf(@"\") + 1);
Posted
Updated 7-Jan-21 3:10am

This is sort of confusing, because if the actual value in the string contains the text "\u001e" then you would need to split based on details.Split(new String[] { "\\u001e" }, StringSplitOptions.None); however if the RS character is present in the string then you can just split by doing details.Split('\u001e');
 
Share this answer
 
Just to add to what Chris said: when you use a '\' followed by a 'u' in a string in C#, it expects the hexadecimal code (normally 4 digits) for a specific Unicode value to follow it, and the whole value \uNNNN will comprise a single Unicode character in the string.
So if you writhe this:
string details = "Test\u001eA01";
Console.WriteLine($"length: {details.Length}");
It will print "8", being the four characters "Test", the three characters "A01", and the single Unicode character '\u001E' - which is a special code called "RS" or "Record Separator" and is a legacy from the early ASCII based days of computing, and was used to separate rows in a datastream.
If you look at the content of details in the Visual Studio debugger, you will see the string as you typed it, but when printed:
Console.WriteLine($"data: {details}");
You would get:
data: TestA01
Or something similar, as the console (and textboxes!) does not support controls codes other than CR, NL, and TAB: '\r', '\n', and '\t'
 
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