下面的程序错在哪里?只是去掉一个String里的某个标点符号。

来源:百度知道 编辑:UC知道 时间:2024/06/20 23:32:19
public static String remove(String sentence, char punctuation){
int first = sentence.indexOf(punctuation);
while (first > 0){
String firstHalf = sentence.substring(0, first);
String secondHalf = sentence.substring(first + 1);
sentence = firstHalf + secondHalf;
first = secondHalf.indexOf(punctuation);
}
return sentence;
}
我发现了自己的错误
public static String remove(String sentence, char punctuation){
int first = sentence.indexOf(punctuation);
String newSentence = "";
String firstHalf = "";
String secondHalf = "";
while (first > 0){
firstHalf = sentence.substring(0, first);
secondHalf = sentence.substring(first + 1);
newSentence = newSentence + firstHalf;
sentence = secondHalf;
first = sentence.indexOf(punctuation);
}
newSentence

我找到错误了,你不注意大小写
我改了看看

public static String remove(String sentence, char punctuation)
{
int first = sentence.IndexOf(punctuation);
while (first > 0)
{
String firstHalf = sentence.Substring(0, first);
String secondHalf = sentence.Substring(first + 1);
sentence = firstHalf + secondHalf;
first = secondHalf.IndexOf(punctuation);
}
return sentence;
}

IndexOf 开头要大写的,你小写了
还有Substring

看不懂