Using String.Replace the string remains the same

5

I have the following string :

string tt= "{\"Response\":{\"StatusCode\":200,\"StatusMessage\":\"OK\",\"Content\":{\"family\":{\"codigo\":14,\"descricao\":\"Cal\u00e7a\",\"frontoffice\":1,\"posicaofront\":31,\"posicaoprint\":26,\"fundo\":\"#c0c0c0\",\"letra\":\"#000000\",\"tipo\":0,\"loja\":14,\"subfamilies\":[{\"codigo\":60005,\"descricao\":\"Ganga\",\"familia\":14,\"fundo\":\"#c0c0c0\",\"letra\":\"#000000\",\"posicao\":0,\"loja\":\"14\",\"lastupdate\":\"2015-12-04 16:51:26\"}],\"lastupdate\":\"\",\"famzonas\":[{\"loja\":14,\"familia\":14,\"zona\":0}]}}}}";

And how much do I try to give Replace() in "\ u00e7" to string stays the same:

tt.Replace("\u00e7", "c");

After executing this command the output remains the same:

{"Response":{"StatusCode":200,"StatusMessage":"OK","Content":{"family":{"codigo":14,"descricao":"Cal\u00e7a","frontoffice":1,"posicaofront":31,"posicaoprint":26,"fundo":"#c0c0c0","letra":"#000000","tipo":0,"loja":14,"subfamilies":[{"codigo":60005,"descricao":"Ganga","familia":14,"fundo":"#c0c0c0","letra":"#000000","posicao":0,"loja":"14","lastupdate":"2015-12-04 16:51:26"}],"lastupdate":"","famzonas":[{"loja":14,"familia":14,"zona":0}]}}}}

How can I change the "\ u00e7" to the "c" character?

    
asked by anonymous 17.02.2017 / 14:36

2 answers

7

Simple:

tt = tt.Replace("\u00e7", "c");

Variables are immutable , so when you use the method, it does not change the string itself , it generates a new one and returns you to do what you want with it. You can use it in another expression or save it to a variable. It can be a new variable or it can be the same, so the new value replaces the old value. So any operation on string that you can avoid or minimize is advantageous.

The documentation shows this.

There is one more problem. By coincidence, the piece of text you want to change is a specially entered character format. \u means that the following code is a Unicode code for the character you want. Then there is an impedance in the representation. It does not see this character set u00e7 , it sees as ç . It is also easy to solve this. Use a string verbatim where each character is interpreted as being there, it ignores the representations special.

using static System.Console;

public class Program {
    public static void Main() {
        var tt= "{\"Response\":{\"StatusCode\":200,\"StatusMessage\":\"OK\",\"Content\":{\"family\":{\"codigo\":14,\"descricao\":\"Cal\u00e7a\",\"frontoffice\":1,\"posicaofront\":31,\"posicaoprint\":26,\"fundo\":\"#c0c0c0\",\"letra\":\"#000000\",\"tipo\":0,\"loja\":14,\"subfamilies\":[{\"codigo\":60005,\"descricao\":\"Ganga\",\"familia\":14,\"fundo\":\"#c0c0c0\",\"letra\":\"#000000\",\"posicao\":0,\"loja\":\"14\",\"lastupdate\":\"2015-12-04 16:51:26\"}],\"lastupdate\":\"\",\"famzonas\":[{\"loja\":14,\"familia\":14,\"zona\":0}]}}}}";
        tt = tt.Replace(@"\u00e7", "c");
        WriteLine(tt);
    }
}

See running on .NET Fiddle . And at Coding Ground . Also put it on GitHub for future reference .

    
17.02.2017 / 14:41
2

SIMPLE SOLUTION

Use @

tt = tt.Replace(@"\u00e7", "c");
    
17.02.2017 / 14:49