[resolved] Extract string before a special character

One Star sj7
One Star

[resolved] Extract string before a special character

Hi,
I am trying to extract string before any special character. How can I do that?
For example:
Column1
india (451,98)
abc (123.43)
resd (34,56)
Expected Output:
Column1
india
abc
resd
I am trying with the java code(tjavarow), But it is showing me the following error:-
statistics] connecting to socket on port 3837
connected
Exception in component tJavaRow_1
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(Unknown Source)
at roamware.copy_of_rw_add_country_code_0_1.Copy_of_RW_Add_Country_Code.tOracleInput_1Process(Copy_of_RW_Add_Country_Code.java:893)
at roamware.copy_of_rw_add_country_code_0_1.Copy_of_RW_Add_Country_Code.runJobInTOS(Copy_of_RW_Add_Country_Code.java:1271)
at roamware.copy_of_rw_add_country_code_0_1.Copy_of_RW_Add_Country_Code.main(Copy_of_RW_Add_Country_Code.java:1139)
My code is as follow:-
output_row.Country = Relational.ISNULL(input_row.COUNTRYNAME)?"":input_row.COUNTRYNAME.trim().length()>0?input_row.COUNTRYNAME.trim().substring(0,input_row.COUNTRYNAME.indexOf("(")): input_row.COUNTRYNAME.trim();

Accepted Solutions

Re: [resolved] Extract string before a special character

If the scenario is that you also have the records that do not contain the character "(" and say if you want to pass them as it is to the output you can try out the below code in tJavaRow (You need to modify it as per your case):
if((input_row.Country) != null && ((String)input_row.Country).contains("("))
output_row.Country = StringHandling.LEFT(input_row.Country,StringHandling.INDEX(input_row.Country,"("));
else if((input_row.Country) != null && !((String)input_row.Country).contains("("))
output_row.Country = input_row.Country;

All Replies
Highlighted
One Star

Re: [resolved] Extract string before a special character

Hi,
you could try :
StringHandling.LEFT(row1.newColumn,StringHandling.INDEX(row1.newColumn,"("))
One Star sj7
One Star

Re: [resolved] Extract string before a special character

Hi, This is still showing me the following error:-
Exception in component tJavaRow_1
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(Unknown Source)
at routines.StringHandling.LEFT(StringHandling.java:218)
at roamware.copy_of_rw_add_country_code_0_1.Copy_of_RW_Add_Country_Code.tOracleInput_1Process(Copy_of_RW_Add_Country_Code.java:891)
at roamware.copy_of_rw_add_country_code_0_1.Copy_of_RW_Add_Country_Code.runJobInTOS(Copy_of_RW_Add_Country_Code.java:1268)
at roamware.copy_of_rw_add_country_code_0_1.Copy_of_RW_Add_Country_Code.main(Copy_of_RW_Add_Country_Code.java:1136)

Hi,
you could try :
StringHandling.LEFT(row1.newColumn,StringHandling.INDEX(row1.newColumn,"("))

Re: [resolved] Extract string before a special character

Hi, This is still showing me the following error:-
Exception in component tJavaRow_1
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(Unknown Source)
at routines.StringHandling.LEFT(StringHandling.java:218)
at roamware.copy_of_rw_add_country_code_0_1.Copy_of_RW_Add_Country_Code.tOracleInput_1Process(Copy_of_RW_Add_Country_Code.java:891)
at roamware.copy_of_rw_add_country_code_0_1.Copy_of_RW_Add_Country_Code.runJobInTOS(Copy_of_RW_Add_Country_Code.java:1268)
at roamware.copy_of_rw_add_country_code_0_1.Copy_of_RW_Add_Country_Code.main(Copy_of_RW_Add_Country_Code.java:1136)

Hi,
This error might be because some of your input records don't have the character "(" which we are using in the StringHandling function. Let us know if that is the case.
Regards,
Diwakar

Re: [resolved] Extract string before a special character

If the scenario is that you also have the records that do not contain the character "(" and say if you want to pass them as it is to the output you can try out the below code in tJavaRow (You need to modify it as per your case):
if((input_row.Country) != null && ((String)input_row.Country).contains("("))
output_row.Country = StringHandling.LEFT(input_row.Country,StringHandling.INDEX(input_row.Country,"("));
else if((input_row.Country) != null && !((String)input_row.Country).contains("("))
output_row.Country = input_row.Country;
One Star sj7
One Star

Re: [resolved] Extract string before a special character

Hi, Thanks a lot.
It is working fine now with this code. Thanks

If the scenario is that you also have the records that do not contain the character "(" and say if you want to pass them as it is to the output you can try out the below code in tJavaRow (You need to modify it as per your case):
if((input_row.Country) != null && ((String)input_row.Country).contains("("))
output_row.Country = StringHandling.LEFT(input_row.Country,StringHandling.INDEX(input_row.Country,"("));
else if((input_row.Country) != null && !((String)input_row.Country).contains("("))
output_row.Country = input_row.Country;

Re: [resolved] Extract string before a special character

That's great.. You're welcome Smiley Happy
Best Regards,
Diwakar

Re: [resolved] Extract string before a special character

You can mark this post as resolved.
Regards,
Diwakar

Re: [resolved] Extract string before a special character

i have a simmilar question, i get API with tRESTClient in JSON, when i use the API link directly in the browser it works fine.
when i use tRESTClient ---> tLogRow i get the same JSON, exept for the first part of the string, Talend added: '200||' to the string. so because of that i cannot use tExtractJSONFields because the JSON is invalid.
how can i extract the first five characters, in this case: '200||', from the string (JSON)??

15TH OCTOBER, COUNTY HALL, LONDON

Join us at the Community Lounge.

Register Now

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

Why Companies Move to the Cloud: 7 Success Stories

Learn how and why companies are moving to the Cloud

Read Now