gpt4 book ai didi

java - 如何从字符串中删除指定的字符串

转载 作者:行者123 更新时间:2023-11-30 04:23:22 24 4
gpt4 key购买 nike

好吧,我正在尝试创建一个接受 2 个字符串的方法,第二个字符串参数是要从第一个字符串参数中删除的字符串。我不明白我的代码有什么问题?有什么帮助吗?谢谢。

我想做的示例:如果您传入“a,b,c”作为第一个参数,“,”作为第二个参数,则返回“abc”。

public List<String> split(String string, String delimiter){
//create and init arraylist.
List<String> list = new ArrayList<String>();
//create and init newString.
String newString="";
//add string to arraylist 'list'.
list.add(string);
//loops through string.
for(int i=0;i<string.length();i++){
//stores each character from string in newString.
newString += string.charAt(i);

}
newString.replace(delimiter, "");
//remove string from arraylist 'list'.
list.remove(string);
//add newString to arraylist 'list'.
list.add(newString);


return list;

最佳答案

非常简单,不确定您要对这些列表做什么,但是根据您的措辞问题,如果您想要的只是来自 a,b,cabc ,这样就可以了...

 public String split(String s1, String s2){
return s1.replace(s2,"");
}

关于java - 如何从字符串中删除指定的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16453155/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com