Search code examples
c#stringcharacter-encoding

How do I replace a special character


I'm trying to replace a special character "½" with ".5"

cell.InnerText="o208½-105u208½-109o208-110u209-110";

                    string tempStr=cell.InnerText;
           
                    if (cell.InnerText.Contains("½"))
                    {
                        cell.InnerText.Replace("½", ".5");
                    }
                    string tempStr1 = cell.InnerText;

but my C# .Replace isn't working , I get the same result.


Solution

  • String is an immutable type. Compiler creates a new string after replacing. So try just this

    var innerText = "o208½-105u208½-109o208-110u209-110";
    innerText= innerText.Replace("½", ".5");
    

    result

    before  -  o208½-105u208½-109o208-110u209-110
    after   -  o208.5-105u208.5-109o208-110u209-110