如何从arraylist中删除多余的空格 [英] How to remove extra spaces from arraylist
问题描述
我必须从数据库中检索值并将其添加到 arraylist 中.这是我的代码..
I have to retrieve values from database and add it into arraylist .Here is my code ..
ArrayList country = new ArrayList();
strQuery = "Select * from country";
rs = conexiondb.Consulta(strQuery);
while (rs.next()) {
String toc = rs.getString("country");
country.add(toc);
}
out.print(country);
System.out.println(country);
out.close();
我已将从数据库中检索到的所有值添加到国家/地区..这是该国家/地区中存在的值..
i have added all the values retrieved from database into country..and here is the values present in the country..
[亚太地区、北美、南美、欧洲]
现在根据我的需要,我必须在每个逗号值之后删除空格并使其像这样..
Now as per my need i have to remove space after each comma values and make it like this..
[亚太地区、北美、南美、欧洲]
请大家帮我解决这个问题..
Please guys help me to solve this..
提前致谢.
推荐答案
你所看到的是 ArrayList
如何格式化它的内容,而不是你的查询结果,尝试类似...... >
What you are seeing is how ArrayList
formats it's contents, not the results of your query, try something like...
for (String name : country) {
System.out.println("[" + name + "]");
}
检查.如果输出中仍有空格,则可以在从数据库中提取值时以及将它们放入 List
To check. If there are still spaces in the output, then you can use String#trim
when you extract the values from the database and before you place them in the List
如果你需要格式化String
,你需要提供你自己的机制,例如...
If you need to format the String
, you need to provide you own mechanism, for example...
List<String> values = new ArrayList<>();
values.add("APAC");
values.add("North America");
values.add("South America");
values.add("Europe");
System.out.println(values);
StringBuilder sb = new StringBuilder(128);
for (String value : values) {
if (sb.length() > 0) {
sb.append(",");
}
sb.append(value);
}
sb.insert(0, "[");
sb.append("]");
System.out.println(sb);
哪些输出...
[APAC, North America, South America, Europe]
[APAC,North America,South America,Europe]
这篇关于如何从arraylist中删除多余的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!