Skip to Content

Split string in to many & control the occurence

Hi Experts,

I have requirement in my mapping to split the input string in to 30 characters each & the occurrence of target field should be controlled by number of strings obtained after splitting. Each split string obtained after splitting to be populated to the corresponding occurrence of target field. Say for example I have input string with 110 characters in the input field so 4 times target field should occur in the target XML with 30,30,30,20 characters in the each occurence of target field.

Thanks,

Ramu.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

7 Answers

  • Apr 12 at 12:22 PM

    Hi Ramu,

    Try below UDF its working fine as per your requirement :

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

    int count = input[0].length()/30;

    int index=30;

    int j=1; for(int i=0;i<=count;i++)

    { if(i==count)

    result.addValue(input[0].substring((i*index),input[0].length()));

    else

    result.addValue(input[0].substring(i*index,(30*j)));

    j=j+1;

    }

    Thanks,

    Ashish

    Add comment
    10|10000 characters needed characters exceeded

  • Apr 11 at 06:14 PM

    You can use QUEUE context from the UDF you have for splitting the string.

    Add comment
    10|10000 characters needed characters exceeded

  • Apr 12 at 06:52 AM

    Hi Experts,

    Can someone help on UDF to achieve this?

    Thanks,

    Ramu.

    Add comment
    10|10000 characters needed characters exceeded

  • Apr 12 at 11:11 AM

    Hi,

    UDF to split input string with length > 70 to that many number of strings with 70 characters each populated to target field. Please help with this UDF.

    Thanks,

    Ramu.

    Add comment
    10|10000 characters needed characters exceeded

  • Apr 12 at 11:17 AM

    Try with the below UDF.

    public void splitInputString(String[] inputData, ResultList result, Container container) throws StreamTransformationException{
    List<String> strings = new ArrayList<String>();
    int index = 0;
    while (index < inputData[0].length()) {
    strings.add(inputData[0].substring(index, Math.min(index + 30,inputData[0].length())));
    index += 30;
    }

    Iterator<String> itr = strings.iterator();
    String currentString = "";

    while(itr.hasNext()){
    currentString = itr.next();
    result.addValue(currentString);
    }
    }

    ---Priyanka
    Add comment
    10|10000 characters needed characters exceeded

  • Apr 12 at 01:21 PM

    Hi Ramu, I had a similar request and used the fragmentSingleValue from UDFNodePool. Regards

    Peter

    Add comment
    10|10000 characters needed characters exceeded

  • Apr 12 at 08:08 PM

    I would say take the length of the source string, divide by limit (30 in your case), take the ceil and input into a UDF:

    if(count[0] > 0)

    for (int i = 0; i < count[0] ; i++)

    result.addValue("");

    else

    result.addSuppress();

    Add comment
    10|10000 characters needed characters exceeded