Home > OS >  Format 00001 number
Format 00001 number

Time:06-27

For example, I get the input number LP00001, LP00023, LP00010 in text format. How can I get a number (1, 23, 10) based on the data received, add one, and form, for example, LP00002, LP00024, LP00011?

System.out.printf("%sd%n", "LP", i);

it suits me, I don't understand how I can extract the number itself

I need a number that is for example in LP00032, that is, in fact I should get 32

CodePudding user response:

If LP is a pattern (always 2 characters at the begining of the string), you could do this:

String text = "LP00001";
int numericValue = Integer.parseInt(text.substring(2))   1;
System.out.printf("%sd%n", "LP", numericValue);

CodePudding user response:

You could use formatted io. A format string with %s to indicate a String for the "LP" component. And then d to indicate five decimals zero filled. For example,

for (int i = 0; i < 10; i  ) {
    System.out.printf("%sd%n", "LP", i);
}

Outputs

LP00000
LP00001
LP00002
LP00003
LP00004
LP00005
LP00006
LP00007
LP00008
LP00009
  • Related