Home > Net >  How do I include a string in an escape sequence in c#
How do I include a string in an escape sequence in c#

Time:11-08

I have a string which is a combination of variables and text which looks like this:

string str = $"\x0{str2}{str3}";

As you can see I have a string escape sequence of \x, which requires 1-4 Hexadecimal characters. str2 is a hexadecimal character (e.g. D), while str3 is two decimal characters (e.g. 37). What I expect as an outcome of str = "\x0D37" is str to contain , but instead I get whitespace, as if str == "\x0". Why is that?

CodePudding user response:

As per the specification, an interpolated string is split into separate tokens before parsing. To parse the \x escape sequence, it needs to be part of the same token, which in this case it is not. And in any case, there is simply no way the interpolated part would ever use escape sequences, as that is not defined for non-literals.

You are better off just generating a char value directly, albeit this is more complex

string str = ((char) (int.Parse(str2   str3, NumberStyles.HexNumber))).ToString();

CodePudding user response:

To answer why your example doesn't work:

The spec calls \x a "hexadecimal escape sequence".

A hexadecimal escape sequence represents a single Unicode character, with the value formed by the hexadecimal number following "\x".

The grammar only permits literal hexadecimal digits to be used in this way. ANTLR grammar:

hexadecimal_escape_sequence
    : '\\x' hex_digit hex_digit? hex_digit? hex_digit?;

hex_digit
    : '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
    | 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'a' | 'b' | 'c' | 'd' | 'e' | 'f';

meaning, "the sequence \x followed immediately by one, two, three, or four hexadecimal digits". Thus, the escape can only be used as a literal in source code, not one concatenated at runtime.

Charlieface provided a good alternative for your case, though depending on the context you may want to rethink your organization and just have a single string holding the characters instead of two.

  • Related