Skip to main content
Announcements
Introducing Qlik Answers: A plug-and-play, Generative AI powered RAG solution. READ ALL ABOUT IT!
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

[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();
Labels (3)
1 Solution

Accepted Solutions
Anonymous
Not applicable
Author

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;

View solution in original post

9 Replies
NicolasTT
Contributor
Contributor

Hi,
you could try :
StringHandling.LEFT(row1.newColumn,StringHandling.INDEX(row1.newColumn,"("))
Anonymous
Not applicable
Author

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,"("))

Anonymous
Not applicable
Author

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
Anonymous
Not applicable
Author

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;
Anonymous
Not applicable
Author

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;

Anonymous
Not applicable
Author

That's great.. You're welcome 0683p000009MACn.png
Best Regards,
Diwakar
Anonymous
Not applicable
Author

You can mark this post as resolved.
Regards,
Diwakar
Anonymous
Not applicable
Author

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)??
Moro
Contributor
Contributor

I used exactly this code and keep getting the error:

 

Exception in component tJavaRow_1 (Extract_Country_Code)

java.lang.StringIndexOutOfBoundsException: Range [0, -1) out of bounds for length 15

 

Do you have any idea why and what I can do? I cannot find any solution for this in the forum.

Thank you!