cancel
Showing results for 
Search instead for 
Did you mean: 

i have a request to add spaces or padding zeros if no values are passing

former_member606938
Discoverer
0 Kudos

Hi Experts.

Could you help me I have a requirement to add the padding zeroes if no values are coming from the source elements.

the length of element values are 10 with alphanumeric characters.

Could anyone help me with the udf code.

Accepted Solutions (0)

Answers (2)

Answers (2)

sugata_bagchi2
Active Contributor
0 Kudos

Try with this one - it will add lead or trail zeros as pert the argument specified -

if (pos[0].equals("LEAD"))
   {          for (int i =0;i<vals.length;i++) 
            {
             //  boolean isAlphaNumeric = vals[i] != null &&  vals[i].chars().allMatch(Character::isLetterOrDigit);  requires JDK >=1.8
              
              if (vals[i].matches(".*[A-Za-z].*") && vals[i].matches(".*[0-9].*") && vals[i].matches("[A-Za-z0-9]*"))
                {
                     rs.addValue(vals[i]);
                }
               else 
                   {
                      while (vals[i].length() < len[0])
                       vals[i] = pad[0]+ vals[i];
                   }
                  rs.addValue(vals[i]);
                }
      }

if (pos[0].equals("TRAIL"))
   {          for (int i =0;i<vals.length;i++) 
               {
             if (vals[i].matches(".*[A-Za-z].*") && vals[i].matches(".*[0-9].*") && vals[i].matches("[A-Za-z0-9]*"))
                {
                     rs.addValue(vals[i]);
                }
               else 
                   {
                 // rs.addValue(String.format("%0$-"+len[0]+, s));
                      rs.addValue(String.format("%1$-" +len[0] + "s",vals[i]).replace(' ', '0'));
                   }
                } 
      }
former_member607993
Contributor
0 Kudos

Hi Rajesh,

If there is no value populating from the source fields you can use 'map with default' and assign the zero(00000000000) appropriately.