When jsp displays the values obtained from ArrayList on the screen, unnecessary square brackets ("[", "]") are printed.Could you tell me how to erase it?

Asked 2 years ago, Updated 2 years ago, 133 views

As stated in the title, I would like to remove the brackets that are unnecessarily attached to the values obtained from ArrayList on jsp.
images:
[123abc] →123abc
The following coding has been validated and none of them worked.

re ReplaceAll method (no regular expression)

<%String str1=str.replaceAll("[[[]]", "");%>
<p>output:<%=str1%><p>

re ReplaceAll method (with regular expression)

<%String str1=str.replaceAll("[\[\]]", "");%>
<p>output:<%=str1%><p>

re Replace method (no regular expression)

<%String str1=str.replace("[", "").replace("]", "");%>
<p>output:<%=str1%><p>

re Replace method (with regular expression)

<%String str1=str.replace("\[", "").replace("\]", "");%>
<p>output:<%=str1%><p>

If you know anything that can give you a hint, please lend me your wisdom.
Thank you in advance.

java jsp

2022-09-29 22:57

1 Answers

List#replaceAll() is the method to replace elements in the list.

In contrast, this output is the result of stringing by the ArrayList#toString() method.
(The square brackets shown are not included in the element, but are given by the toString() method.)

If you want to remove square brackets by string replacement, you must target the results of toString():

String str1 = str.toString().replaceAll("^\\[|\\]$", "");

If you don't like the toString() string expression, you don't have to use toString().
For example, you can use the String.join() method to string as follows:

String str1 = String.join(", ", str);


2022-09-29 22:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.