cancel
Showing results for 
Search instead for 
Did you mean: 

Splitting filed

Former Member
0 Kudos

Hi Experts,

I have one source, two target fields.

logic:

if source value length < 40, map source to targetone , send empty string to targettwo

else if (source value length > 40), map subString(0,40) to targetone, remaining value to targettwo

else pass empty string to both target fields.

I want to try UDF for this requirement, Please help me with the code.

Accepted Solutions (1)

Accepted Solutions (1)

RaghuVamseedhar
Active Contributor
0 Kudos

Mohan,

Please use UDF execution type 'All values of a context'.


    public void udf_split(String[] in, ResultList out1, ResultList out2, Container container) throws StreamTransformationException {

        in[0] = in[0] + "                                        "; //To avoid ArrayIndexOutOfBoundsException

        out1.addValue(in[0].substring(0, 40).trim());

        out2.addValue(in[0].substring(40).trim());

    }

Former Member
0 Kudos

Thanks Raghu.

This code is not working if source value is less than 40 length.

Below is error

Exception:[java.lang.StringIndexOutOfBoundsException: String index out of range: 80] in class.

former_member186851
Active Contributor
0 Kudos

Did you try my logic Mohan?

Former Member
0 Kudos

Not yet Raghu. I have similar kind of requirement for most of the fields in different interfaces.

so thought achieving this requirement throw UDF instead of graphical map.

markangelo_dihiansan
Active Contributor
0 Kudos

Hi Mohan,

Here is a modified UDF of Raghu

Regards,

Mark

Former Member
0 Kudos

Thank you Mark. It's working

Answers (3)

Answers (3)

pvishnuvardan_reddy
Active Contributor
0 Kudos

Hi Mohan,

Pls try the below udf and check the outcome:

Code snippet:

if(input[0].length()<=40)

{

    output1.addValue(input[0]);

     output2.addValue("");

}

else if(input[0].length()>40)

{

    output1.addValue(input[0].substring(0,40));

    output2.addValue(input[0].substring(40));

}

else

{

    output1.addValue("");

    output2.addValue("");

}

former_member257758
Participant
0 Kudos

Hi Mohan,

I hope the above examples helped you. But in addition to this, What if the source string length is 200 char or more? you just need take remaining 120 char (after 40 char) and populate target2 field? or you need take 40 char a time and then keep creating the Target2 filed until string is completed. If this is the case , use the below UDF. Use Execution Type as All values of Queue.

public void Nodesplit(String[] input, ResultList result, Container container) throws StreamTransformationException{

String str=input[0];

while(str.length()>40)

{

result.addValue(str.substring(0,40));

str=str.substring(5,str.length());

}

result.addValue(str);

}

With this UDF you can split the source value to multiple Target field, In Case remaining source values need to be copied to target fields (you want repeat the target structure based source string length which is more than 40 char), You can use the nodefunction UseOneAsMany and SplitByValue (each Value) combination.

Hope this helps too..

former_member186851
Active Contributor
0 Kudos

Hello Mohan,

Try the below Logic,

For Target Field 1:

For Targe Field 2:

UDF1:

int L=var1.length();

var1=var1.substring(40,L);

return var1;

Let me know if you wish to add more logic in this.