来一份java分解逗号相连的字符串删除某一特定字符的代码
以下是Java代码,可以将逗号分隔的字符串转换为字符串数组,并删除该数组中所有包含特定字符的字符串:
public static String[] removeString(String input, String toRemove) {
String[] arr = input.split(",");
ArrayList<String> list = new ArrayList<String>();
for (String s : arr) {
if (!s.contains(toRemove)) {
list.add(s);
}
}
String[] output = new String[list.size()];
output = list.toArray(output);
return output;
}
使用示例:
String input = "apple,banana,orange,pear";
String toRemove = "e";
String[] output = removeString(input, toRemove);
System.out.println(Arrays.toString(output)); // 输出:[banana, orange]
该代码将输出一个字符串数组,其中不包含任何包含特定字符的字符串
原文地址: https://www.cveoy.top/t/topic/cFsD 著作权归作者所有。请勿转载和采集!