OK, so I have an ArrayList that I need to return as a String. Right now I am using this approach:
我有一个ArrayList需要作为字符串返回。现在我正在使用这种方法:
List<Customer> customers = new ArrayList<>();
List<Account> accounts = new ArrayList<>();
public String customerList()
{
String list = Arrays.toString(customers.toArray());
return list;
}
public String accountList()
{
String account = Arrays.toString(accounts.toArray());
return account;
}
This works fine, but the problem is that I get brackets around the text. I am getting stuff like:
这很好,但问题是我在文本周围加上了括号。我得到的东西是:
Customer list:
--------------
[Name: Last, First
, Name: Last, First
, Name: Last, First
]
When I want something like this:
当我想要这样的东西:
Customer list:
--------------
Name: Last, First
Name: Last, First
Name: Last, First
However, unlike similar questions, I don't simply want to output line by line. I need to store the ArrayList in a String without the brackets so I can work with it.
然而,与类似的问题不同,我不希望逐行输出。我需要将ArrayList存储在一个没有括号的字符串中,这样我就可以使用它。
EDIT: It should be noted that the one comma in the version I want it to look like was placed there by a method in a different class:
编辑:应该注意的是,我想要的版本中的一个逗号是由另一个类中的方法放在那里的:
public final String getName()
{
return getLast() + ", " + getFirst();
}
EDIT 2: I was able to find a complete solution by adapting the two answers I was given. It was difficult deciding which was the "answer" because I found both useful, but went with the one that I could use more selectively.
编辑2:我能够找到一个完整的解决方案,通过调整我得到的两个答案。很难决定哪一个是“答案”,因为我发现这两个都有用,但我选择了一个可以更有选择性地使用的。
public String customerList()
{
String list = Arrays.toString(customers.toArray()).replace(", N", "N").replace(", N", "N");
return list.substring(1,list.length()-1);
}
To remove the brackets I used the modified return. Removing the comma required me to focus on the fact that each line will start with an "N", but the flaw in this approach is that the code would break if I forget to change it here if I change it there. Still, it solves my specific problem, and I can always notate to myself in both places as needed.
为了删除括号,我使用了修改后的返回。删除逗号要求我关注这样一个事实:每一行都以“N”开头,但这种方法的缺点是,如果我忘记在这里修改代码,代码就会中断。尽管如此,它还是解决了我的具体问题,而且我可以根据需要在这两个地方给自己做点笔记。
6 个解决方案
#1
19
You could try to replace the '[' and ']' with empty space
你可以尝试用空的空间来替换“['和']”
String list = Arrays.toString(customers.toArray()).replace("[", "").replace("]", "");
#2
3
Can directly convert list/set to string and perform action on it
可以直接将list/set转换为string并对其执行操作吗
customers.toString().replace("[", "").replace("]", "")
#3
2
A possible solutions is:
一个可能的解决方案是:
String account = Arrays.toString(accounts.toArray());
return account.substring(1,account.length()-1);
Or do you override the toString
method to return the string as per you wanted.
或者您是否重写toString方法以按需要返回字符串。
#4
1
If you want your output to look like: item1, item2, item3
如果您希望输出看起来像:item1、item2、item3
Arrays.toString(customers.toArray()).replace('[', ' ').replace(']', ' ').trim()
If you want your output to look like: item1 item2 item3
如果您希望输出看起来像:item1 item2 item3
Arrays.toString(customers.toArray()).replace('[', ' ').replace(']', ' ').replace(',', ' ').trim()
#5
1
You can use the method substring() to remove starting and ending brackets without tampering any entries in the ArrayList. I used something like this to convert a ArrayList<String>
to String
您可以使用method substring()删除开始和结束括号,而无需修改ArrayList中的任何条目。我使用类似的方法将ArrayList
String str = list.toString().substring(1, list.toString().length() - 1);
#6
0
You can override the toString() method and represent the output in whatever format you
您可以覆盖toString()方法,并以任何格式表示输出。
#1
19
You could try to replace the '[' and ']' with empty space
你可以尝试用空的空间来替换“['和']”
String list = Arrays.toString(customers.toArray()).replace("[", "").replace("]", "");
#2
3
Can directly convert list/set to string and perform action on it
可以直接将list/set转换为string并对其执行操作吗
customers.toString().replace("[", "").replace("]", "")
#3
2
A possible solutions is:
一个可能的解决方案是:
String account = Arrays.toString(accounts.toArray());
return account.substring(1,account.length()-1);
Or do you override the toString
method to return the string as per you wanted.
或者您是否重写toString方法以按需要返回字符串。
#4
1
If you want your output to look like: item1, item2, item3
如果您希望输出看起来像:item1、item2、item3
Arrays.toString(customers.toArray()).replace('[', ' ').replace(']', ' ').trim()
If you want your output to look like: item1 item2 item3
如果您希望输出看起来像:item1 item2 item3
Arrays.toString(customers.toArray()).replace('[', ' ').replace(']', ' ').replace(',', ' ').trim()
#5
1
You can use the method substring() to remove starting and ending brackets without tampering any entries in the ArrayList. I used something like this to convert a ArrayList<String>
to String
您可以使用method substring()删除开始和结束括号,而无需修改ArrayList中的任何条目。我使用类似的方法将ArrayList
String str = list.toString().substring(1, list.toString().length() - 1);
#6
0
You can override the toString() method and represent the output in whatever format you
您可以覆盖toString()方法,并以任何格式表示输出。