how to set text length in a cell and replace a constant length

I need some help in setting string validation for example a text/interger length in a cell.

thank you in advance.

for example:

Barcode
00001
12
00123
00033
999

 I want to replace my column to be like following:

New Bar code
00001
00012
00123
00033
00999

Not sure if there is an existing node; I would probably use a Java Snippet with a DecimalFormat class like:

NumberFormat format = new DecimalFormat("00000");
System.out.println(format.format(12)); // 00012

What if i also have value like:

 

011A

0003B

 

That would involve some more code. For example:

final int DESIRED_LENGTH = 5;

String input = "011A";
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < DESIRED_LENGTH - input.length(); i++) {
    buffer.append('0');
}
buffer.append(input);
String result = buffer.toString();
System.out.println(result); // 0011A

 

thank you so much. My problem is solved.

another question that i just noticed.

i have values for example 10A

the result came out to be 0001A It arranged all my 0 in the front. How to resolved this.

Thank you.

Hi,

Various alternative ideas & code snippets here:

http://stackoverflow.com/questions/17098329/add-leading-zeroes-to-a-string

Cheers
E