检查字符串一是否在Java中包含字符串二

问题描述 投票:-2回答:1

[任务:编写一个接收2个字符串参数并检查第二个字符串是否包含在第一个字符串中的方法。该方法将返回一个布尔值。示例:对“巫师”和“巫师”返回true。

import java.util.*;

class Dcoder {
    public static void main(String args[]) { 
        System.out.println(method("The Witcher","Witcher"));
    }

    public static boolean method(String str1, String str2) {
        String s1 = "The Witcher";
        boolean s2 = s1.indexOf("Witcher") != -1 ? true : false;
        return s2;
    }
}

我的问题:如何编写代码,以便仅在主要方法中指定“ The Witcher”和“ Witcher”。

java string indexof
1个回答
0
投票

用于在方法中进行比较的字符串实际上并不是您作为参数传递给函数的字符串。另外,Java中已经有一种方法可以让您轻松地执行此操作,如下所示:

String s1 = "The Witcher";
Boolean result = s1.contains("Witcher");
© www.soinside.com 2019 - 2024. All rights reserved.