Split string and find index

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
Highlighted
Nine 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
Thirteen 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

-----------
Highlighted
Nine 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 :-)

What’s New for Talend Spring ’19

Join us live for a sneak peek!

Sign up now

Definitive Guide to Data Quality

Create systems and workflow to manage clean data ingestion and data transformation.

Download

Tutorial

Introduction to Talend Open Studio for Data Integration.

Watch

Downloads and Trials

Test drive Talend's enterprise products.

Downloads