2

我不认为我对为什么 -1 在这段代码中起作用有一个明确的理解:它只是一个允许程序继续运行的位置标记吗?任何帮助或指导将不胜感激。

public class RemovingChar {

    public static void main(String[]args)
    {
        String str = "Looking out the window of my small apartment";
        String remove = "aeiou";


        String x = " ";

        for(int i=0; i<str.length(); i++)
        {
            char c = str.charAt(i);

            if(remove.indexOf(c) == -1)
            {
                x+= c;
            }
        }
        System.out.print(x);
    }
}
4

6 回答 6

3

公共 int indexOf(int ch)

如果此字符串中没有出现此类字符,则返回 -1。

于 2013-05-03T08:45:19.300 回答
1

-1 表示CharacterNot found in the given String

文档中清楚地写着:

如果此字符串中没有出现此类字符,则返回 -1。

于 2013-05-03T08:46:53.777 回答
0
   StringBuilder strB = new StringBuilder(yourstring);
   strB.deleteCharAt(yourstring.length() - 1);

   System.out.print( strB.toString());

如果你想让 JButton 像 Backspace 一样工作,这里有一个教程

于 2013-05-03T09:49:36.697 回答
0
remove.indexOf(c) == -1

ifc不会出现在remove字符串返回中-1

于 2013-05-03T08:46:55.207 回答
0

indexOf()返回匹配字符串的索引...如果未找到,则返回-1 。

例如

如果您的字符串是“hello” ,那么“ello”的索引是1(因为它位于位置 1)。

或者

如果你的字符串是"hello"那么"bla"的索引是-1(因为它没有找到,所以indexOf返回-1

参考在W3Schools

于 2013-05-03T08:48:21.523 回答
0

indexOf如果参数字符不在remove变量中,则方法返回 -1 。

于 2013-05-03T08:48:38.823 回答