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:42
public static boolean isSubstring(String s1, String s2){
    if(s1.length()<s2.length()) return false;
    if(s1.length()==s2.length()) return s1.equals(s2);
    for(int i=0;i<=s1.length()-s2.length();i++){
        if(s1.charAt(i)==s2.charAt(0)){
            int matchLength=1;
            for(int j=1;j<s2.length();j++){
                if(s1.charAt(i+j)!=s2.charAt(j)){
                    break;
                }
                matchLength++;
            }
            if(matchLength==s2.length()) return true;
        }
    }
    return false;
}

This checks if s2 is a substring of s1.

查看更多
仙女界的扛把子
3楼-- · 2020-02-14 02:44

There is a contains() method! It was introduced in Java 1.5. If you are using an earlier version, then it's easy to replace it with this:

str.indexOf(substr) != -1
查看更多
冷血范
4楼-- · 2020-02-14 02:46

use indexOf it will return -1 if no match (contains was added in 1.5, maybe you are using older jdk?) see "contains(CharSequence s)" method in String class in JDK 1.4.2 for details

查看更多
▲ chillily
5楼-- · 2020-02-14 02:47

String.indexOf(substr) complexity is O(n2).. Luixv asked a cheaper solution.. But as far as , I know there is no better algorithm than current one.

查看更多
Lonely孤独者°
6楼-- · 2020-02-14 02:49
    String str1 = "Java8 makes Java more powerful";
    String str2 = "Java";
    char c;
    char d;
    int count=0;
    boolean match = true;
    for (int i = 0; i < str1.length(); i++) {
        c = str1.charAt(i);
        for (int j = 0; j < str2.length(); j++) {
            d = str2.charAt(j);
            if (c == d) {
                match = true;
                count++;
                if(count== str2.length()){
                    i = str1.length();
                    break;
                }
                i++;
                c = str1.charAt(i);
            } else {
                match = false;
            }   
        }
    }

    if(match == true){
        System.out.println("SubString ");
    }
查看更多
再贱就再见
7楼-- · 2020-02-14 02:52

Consider the following code:

If substring is present then it returns the start index of substring in a given string

Else returns -1

public static int isSubstring(String str, String pattern)
{
    int str_length = str.length();
    int pattern_length = pattern.length();

    for (int i = 0; i <= str_length - pattern_length; i++)
    {
        int j;

        for (j = 0; j < pattern_length; j++)
            if (str.charAt(i + j) != pattern.charAt(j))
                break;

        if (j == pattern_length)
            return i;
    }
    return -1;
}
查看更多
登录 后发表回答