cancel
Showing results for 
Search instead for 
Did you mean: 

Split string in to many & control the occurence

ramu_g4
Participant
0 Kudos

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.

Accepted Solutions (0)

Answers (7)

Answers (7)

ambrish_mishra
Active Contributor
0 Kudos

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();

peter_karg
Explorer
0 Kudos

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

Peter

ashish_goel4
Active Participant
0 Kudos

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

PriyankaAnagani
Active Contributor
0 Kudos

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
ramu_g4
Participant
0 Kudos

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.

ramu_g4
Participant
0 Kudos

Hi Experts,

Can someone help on UDF to achieve this?

Thanks,

Ramu.

Satyagadadas
Active Participant
0 Kudos

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