0

I want to remove all the escape sequences from the below string which I get from Pokemon API.

"A strange seed was\nplanted on its\nback at birth.\fThe plant sprouts\nand grows with\nthis POKéMON."

I have used Regex.Unescape("A strange seed was\nplanted on its\nback at birth.\fThe plant sprouts\nand grows with\nthis POKéMON.") but I am getting the same string.

Output enter image description here

Could you please help me to sort this out?

I do not want to do a string replace as escaping sequence might be different in the next API call.

Ash
  • 447
  • 2
  • 6
  • 19
  • 3
    Your original string doesn't contain any escaped characters. It contains newline characters, which are escaped in the *string literal* as `\n`, and then escaped *by the debugger*. But the string doesn't actually contain any backslashes. – Jon Skeet Aug 18 '21 at 06:19
  • Thanks for your comment. this line break actually breaks my Uri.EscapeUriString(). Basically, I need to send this string to https://funtranslations.com/api/shakespeare#translate as a query string parameter. Uri.EscapeUriString(). I get below result A%20strange%20seed%20was%0Aplanted%20on%20its%0Aback%20at%20birth.%0CThe%20plant%20sprouts%0Aand%20grows%20with%0Athis%20POK%C3%A9MON. Unfortunately I get an error with this. If I remove the \n and \f before uri encoding, then I was able to get the result what I want. – Ash Aug 18 '21 at 06:38
  • 1
    Right, so remove those... but don't expect an Unescape method to do anything when you have no escape sequences. (You wouldn't want to unescape them anyway by the sounds of it - it sounds like you want to *remove* the characters, which isn't the same thing.) – Jon Skeet Aug 18 '21 at 06:44
  • _"I get below result"_ - What's wrong with that? What result do you expect? – ProgrammingLlama Aug 18 '21 at 06:46
  • ah ok, is it that means I have to manually remove all of them using string.Replace()? – Ash Aug 18 '21 at 06:47
  • Yes, if you want to remove those characters you'll have to replace them. – ProgrammingLlama Aug 18 '21 at 06:47

2 Answers2

4

Of course you're getting the same string - there are no escaped characters in your example :)

The string literal has escape characters, but those aren't present in the string itself. When the immediate window does the printout of the result, it shows it as a string literal itself. It's not the same you would get from something like Console.WriteLine, for example.

Luaan
  • 62,244
  • 7
  • 97
  • 116
  • Thanks for your comment. this line actually breaks my Uri.EscapeUriString(). Basically, I need to send this string to funtranslations.com/api/shakespeare#translate as a query string parameter. Uri.EscapeUriString(). I get below result A%20strange%20seed%20was%0Aplanted%20on%20its%0Aback%20at%20birth.%0CThe%20plant%20sprouts%0Aand%20grows%20with%0Athis%20POK%C3%A9MON. Unfortunately I get an error with this. If I remove the \n and \f before uri encoding, then I was able to get the result that I want. – Ash Aug 18 '21 at 06:44
  • https://api.funtranslations.com/translate/shakespeare.json?text=A%20strange%20seed%20was%0Aplanted%20on%20its%0Aback%20at%20birth.%0CThe%20plant%20sprouts%0Aand%20grows%20with%0Athis%20POK%C3%A9MON. – Ash Aug 18 '21 at 06:44
0

You could use regex replace and have a set of possible escape sequences (like \n and \t etc)

regex example

(updated) example: https://rextester.com/WXSC17170

PawZaw
  • 1,033
  • 7
  • 15