How to use IndexOf and Tokenize for the below requirement

There are two fields in the file layout,

Address Field 1 (position 71-100)

Address Field 2 (106-140)

We need to have the value in address field 1 be less than 30 characters. If it goes over 30 characters then it needs to start in Address field 1 and then continue on in Address field 2. They do not want the words to truncate either so for example if the word ‘Box’ is at position 100 they want the entire word moved to start at position 106 not just the ‘ox’

Example:

On the xml the Address is: 9999 Old main high street rd Box 200

In Address Field 1 (position 71-100) we need to see: 9999 Old main high street rd

Then have Filler records from (position 101-105)

Then In Address Field 2 (106-140) we need to see: Box 200

Please suggest how to achieve this. I tried the below but it is not working,

I used indexof transforer to look for the space and created loop and keep on doing the indexof but now the starting position is of the previous space position which is found. Then used Tokenize with the space as the delimiter to make a valuelist array and looped on that array to built a string. When the string length is > 30 is it will map the result to Address 2. This is where I am struggling. Please advise.

Hi Ganapathy,

can you provide a complete line as a sample please?

Usage of Tokenize sounds good so far.

You can test the length of original string for being less or equal to 30 or if it is longer than 30 characters.
For the first condition you can directly map it from original source to to target field.
For second condition you can try the below approach.

During iteration over the array sum up the length of each part before concatening together the parts to the target field.
Remove the parts which already have been concatened to the target field from the original string.
When the sum exceeds 30 exit from $loop and put the reminder from the original string into the second field.

You can use trim to remove trailing spaces if needed.

Regards,
Holger