0

I need to created a method which validates if a string contains an escaped unicode character. First thing that passed through my mind was to convert the string to char[] in order to get each element and then just to extract the "\uxxxx" from there. It didn't work. Here is a little thing:

public static string Quoted(string text)
            => $"\"{text}\"";

string inputData = Quoted(@"a \u26Be b");

Console.WriteLine(inputData) // ==> output will be: "a \u26Be b"

So there is obviously an large unicode character. I feel those quotes make my work harder. Also, I'm a begginer who just passed the alghoritm part of learning, now I'm learning and working with .Json, github, gitbash, etc. This task is one unit test in order to validate .Json file. Any help or hints would be appreciated. Thanks in advance!

  • See here, does this help? https://stackoverflow.com/questions/4459571/how-to-recognize-if-a-string-contains-unicode-chars – daveBM Sep 26 '22 at 20:03
  • Did you try the solution you described in text? If yes, what was the problem with it? – Xerillio Sep 26 '22 at 20:04
  • 1
    @daveBM I think it's different as OP asks for recognizing **escaped** unicode characters. – Xerillio Sep 26 '22 at 20:04
  • @Xerillio,the output is "a \u26Be b" which has 12 length. As you know, in order to convert to char you need 1 element. I found that if an character is greater than 127 (ASCII code for "delete") is an unicode character. If I'm wrong, please tell me. So, from that 12 length string i need to get only \u26Be. The unicode \u26Be is one element, but I can't extract just this element from the string – andreivarga Sep 26 '22 at 20:11
  • Adding the at symbol (@) before the string avoid the escape sequences being processed… See https://stackoverflow.com/questions/17280482/unicode-literal-string so I'd guess that `Quoted("a \u26Be b")` could work… – JosefZ Sep 26 '22 at 20:15
  • @JosefZ, I think that my teacher put that @ with purpose, maybe to make me research. You're right, now I can get that unicode. It's @ job to do what you just described there? Or is just a fact and if there's something before the string that makes him to avoid escape sequences being processed? As a solution, I will remove that @ from there and keep going. Thank you for your answer, it was very helpfull. – andreivarga Sep 26 '22 at 20:23
  • Sorry, I have a smattering of C# however [this answer](https://stackoverflow.com/a/32463647/3439404) could help (there must be a C# equivalent as PowerShell is a kind of dotNET implementation). – JosefZ Sep 26 '22 at 20:38
  • https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/verbatim – Xiang Wei Huang Sep 27 '22 at 02:22

0 Answers0