Split long String

Highlighted
Five Stars

Split long String

please could you help me split a long string more than 50 char from input to 2 columns in output.  the output should be splitted so the columns should make sense.

 

input :

company_name =   "i am new to talend and want to learn,  it great tool for data integration"

 

output:

row1.company_name1 = i am new to talend and want to

row1.Company_Name2 = learn,  it great tool for data integration

 

Regards,


Accepted Solutions
Highlighted
Sixteen Stars
Sixteen Stars

Re: Split long String

Probably due to null values on fields.

Try this:

String[] arr = input_row.col1.split(" ");
output_row.input_row.Company_Name = "";
output_row.input_row.Company_Name1 = "";

for (String word : arr) {
    if ((output_row.Company_Name.length() + word.length() + 1) < 50)
        output_row.Company_Name = output_row.Company_Name + " " + word;
    else
        output_row.Company_Name1 = output_row.Company_Name1 + " " + word;
}
if ((output_row.Company_Name1.length() < 1)
    output_row.Company_Name1 = null;

 


TRF

View solution in original post


All Replies
Highlighted
Sixteen Stars
Sixteen Stars

Re: Split long String

Hi,
Using a tJavaRow and split() method you should be able to do that:

String[] arr = input_row.col1.split(" ");
for (String word : arr) {
if ((output_row.col1.length() + word.length() + 1) < 50)
output_row.col1 = output_row.col1 + " " + word;
else
output_row.col2 = output_row.col2 + " " + word;
}

Can't try cause actually on my mobile, but at least you got the idea.
Hope this helps.

TRF
Highlighted
Five Stars

Re: Split long String

thanks TRF , not sure what I am doing wrong. for some reason it wont copy the out to excel.Capture.JPG

 

Highlighted
Sixteen Stars
Sixteen Stars

Re: Split long String

The problem is on the tFileExcelInput component which gives you no rows.


TRF
Highlighted
Five Stars

Re: Split long String

fixed the tFileExcelInput , but still below error.

 

 

not sure if tjavarow is in right place and the code in it.

input row column name is "input_row.Company_Name" I want to split it to "output_row.Company_Name" and "output_row.Company_Name1"

output_row.Company_Name can have max 50 char.

 

Capture.JPG

 

Highlighted
Sixteen Stars
Sixteen Stars

Re: Split long String

Probably due to null values on fields.

Try this:

String[] arr = input_row.col1.split(" ");
output_row.input_row.Company_Name = "";
output_row.input_row.Company_Name1 = "";

for (String word : arr) {
    if ((output_row.Company_Name.length() + word.length() + 1) < 50)
        output_row.Company_Name = output_row.Company_Name + " " + word;
    else
        output_row.Company_Name1 = output_row.Company_Name1 + " " + word;
}
if ((output_row.Company_Name1.length() < 1)
    output_row.Company_Name1 = null;

 


TRF

View solution in original post

Highlighted
Five Stars

Re: Split long String

perfect works fine.

Highlighted
Sixteen Stars
Sixteen Stars

Re: Split long String

Great!


TRF

2019 GARTNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

Best Practices for Using Context Variables with Talend – Part 2

Part 2 of a series on Context Variables

Blog

Best Practices for Using Context Variables with Talend – Part 1

Learn how to do cool things with Context Variables

Blog

Migrate Data from one Database to another with one Job using the Dynamic Schema

Find out how to migrate from one database to another using the Dynamic schema

Blog