Search code examples
talend

Talend - split a string to n rows


I would like to split a string in a column to n rows in Talend.

For example :

column
2aabbccdd

The first number is the "n" which I use to define the row lenght, so the expected result should be :

row 1 = aa

row 2 = bb

row 3 = cc

row 4 = dd

The idea here is to iterate on the string and cut it every 2 characters.

Any idea please ?


Solution

  • I would use a tJavaFlex to split the string, with a trick to have n rows coming out of it. enter image description here

    tJavaFlex's main code:

    int n = Integer.parseInt(row1.str.substring(0, 4)); //get n from the first 4 characters
    String str2 = row1.str.substring(4); //get the string after n
    
    int nbParts = (str2.length() + 1) / n;
    
    System.out.println("number of parts = " + nbParts);
    
    for (int i = 0; i < nbParts; i++)
    {
        String part = str2.substring(i * n);
        if(part.length() > n)
        {
            part = part.substring(0, n);
        }
    
        row2.str = part;
    

    And tJavaFlex's end code is just a closing brace:

    }
    

    The trick is to use a for loop in the main code, but only close it in the end code.
    tFixedFlowInput contains just one column holding the input string.