因此,我输入了两个字符串,并在 mString 中查找 subString。当我将方法更改为布尔值时,它会返回正确的输出 true 或 false(通过在 contains 语句上使用 return)。
我不知道如何使用该语句来检查包含运算符的结果。我已经完成了以下工作。
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 语句中返回一个值?