Skip to main content
Announcements
See what Drew Clarke has to say about the Qlik Talend Cloud launch! READ THE BLOG
cancel
Showing results for 
Search instead for 
Did you mean: 
lmit
Creator II
Creator II

substring method

Hi All,

Am trying to extract substring from a string like Adult (18-60 years)

i should get the output as Adult from the above string

I used method

column_name == null || column_name == ""?

null

:

column_name..substring(column_name.lastIndexOf(" ")+1)

but am getting below error

java.lang.StringIndexOutOfBoundsException: String index out of range: -1

Can someone please suggest what wrong am doing

Thanks in advance,

lmit

Labels (2)
1 Solution

Accepted Solutions
Anonymous
Not applicable

Hello lmit,

To get the Adult from the above string, you can try:

column_name.substring(0,5)

or

column_name.split(" ")[0]

 

Best regards

Aiming

 

View solution in original post

3 Replies
Anonymous
Not applicable

Hello lmit,

To get the Adult from the above string, you can try:

column_name.substring(0,5)

or

column_name.split(" ")[0]

 

Best regards

Aiming

 

ThWabi
Creator II
Creator II

Hello lmit,

 

please do not use == operator to compare Strings in Java, use

column_name.equals("")

or

column_name.isEmpty()

 

Does your input data contain Strings without a blank? Perhaps the IndexOutOfBounds-Exception occurs in those cases.

 

For your example input "Adult (18-60 years)", the substring- and lastIndexOf-Methods would yield "years)", because the substring starts at the last blank (+1) in the string and extends to the end. May I suggest

column_name.substring(0, column_name.indexOf(" ") -1)

 

Best regards,

 

Thomas

 

 

lmit
Creator II
Creator II
Author

Thanks @Aiming Chen​