Split string and find index

Highlighted
Six Stars

Split string and find index

hi, I have two fields (field1 and field2). field1 is of the type "800; 805; 116; 809; 804; 830; 346; 345; 890; 881; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0" while field2 is of the type "1; 0; 215; 0; 0; 1; 2; 66; 5; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0". I would need to extract from field2 the value corresponding to the index searched in field1 (eg I look for "800" in field1 and extract "1" from field2, for example I look for "116" in field1 and extract "215" from field2). This I would need to do it within tmap.

I hope I have not made too much confusion ..... :-)
thanks a lot


Accepted Solutions
Eleven Stars

Re: Split string and find index

in Tmap buiding expression, use below expression

field2.split(";")[java.util.Arrays.asList(field1.split(";")).indexOf("stringtosearch")]

e.g.
String f1 = "800;801;802;803";
String f2 = "X;Y;Z;G";
f2.split(";")[java.util.Arrays.asList(f1.split(";")).indexOf("801")]
this will return Y
Regards
Abhishek KUMAR

All Replies
Forteen Stars

Re: Split string and find index

Hi

 

you can do this thru Java code, and if you want to do this with build components:

  • use tNormalize for split row into many rows
  • add row number to the flow - sequence
  • join in tMap by row number

 

regards, Vlad

-----------
Eleven Stars

Re: Split string and find index

in Tmap buiding expression, use below expression

field2.split(";")[java.util.Arrays.asList(field1.split(";")).indexOf("stringtosearch")]

e.g.
String f1 = "800;801;802;803";
String f2 = "X;Y;Z;G";
f2.split(";")[java.util.Arrays.asList(f1.split(";")).indexOf("801")]
this will return Y
Regards
Abhishek KUMAR
Six Stars

Re: Split string and find index

thank you :-)

2019 GARNER 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

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch Now

Best Practices for Using Context Variables with Talend – Part 4

Pick up some tips and tricks with Context Variables

Blog

How Media Organizations Achieved Success with Data Integration

Learn how media organizations have achieved success with Data Integration

Read

Downloads and Trials

Test drive Talend's enterprise products.

Downloads