Using "String.Replace()" the string remains the same

Asked

Viewed 252 times

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 I try to give Replace() in the " u00e7" to string looks 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 do I change the " u00e7" to the "c" character"?

2 answers

9


Simple:

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

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

To documentation shows that.

There is one more problem. By coincidence the text snippet you want to exchange is a special character format entered. The \umeans that the following code is a Unicode code for the desired character. Then there is an impedance in the representation. He doesn’t see this set of characters u00e7, he sees as the ç. It’s also easy to fix it. Use a string Verbatim where each character is interpreted as it is there, it ignores the special representations.

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);
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • I understood what I was doing wrong, about the second problem before I saw your Dit I had put the code in the following way and it worked <code>tt = tt. Replace(" u00e7", "c"); </code>

  • 1

    @Strange medium, not supposed to work no, see: https://dotnetfiddle.net/Q8rvS5

  • the code I had commented on was poorly edited and that’s how it worked using the " " before the "u00e7"

  • 1

    @Medium true, I had not attacked me that you used the \\ which also solves.

2

SIMPLE SOLUTION

Use the @

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.