How to remove field to field from a String starting from left and bounded by a size desired by the user

Asked

Viewed 33 times

-1

package teste;

public class RemoverStringAEsquerda {
    
    public static String removerCampoAEsquerda(String value, int tamanhoMaximoString) {
        int i = 0;
        while (value.length() >= tamanhoMaximoString){
            value = value.substring(i);
            i++;
        }
        
        return value;
    }
    
    public static void main(String[] args) {
        
        String campo = "0010004500000000023456841466512";
        campo = removerCampoAEsquerda(campo, 14);
        System.out.println(campo);
    
    }

}

Expected result: 23456841466512

Or if the desired String size is 5: removerCampoAEsquerda(campo, 5) - the expected result is 66512

1 answer

0

Your solution is not ideal, but first we will fix your code, and then we will see a simpler solution.


The problem is that you increase the i. That is, in the first iteration, you take substring(0) (that is not very "smart", since so you get the whole string).

Then in the second iteration, i is worth 1, then substring(1) take from the second character onwards. That is, the first character has been deleted.

In the third iteration, i worth 2, then substring(2) taken from the third character onwards. But remember that the first character of the original string has already been deleted, that is, the third character here equals the fourth character of the original string.

In the fourth iteration, i is worth 3 then substring(3) taken from the fourth character onwards. But we have seen that the previous step eliminated the first 4 characters, so the fourth character here equals the eighth character of the original string.

And so on... With each iteration, you’re eliminating several more characters.

If the idea was to make one loop, then always delete the first character:

public static String removerCampoAEsquerda(String value, int tamanhoMaximoString) {
    while (value.length() > tamanhoMaximoString) { // se o tamanho for menor ou igual ao máximo, nem entra no while
        value = value.substring(1); // sempre pegue do segundo caractere em diante
    }
    return value;
}

But as I said at the beginning, this solution is far from the best. There is no reason to keep creating several strings for nothing (each call from substring creates a new string). Actually what you want is to take the last characters of the string, so you just need to call substring once:

public static String ultimosCaracteres(String value, int tamanhoMaximo) {
    if (value.length() <= tamanhoMaximo) { // se o tamanho for menor ou igual ao máximo, não precisa modificar a string
        return value;
    }
    // pega os últimos caracteres da string
    return value.substring(value.length() - tamanhoMaximo);
}

Testing:

String campo = "0010004500000000023456841466512";
System.out.println(ultimosCaracteres(campo, 14)); // 23456841466512
System.out.println(ultimosCaracteres(campo, 5));  // 66512
  • Now I understood how it should proceed and tbm in where was sinning in the code, vlw even hkotsudo

  • @Etgames13 If the answer solved your problem, you can accept it, see here how and why to do it. It is not mandatory, but it is a good practice of the site, to indicate to future visitors that it solved the problem. And when I get 15 points, you can also vote in all the answers you found useful.

Browser other questions tagged

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