cancel
Showing results for 
Search instead for 
Did you mean: 

calculate the length of field

Former Member
0 Kudos

Hi Friends,

Its an IDOC to File interface. For a particular field coming from IDOC I need to calculate the length. The actual length I need to send is only 8 . In XI is there any UDF for calculating the length of that particular field?

I need the calculations as follows: if length of field is less than 8, need to send a length of 8 starting from the right with zeros padded on the left and equal to 9 or 10 then only last 8 need to be considered.

Thanks in advance,

Regards,

Meghna.

Accepted Solutions (1)

Accepted Solutions (1)

Jitendra_Jeswan
Contributor
0 Kudos

public String appendZero(String a,Container container)

// code start

int length = a.length();

if ( length > 8 )

return(a.substring((a.length()-8),a.length()));

else

for (int i=length;i<8;i++)

{

a="0"+a;

}

return a;

// code ends

input1 : ABC

Result : 00000ABC

input2 : ABC123456

Result: BC123456

Former Member
0 Kudos

Hi Jeet,

The code given by you is very helpful, thankyou so much for your great help.

Regards,

Meghna.

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi Meghna,

This would require some simple java Coding

let us assume the field is a

int diff;

String delimter = "0".

diff = a.length - 8.

if ( diff = 0 ){

// do nothing

}

elseif ( diff > 0 ) {

a.substring( start index , end index ) would be your answer

}

Elseif ( diff < 0 ){

for ( int i = 0 ; i <= diff ; i ++ ) {

delimter = delimeter.concat("0").

}

delimeter.concat(a) would be your String.

Put this in an UDF execute yourmapping

award points if useful

Regards

Abhishek

Former Member
0 Kudos

Hi,

You could do this with UDF

See below code

Source field --->> Test UDF ---> Target field

Use the A as Source field and cache parameter as value.

try

{

If(A.length() >= 😎

return(A.substring((a.length()-8),a.length()));

else

{

for(int i=0; i<(A.length()-8); i++)

A = A.concat("0");

return(A.substring((a.length()-8),a.length()));

}

}catch(Exception e){}

Thanks

Swarup

Former Member
0 Kudos

Hi Swarup,

Thankyou for responding so fast, can you please send the whole Java code as per my requirement.

Regards,

Meghna

Former Member
0 Kudos

Hi,

the above mentioned code is just have to copy paste in your UDF editor.

This is the complete code as per your requirment.

If still you have some other things to be consider please let me know.

Is there any multiple occurance with the Target field then probably you need some changes in the coding. Let me know about it.

Otherwise above code is fine. Its the compact way of coding UDF...:-)

Thanks

Swarup