cancel
Showing results for 
Search instead for 
Did you mean: 

udf for adding spaces

Former Member
0 Kudos

Dear All,

I have a quantity field coming in which I am concatenating with date field.

I am checking a condition and then passing quantity to output and concatenate with date else concatenate a constant [space[ with date. My mapping looks like this.

quantity - trim -- if condition true --- concat [ quantity + date ]

    else

            constant{space} ----- concat [constant + date. ]

After the if else before the concatenate i want to make sure that the space and quantity of same length . For eg if i have quantity coming in as 123.50 for 1st item and no quantity in second item i need to make sure i leave equal number of spaces.

So in the output of the if then else i wrote a UDF below but does not seems to work as intended. Please advice.Thanks !!

int len=input.length();

if(input.length()<15) {

for(int i=0; i<15-len;i++) {

input = input + " " ; }

}

return input;

I want to see result like below.

quantity          date                concat

123.50          20120508         123.5020120508

[ ]                 20120508                  20120508

46.50            20120508           46.5020120508

Accepted Solutions (1)

Accepted Solutions (1)

former_member184681
Active Contributor
0 Kudos

Hi Teresa,

In fact, you don't have to loop over the string and test its length to meet your requirement. Here is the simplest code that will generate the desired output (assuming input is still your input parameter):

String output = String.format("%1$#" + 15 + "s", input);

return output;

Regards,

Greg

Answers (2)

Answers (2)

baskar_gopalakrishnan2
Active Contributor
0 Kudos

Your screenshot is not very clear to understand. But, I'm trying to understand. You just want UDF for giving spaces for the concat operation. The maximum length for the output string is 15. is that right?

// Use var1 as argument and use the below coding..

for (int count=0; count < 15 - var1.length();  count++){

   var1=  " " + var1;

}

return var1;

rajasekhar_reddy14
Active Contributor
0 Kudos

Trye below UDF,var1 argument

       StringBuffer s= new StringBuffer(var1);

       int len=s.length();

       int spaces=15-len;

       for (int i=0;i<spaces;i++)

               s.append(" ");

      

          var1 = s.toString();

     

         return var1;

Best Regards,

Raj