cancel
Showing results for 
Search instead for 
Did you mean: 

Help regarding udf

Former Member
0 Kudos

Hi Experts ,

I have a requirement in which I have to pad an incoming string with spaces based on it's length so that it's size ultimately become 10 character...can anyone give me a udf code for the same.I have tried myself but it didn't work

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Deepak,

Try this

1. calculate the length of the string(str) = len

2.

while (len <= 10)

{

a) append a space to the string

b) len = str.len

}

Regards,

Akshay

Former Member
0 Kudos

logically this is very simple ...but i do not know java ...

so was finding it difficult to code ...was getting some error after another

Former Member
0 Kudos

I want leading spaces not zeroes ...

Former Member
0 Kudos

Hi Deepak,

Try this

int len = str.length();

while (len <= 10)

{

str = str + " ";

len = str.length();

}

result.addValue(str);

Regards,

Akshay

Former Member
0 Kudos

Hi ,

Got the following when I tried this code

usr/sap/JXS/DVEBMGS00/j2ee/cluster/server0/./temp/classpath_resolver/Maped3f5b80b6e511dba9a0001125a6778d/source/com/sap/xi/tf/_MM_Inbound_Delv_.java:888: cannot resolve symbol symbol : variable result location: class com.sap.xi.tf._MM_Inbound_Delv_ result.addValue(str); ^

Former Member
0 Kudos

Hi Deepak,

Ok remove the result.addValue st and put

return str;

instead of it.

Try it now....

Regards,

Akshay

Former Member
0 Kudos

Deepak,

understand that the 'return' statement is when you choose the Radio button 'Cache'

as 'Value'

and it is 'result.addValue()' when you choose Radio button 'Cache' as 'Context'

Ranjit.

Former Member
0 Kudos

Hi ,

This code is not executing ..and it is taking forever ...

int len = str.length();

while (len <= 10)

{

str = str + " ";

}

return str;

Former Member
0 Kudos

Hi Deepak,

for sure it is an infinite loop, you have to recompute the value of the variable len inside the loop or do in this way:

while (str.length() <= 10)
  str = str + " ";

result.addValue(str);
//return str; *** depending on the type of function

without the need to declare a variable for string lenght.

Regards,

Sergio

Former Member
0 Kudos

Hi Deepak,

You need to reassign the len variable in the while loop with the new length. You missed one stmt !!!!

Regards,

Akshay

ranjit_deshmukh
Active Participant
0 Kudos

Deepak,

try this:

while (len <= 10)

{

str = str + " ";

len++;

}

return str;

this will do

Ranjit

Former Member
0 Kudos

Thanks akhshay ..i hope under no circumstances the length

will exceed 10 ...I have fixed file length on the other end

Former Member
0 Kudos

<i>>>>i hope under no circumstances the length will exceed 10</i>

You can guard against this by putting in another statement at the beginning:

if (str.length() > 10)
    return str.substring(0,10);

... rest of your original code

Regards,

Jin

Answers (2)

Answers (2)

Former Member
0 Kudos

Deepak,

Just create a UDF and click on <b>Value</b> radio button. In the arguments name the Argument as <b>str</b>.

Then inside you put as given by sergio:

while (str.length() <= 10)

str = str + " ";

return str;

--> Save and close.

Then it will work.

---Satish

Former Member
0 Kudos

Deepak,

This function will resolve your issue:

For e.g. if a string has to be padded with leading zeros to make it 10 character long.

while (String.length()<Integer.parseInt(Length))

{

String = Value+String;

}

return String;

---Satish