Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
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
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
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
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
Thanks @Aiming Chen