I need to be able to eliminate the first and also the last sequence of repeated zeros of a String
in Java
For example I have the following String
00000000110110110011100000
And I need to transform it to
1101101100111
I need to be able to eliminate the first and also the last sequence of repeated zeros of a String
in Java
For example I have the following String
00000000110110110011100000
And I need to transform it to
1101101100111
Just to add one more answer, and as an example of how this can be done in a single line, since we are talking about a string, it is best to use the functions that already exist for string to solve this case.
The trim function removes spaces back and forth .. but here we do not have spaces, we have 0. But what happens if we change those 0 for spaces?
String s = "00000000110110110011100000";
String r = s.replace("0"," ").trim().replace(" ","0");
In this case, r will contain exactly the string you ask for.
The location of the first digit 1
:
cadena.indexOf('1');
The location of the last digit 1
:
cadena.lastIndexOf('1');
Using both results, you can create a method that does what you expect.
public static String trimZeros(String s) {
String resultado = "";
int indiceMenor = s.indexOf('1');
//si indiceMenor es menor a 0 entonces la cadena s está compuesta solo por "0"s.
if (indiceMenor >= 0) {
int indiceMayor = s.lastIndexOf('1');
//solo existe 1 "1"
if (indiceMayor == indiceMenor) {
resultado = "1";
} else {
resultado = s.substring(indiceMenor, indiceMayor+1);
}
}
return resultado;
}
Demonstration:
public static void main(String[] args) {
System.out.println(trimZeros("00000"));
System.out.println(trimZeros("00100"));
System.out.println(trimZeros("01010"));
}
Exit
<-- en blanco porque removió todos los 0s
1
101
Use this little function, you send string and character that you want to clean:
public static String eliminaCaracterIzqDer(String cad, char cadEliminar){
String[] acad=cad.split("");
int posL = 0,posR = acad.length;
for(int i=0;i<acad.length;i++){
if(!acad[i].equals(Character.toString(cadEliminar))){posL=i;break;}
}
for(int i=acad.length;i>0;i--){
if(!acad[i-1].equals(Character.toString(cadEliminar))){posR=i;break;}
}
return cad.substring(posL,posR);
}
You invoke it:
System.out.println(eliminaCaracterIzqDer("000001254780900",'0'));
Result: 12547809
As an option, you can use a StringBuilder and methods delete () to remove sequences of zeros at the beginning and at the end:
StringBuilder str = new StringBuilder("00000000110110110011100000");
int start = str.indexOf("1");
str.delete(0, start); //Elimina ceros al inicio.
int end = str.lastIndexOf("1")+1;
str.delete(end, str.length()); //Elimina ceros al final.
System.out.println("Resultado : " + str);
Resulting in:
Resultado : 1101101100111
But, What would happen if you do not have values of 1 in your String? , for this here you have an improved version of the above that meets all cases:
StringBuilder str = new StringBuilder("0000000000000");
int start = str.indexOf("1");
if(start>-1){ //Valida si encuentra primera coincidencia de 1
str.delete(0, start);
}
int end = str.lastIndexOf("1");
if(end>-1){ //Valida si encuentra última coincidencia de 1
str.delete(end + 1, str.length());
}
if(start <0 && end <0 ){ //no encontro valores de 1
str.delete(0, str.length()); //Elimina todos los 0
}
Resulting in:
Resultado :
I add a method that meets all the cases and you can use it in your program:
private static String eliminaCeros(String valor){
StringBuilder str = new StringBuilder(valor);
int start = str.indexOf("1");
if(start>-1){ //Valida si encuentra primera coincidencia de 1
str.delete(0, start);
}
int end = str.lastIndexOf("1");
if(end>-1){ //Valida si encuentra última coincidencia de 1
str.delete(end + 1, str.length());
}
if(start <0 && end <0 ){ //no encontro valores de 1
str.delete(0, str.length());
}
return str.toString();
}
Also a online example to try the method.
Another option is commented by gbianchi that I think is excellent and shorter.
00000000110110110011100000
: 11 11 11 111
.trim()
method: 11 11 11 111
1101101100111
This would be a method with this example:
public static String validaNumeros(String valor){
return valor.replace("0"," ").trim().replace(" ","0");
}