How to know if a given string is substring from an

2020-02-14 02:18发布

Hi I have to compute if a given string is substring of a bigger string. For example

String str = "Hallo my world";
String substr = "my"

The method "contains" should return true because str contains substr (false otherwise).

I was looking for something like "contains" at the String class but I didn't find it. I suppose that the only solution is to use pattern matching. If this is the case which would be the better (cheapest) way to do this?

Thanks!

15条回答
冷血范
2楼-- · 2020-02-14 02:35

here is a general method that you can use

public static boolean isSubstring(String s1, String s2) {
    if(s1.length() == s2.length()) 
        return s1.equals(s2);
    else if(s1.length() > s2.length())
        return s1.contains(s2);
    else
        return s2.contains(s1);

}
查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-02-14 02:37

You can use .substring(int beginIndex,int lastIndex) to check this program. Sample code goes as below:-

public class Test {

    public static void main(final String[] args) {
        System.out.println("Enter the first String");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        try {
            String s1 = br.readLine();
            System.out.println("Enter the second String");
            String s2 = br.readLine();

            boolean result = isSubStr(s1, s2);
            if (result == true)
                System.out.println("The second String is substring of the first String");
            else
                System.out.println("The second String is not a substring of the first String");

        } catch (IOException e) {
            System.out.println("Exception Caught: " + e);
        }

    }

    public static boolean isSubStr(String st1, String s2) {

        boolean result = false;

        String tem_str = "";
        int len1 = st1.length();
        int i = 0;
        int j;

        while (i < len1) {
            j = i+1;
            while (j <=len1) {
                tem_str = st1.substring(i, j);
                if (tem_str.equalsIgnoreCase(s2)) {
                    result = true;
                    break;
                }
               j++;
            }

            i++;
        }
        return result;
    }
}
查看更多
淡お忘
4楼-- · 2020-02-14 02:38
  String s = "AJAYkumarReddy";
    String sub = "kumar";
    int count = 0;
    for (int i = 0; i < s.length(); i++) {
        if (s.charAt(i) == sub.charAt(count)) {
            count++;
        } else {
            count = 0;
        }
        if (count == sub.length()) {
            System.out.println("Sub String");
            return;
        }

    }
查看更多
太酷不给撩
5楼-- · 2020-02-14 02:39
 String str="hello world";
        System.out.println(str.contains("world"));//true
        System.out.println(str.contains("world1"));//false
查看更多
爷、活的狠高调
6楼-- · 2020-02-14 02:41
if (str.indexOf(substr) >= 0) {
    // do something
}
查看更多
放我归山
7楼-- · 2020-02-14 02:41

Go through this method. visit for tricky code

public static boolean isSubString(String s, String sub) {
    int count = 0;
    for (int i = 0; i < s.length(); i++) {
        if (s.charAt(i) == sub.charAt(count)) {
            count++;
        } else {
            i-=count;
            count = 0;
        }
        if (count == sub.length()) {
            return true;
        }

    }
    return false;
}
查看更多
登录 后发表回答