Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik GA: Multivariate Time Series in Qlik Predict: Get Details
cancel
Showing results for 
Search instead for 
Did you mean: 
Moro
Contributor
Contributor

extracting string before certain character - error: java.lang.StringIndexOutOfBoundsException: Range [0, -1) out of bounds for length 15

Hi all

I would like to extract all characters of a string before a certain character (eg. "-" or " ").

I used tJavaRow for this with the following code:

if((row3.street== null) || ("".equals(row3.street )) || row3.street.isEmpty() && ((String)input_row.street).contains("-"))

output_row.street = StringHandling.LEFT(input_row.street,StringHandling.INDEX(input_row.street,"("));

else if((input_row.street) != null && !((String)input_row.street).contains("("))

output_row.street = input_row.street;

I keep getting this out of bounds for lengths error and could t find a fix. Could you help me out here?

Exception in component tJavaRow_1 (Extract_Country_Code)

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

at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)

at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)

at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)

at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)

at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)

at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromToIndex(Preconditions.java:112)

at java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:349)

at java.base/java.lang.String.checkBoundsBeginEnd(String.java:4589)

at java.base/java.lang.String.substring(String.java:2703)

at routines.StringHandling.LEFT(StringHandling.java:229)

Thank you very much!

Labels (2)
1 Solution

Accepted Solutions
Anonymous
Not applicable

Something like this should work.....

 

(row3.street!=null&&row3.street.trim().equals("")==false) ? row3.street.indexOf('-')>-1 ? row3.street.substring(row3.street.indexOf('-')+1) : row3.street.indexOf(' ')>-1 ? row3.street.substring(row3.street.indexOf(' ')+1) : row3.street : row3.street

View solution in original post

4 Replies
Anonymous
Not applicable

I think this is what you need.....

 

(row3.street!=null&&row3.street.trim().equals("")==false) ? row3.street.indexOf('-')>-1 ? row3.street.substring(0,row3.street.indexOf('-')) : row3.street.indexOf(' ')>-1 ? row3.street.substring(0, row3.street.indexOf(' ')) : row3.street : row3.street

 

Moro
Contributor
Contributor
Author

Hi Shall,

thank you very much. This works.

I would like to get the characters after the " " or "-". How to do this?

Anonymous
Not applicable

Something like this should work.....

 

(row3.street!=null&&row3.street.trim().equals("")==false) ? row3.street.indexOf('-')>-1 ? row3.street.substring(row3.street.indexOf('-')+1) : row3.street.indexOf(' ')>-1 ? row3.street.substring(row3.street.indexOf(' ')+1) : row3.street : row3.street

Moro
Contributor
Contributor
Author

Thank you very much rhall! It works and understand it now. Thanks!!