1

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("?","?");
3
  • 1
    HttpUtility.HtmlDecode? Or WebUtility.HtmlDecode if it's not a web app. Commented Jul 23, 2019 at 23:15
  • 1
    Note: Those are Unicode codepoint values. Nothing to do with ASCII. Commented Jul 24, 2019 at 1: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 <b>Could not find &#39;Some User&#39;.<b> <i>Are you sure this user is valid&#63;</i>. Check the API documentation. Commented Jul 24, 2019 at 1:34

1 Answer 1

4

you can use Html Encode/Decode

using System.Net;

    string val = "Could not find &#39;Some User&#39; are you sure this user is valid&#63;";
    string decodedString = WebUtility.HtmlDecode(val);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.