Remove or Convert ' to (')

前端 未结 3 1332
粉色の甜心
粉色の甜心 2021-01-18 00:16

I am consuming an api and I noticed that it comes back with \"'s\" and not an apostrophe. Since I am not going to be displaying this text in html this w

相关标签:
3条回答
  • 2021-01-18 00:45

    Why aren't you uysing String.Replace Method (String, String) for this purpose. Just find your string and replace it with your required one.

    string myStringToDecode = "Hello 'World'";
    myStringToDecode.Replace("'","'");
    
    0 讨论(0)
  • 2021-01-18 00:46

    As of .NET 4.0 you can use System.Web.HttpUtility.HtmlDecode (resides in the System.Web.dll assembly, in the namespace System.Web).

    Or you could use the System.Net.WebUtility.HtmlDecode function, you don't even need an extra reference for this (because it resides in the System.dll assembly, in the namespace System.Net).

    Usage:

    string myStringToDecode = "Hello 'World'";
    
    string decodedString = System.Web.HttpUtility.HtmlDecode(myStringToDecode);
    // or
    string decodedString = System.Net.WebUtility.HtmlDecode(myStringToDecode);
    
    0 讨论(0)
  • 2021-01-18 00:58

    The MatchEveluator is available since .NET 1.0 and comes in handy if you want to address this problem in a more generic fashion - i.e. do more than just HTML character decoding. The general recipe looks like this:

    MatchEvaluator ev = (Match m) => Char.ToString((char)Int32.Parse(m.Groups[1].Value));
    string result = Regex.Replace("The king's key.";, @"&#(\d+);", ev);
    
    0 讨论(0)
提交回复
热议问题