How to add leading zeros to a JTextfied (string type attribute)

0

Hello, I have a series of codes that contain positive integers that I want to print making them occupy the same space.

For example given:

  

j1

     

j23

     

j777

I would like you to have zeros to the left and chains of the type to come out:

  

j0001

     

j0023

     

j0777

It should be noted that the j is static, I am interested in filling in the spaces between the numbers and the letters.

    
asked by Marwin José Yepez Vargas 10.05.2018 в 23:21
source

1 answer

0

Try the following code:

//Letra a convertir
String letra = "j777";
//Se divide por la letra deseada "j"
String[] letras = letra.split("j");
//Se declara el formato al que se desea convertir
DecimalFormat format = new DecimalFormat("0000");
//Se aplica el formato a la segunda parte del arreglo y se concatena la 'j'
String letraFin = "j"+format.format(Integer.valueOf(letras[1]));
//Se imprime el resultado de la conversión
System.out.println("Formato final: "+ letraFin);

Console output:

  

Final format: j0777

    
answered by 10.05.2018 в 23:32