java - 使用 'contains' 返回两个字符串的比较结果

标签 java if-statement boolean

因此,我输入了两个字符串,并在 mString 中查找了 subString。当我将方法更改为 boolean 值时,它会返回 true 或 false 的正确输出(通过在 contains 语句上使用 return )。

我不知道如何使用该语句来检查 contains 运算符的结果。我已经完成了以下内容。

public class CheckingString
{

    public static void main(String[] args)
    {
        // adding boolean value to indicate false or true
        boolean check;

        // scanner set up and input of two Strings (mString and subString)
        Scanner scan = new Scanner(System.in);
        System.out.println("What is the long string you want to enter? ");
        String mString = scan.nextLine();
        System.out.println("What is the short string that will be looked for in the long string? ");
        String subString = scan.nextLine();

        // using the 'contain' operator to move check to false or positive.
        // used toLowerCase to remove false negatives
        check = mString.toLowerCase().contains(subString.toLowerCase());

        // if statement to reveal resutls to user
        if (check = true)
        {
            System.out.println(subString + " is in " + mString);
        }
        else
        {
            System.out.println("No, " + subString + " is not in " + mString);
        }
    }

}

有没有办法让该检查字段正常工作以在 if-else 语句中返回值?

最佳答案

if (check = true){

应该是:

if (check == true){

通常你会写:

if(check)

检查是否正确

和:

if(!(check)) 

或者:

if(!check)

检查是否错误。

关于java - 使用 'contains' 返回两个字符串的比较结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13485968/

相关文章:

java - 覆盖 struts 2 拦截器

android - 我可以使用 Java API 将图像文件存储在 firebase 中吗?

batch-file - BATCH 文件中多个文件名的条件 IF EXIST 语句

java:循环两个 boolean 值(false,true)

swift - 可以比较包含 boolean 值的元组。但是官方文件说没有

java - Java 中使用客户端证书公钥进行 RSA 算法 PKCS #1.5 填充(不是 OEAP)密码加密

java - 异常信息在哪里?

c++ - 在 C++ 语法中,if else 的条件可以返回一个 int 并仍然执行其中的语句

android studio edittext字段值转换

javascript - 字符串 '0' 在 javaScript 中为 true|false