cancel
Showing results for 
Search instead for 
Did you mean: 

Split string based on length

Former Member
0 Kudos

Hi ,

I have to find a function where first I need to find the length of the source field and then split the contents into two different target fields based on the lenght .

For eg : sourcefield = 40 characters

I need to first check the length = 40 and then split into two fields target1 & target2 20 characters each.

If the source field is 30 characters then i need to split into 15 characters each.

I know there is a substring function to split but I do not know how to dynamically split by first getting the length.

Thanks,

Teresa

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

if u r one PI7.1 then chk this:

execution type: all values of a context.

Input will be var1 and add 2 resultlist result1 and result2...


int a = var1[0].length();
String a1 =var1[0].substring(0,a/2);
String b1 = var1[0].substring(a/2);
result1.addValue(a1);
result2.addValue(b1);

Ur mapping will be:


                  ----------output1
Input--UDF---- 
                   ------------output2

Former Member
0 Kudos

Hi Amit,

I want to write two seperate functions as I cannot use single function to map to two different IDOC segments. So i would need one function to get the first half of the string and another function to get the second half.

How do I change your code?

Thanks,

Teresa

baskar_gopalakrishnan2
Active Contributor
0 Kudos

PLease use my reply. That code is for two different function.

public String splitFirstValue(String input, Container container) throws StreamTransformationException{
int length = input.length();
int splitLength = length/2;
String split1 = input.substring(0, splitLength);
return split1;
}

public String splitSecondValue(String input, Container container) throws StreamTransformationException{

int length = input.length();
int splitLength = length/2;
String split2 = input.substring(splitLength);
return split2;
 
}

You dont need any import statements.

Former Member
0 Kudos

But Bhaskar Don't we need a Input argument and output result . I cannot use the function without an input and output to map the source and target fields.

I did create a function with no input and output just as you said but not able to use it . I am creating a user definited function

Thanks,

Teresa

baskar_gopalakrishnan2
Active Contributor
0 Kudos

>I did create a function with no input and output just as you said but not able to use it . I am creating a user definited function

Looks like you dont understand what I mentioned.

Just copy What I did below

public String splitFirstValue(String var1, Container container) throws StreamTransformationException{
int length = var1.length();
int splitLength = length/2;
String split1 = var1.substring(0, splitLength);
return split1;
}
 
public String splitSecondValue(String var1, Container container) throws StreamTransformationException{
 
int length = var1.length();
int splitLength = length/2;
String split2 = var1.substring(splitLength);
return split2;
 
}

source field -->splitFirstValue --> target first element

source field >splitSecondValue>target second element

Edited by: Baskar Gopal on Aug 2, 2011 12:35 PM

Former Member
0 Kudos

I got Baskar. Thank you very much.

Best Regards,

Teresa

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi ,

Try this code.

String inval;

if(String.length==40)

{

String Tar1,Tar2;

Tar1=Substring(0,19);

Tar2=Substring(20,40);

res1=resultlist.addContextChange(Tar1);

addValue(res1);

res2=resultlist.addContextChange(Tar2);

addValue(res2);

Srini

baskar_gopalakrishnan2
Active Contributor
0 Kudos

There are many ways to do this.... YOu can try UDF as below.. create a function and pass input as string.

Function 1) For the first split to target

int length = input.length();
int splitLength = length/2;
String split1 = input.substring(0, splitLength);
return split1

Function 2 ) For the second split to target

int length = input.length();
int splitLength = length/2;
String split2 = input.substring(splitLength);
return split2

Former Member
0 Kudos

Baskar,

Your code is giving errors. It does not recognize method length. Can you please specify the input and output parameters?

Thanks,

Teresa

Former Member
0 Kudos

You need to add following libraries:

java.lang.*;java.util.*

baskar_gopalakrishnan2
Active Contributor
0 Kudos

May be your method arguments are var1. I use the name input for the type string. That could be the problem. You can correct to input as I mentioned in the method signature. If you still have issues, let us know. Already verified and works for me.

Former Member
0 Kudos

Thank you . Let me try . Will let you know.