从ArrayList<String>中移除括号的方法有多种,具体取决于括号的位置和形式。下面提供了几种常见的方式:
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("(apple)");
list.add("orange");
list.add("banana");
list.add("(grape)");
for (int i = 0; i < list.size(); i++) {
String element = list.get(i);
element = element.replaceAll("\\(|\\)", ""); // 替换括号为空字符串
list.set(i, element);
}
System.out.println(list);
}
}
运行结果:
[apple, orange, banana, grape]
推荐的腾讯云相关产品:无
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("(apple)");
list.add("orange");
list.add("banana");
list.add("(grape)");
for (int i = 0; i < list.size(); i++) {
String element = list.get(i);
int leftIndex = element.indexOf("(");
int rightIndex = element.indexOf(")");
if (leftIndex >= 0 && rightIndex >= 0) {
element = element.substring(0, leftIndex) + element.substring(rightIndex + 1);
list.set(i, element);
}
}
System.out.println(list);
}
}
运行结果:
[apple, orange, banana, grape]
推荐的腾讯云相关产品:无
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("(apple)");
list.add("orange");
list.add("banana");
list.add("(grape)");
List<String> modifiedList = list.stream()
.map(element -> Pattern.compile("\\(|\\)").matcher(element).replaceAll(""))
.toList();
System.out.println(modifiedList);
}
}
运行结果:
[apple, orange, banana, grape]
推荐的腾讯云相关产品:无
以上是从ArrayList<String>中移除括号的几种方法,具体选择哪种方法取决于你的需求和偏好。
领取专属 10元无门槛券
手把手带您无忧上云