简体   繁体   中英

Why/how function string.Substring treats "\u0002" as a one sign? Unicode chars in C#

Why/how function string.Substring treats "\" as a one sign? I mean ok, "\" is a "character" STX.

  1. \\u says that is unicode
  2. Character and string processing in C# uses Unicode encoding. The char type represents a UTF-16 code unit, and the string type represents a sequence of UTF-16 code units.

Code checks if preffix suffics is correct. Data length does not matter. Prefix is STX , suffix is ETX added do data string. How to do this(code below) explicitly without a doubt?

    string stx = "\u0002";
    string etx = "\u0003";
    string ReceivedData= stx + "1122334455" + etx;
    
    string prefix = ReceivedData.Substring(0, 1);
    string suffix = ReceivedData.Substring(ReceivedData.Length - 1, 1);

Do you wonder the working mechanism of UTF-16 and Unicode? May this topic helps: What is Unicode, UTF-8, UTF-16?

The code snippet looks reasonable as the variables are explicitly named and '\\u\u0026#39; is a sign of Unicode.

string stx = "\u0002";
string etx = "\u0003";

string prefix = ReceivedData.Substring(0, 1);
string suffix = ReceivedData.Substring(ReceivedData.Length - 1, 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM