Empty char works only sometimes

Hi, im a newcomer to arduino, and I'm trying to empty some characters in a string in order to get only the desired value and then sotre it in a constant. Here is the code

else if (comandoGi.startsWith("G0", 0)) {
        String copiaCmdGX = comandoGi;  // cmdGD = G01X???Y???D??*
        String copiaCmdGY = comandoGi;  // cmdGD = G01X???Y???D??*
        String copiaCmdGD = comandoGi;  // cmdGD = G01X???Y???D??*

        const int auxVarX = comandoGi.indexOf('X');
        const int auxVarY = comandoGi.indexOf('Y');
        const int auxVarD = comandoGi.indexOf('D');
        const int auxVarA = comandoGi.indexOf('*');

        lcd.setCursor(0, 0);
        lcd.print("Mover caneta G01");
        lcd.setCursor(0, 1);
        lcd.print("                ");

        ehMoverCaneta = true;
        ehTracado = false;
        ehFuro = false;
        cmdG = 1; //O comando G é G01!


        // X ---> WORKING!!!
        for (int i = auxVarY; i <= auxVarA; i++) { //cmdGX = G01X???Y???D??*
          copiaCmdGX.setCharAt(i, "");    // cmdGX = G01X???
        }
        copiaCmdGX.replace("G01X", "");  // cmdGX = ???-->PRONTO!

        cmdX = copiaCmdGX.toInt();


        //Y --->NOT WORKING!!!
        for (int j = auxVarX; j <= auxVarY; j++) {
          copiaCmdGY.setCharAt(j, "");      //cmdGXY = G01???D??*
        }
        for (int k = auxVarD; k <= auxVarA; k++) {
          copiaCmdGY.setCharAt(k, "");        //cmdGXY = G01???
        }
        copiaCmdGY.replace("G01", "");
        cmdY = copiaCmdGY.toInt();



        //D --->NOT WORKING!!!
        for (int g = auxVarX; g <= auxVarD; g++) {
          copiaCmdGD.setCharAt(g, "");      //cmdGXY = G01??*
        }
        copiaCmdGD.replace("*", "");
        copiaCmdGD.replace("G01", "");

        cmdDii = copiaCmdGD.toInt();

        comandoGi2 = "X=";
        comandoGi2 += cmdX;
        comandoGi2 += "Y=";
        comandoGi2 += cmdY;

        lcd.setCursor(0, 1);
        lcd.print(comandoGi2);
        Serial.write("recebido");
      }

but when it is displayed at LCD, only X is a number, and Y is always 0, why??I am doing the same thing with the strings but only with X works! How can I fix that?

Please specify what type of LCD you are using....I would suggest that you would use toCharArray() and then modify the string in form of a char array....

Using "String" variables is extremely unwise and liable to cause "mysterious" crashing.

Use character arrays.