0

I am receive a string of data from an API where all special characters are represented as there ASCII values. What is the best, in terms of speed and accuracy, way to detect and replace these?

Source String:

    "Could not find 'Some User' are you sure this user is valid?"

Desired Result:

    "Could not find 'Some User' are you sure this user is valid?"

I am currently using the string replace function, but that will look very ugly if I have to do it for every single special character.

string correctedErrorMessage = originalErrorMessage.Replace("'", "'").Replace("?","?");
Kevin Adams
  • 123
  • 7
  • 1
    [`HttpUtility.HtmlDecode`](https://learn.microsoft.com/en-us/dotnet/api/system.web.httputility.htmldecode)? Or [`WebUtility.HtmlDecode`](https://learn.microsoft.com/en-us/dotnet/api/system.net.webutility.htmldecode) if it's not a web app. – 41686d6564 stands w. Palestine Jul 23 '19 at 23:15
  • 1
    Note: Those are Unicode codepoint values. Nothing to do with ASCII. – Tom Blodget Jul 24 '19 at 01:30
  • 1
    Also note: Given that the text is an HTML or XML value, could it be that the API might give you are a more complex result that you'd want to deal with in more detail, such as just the text without the markup? The HTTP response header Content-Type would give more information about the intent. Consider if you got `Could not find 'Some User'. Are you sure this user is valid?`. Check the API documentation. – Tom Blodget Jul 24 '19 at 01:34

1 Answers1

3

you can use Html Encode/Decode

using System.Net;

    string val = "Could not find 'Some User' are you sure this user is valid?";
    string decodedString = WebUtility.HtmlDecode(val);