我有字符串{"All-Inclusive,All Inclusive","Luxury,Luxury","Spa-And-Relaxation,Spa & Relaxation"}数组
我想根据",“将它们分割成两个数组,第一个数组{"All-Inclusive","Luxury","Spa-And-Relaxation"}和第二个数组{"All Inclusive","Luxury","Spa & Relaxation"}。
你能建议一下该怎么做吗?
发布于 2014-11-28 07:03:03
您可以迭代您的String数组。对于每个元素,调用String.split(String)并生成一个临时数组。确保从数组中获得两个String,然后将其分配给输出first和second,如下所示
public static void main(String[] args) {
String[] arr = { "All-Inclusive,All Inclusive", "Luxury,Luxury",
"Spa-And-Relaxation,Spa & Relaxation" };
String[] first = new String[arr.length];
String[] second = new String[arr.length];
for (int i = 0; i < arr.length; i++) {
String[] t = arr[i].split("\\s*,\\s*");
if (t.length == 2) {
first[i] = t[0];
second[i] = t[1];
}
}
System.out.printf("First = %s%n", Arrays.toString(first));
System.out.printf("Second = %s%n", Arrays.toString(second));
}输出是
First = [All-Inclusive, Luxury, Spa-And-Relaxation]
Second = [All Inclusive, Luxury, Spa & Relaxation]https://stackoverflow.com/questions/27183422
复制相似问题