I am trying to get an element from my string which I have attained through a getSelectedValue().toString from a JList.
It returns [1] testString
What I am trying to do it only get the 1 from the string. Is there a way to only get that element from the string or remove all else from the string?
I have tried:
String longstring = Customer_list.getSelectedValue().toString();
int index = shortstring.indexOf(']');
String firstPart = myStr.substring(0, index);
String#substring(int)defines the substring from the beginning index to the end. This will just give you everything after the[1]. if you useString#substring(int,int), which is defined as create a substring from posxto excluse posythen you could do it like this:longstring.substring(0, longstring.indexOf("]") + 1).String#replace(charSequence, charSequence)method. The first parameter should be either"["or"]", your second could be a empty String as"". Just call it twice after each other and the brackets should be gone.