不等于符号(java)

is not equal to the symbols(java)

对不起我的英语。我想为自己找出一点,但我没有得到。我想要那行输出用在 word 的情况下。但我有这样的印刷品:如果我想找到一个词 - one 是应用程序 - ones entered the convent home。它仍然是输出线。而在这种情况下不想显示。

public static void main(String[] args) throws FileNotFoundException {
    String line = "line number ones";
    String word = "num";
    String myWord = "";
    int startWord = 0;


    for(int i = 0; i < line.length()-1; i++) {
        if(line.charAt(i) == word.charAt(startWord)) {
            startWord++;
            myWord += line.charAt(i);

            if(myWord.equals(word)) {
                if(startWord == word.length()) {
                    System.out.println(line);
                }
                startWord = 0;
            }
        }
    }
}

因为需要进行额外的检查。类似于:

if(Character.toString(line.charAt(i+1)) != any characters except space) {
output line
}

结果是这样的:

if(startWord == word.length() && Character.toString(line.charAt(i+1)) == " ") {
                        System.out.println(line);
                    }

但是不行。我们还必须考虑到它可以是一个点、一个逗号,也可以是行尾。怎么做?

您是否要检查字符是否等于 space?如果是,则不能在 String 上使用 ==,因为它是一个对象。但是您可以在 char.

上使用它

所以两种方式是这样的:

String space = " ";
if (space.equals (" ")) // true

char space = ' ';
if (space == ' ') // true

在你的例子中,你会这样做:

Character.toString(line.charAt(i+1)).equals (" ")