如何知道给定字符串是否是Java中另一个字符串的子字符串

时间:2023-01-25 07:07:53

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).

方法“contains”应返回true,因为str包含substr(否则为false)。

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?

我在String类中寻找类似“contains”的东西,但我找不到它。我想唯一的解决方案是使用模式匹配。如果是这种情况会更好(最便宜)的方式吗?

Thanks!

谢谢!

10 个解决方案

#1


19  

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:

有一个contains()方法!它是在Java 1.5中引入的。如果您使用的是早期版本,则可以轻松地将其替换为:

str.indexOf(substr) != -1

#2


4  

 String str="hello world";
        System.out.println(str.contains("world"));//true
        System.out.println(str.contains("world1"));//false

#3


2  

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

使用indexOf它会返回-1如果没有匹配(包含在1.5中添加,也许你使用的是较旧的jdk?)请参阅JDK 1.4.2中String类中的“contains(CharSequence s)”方法了解详细信息

#4


2  

  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


1  

if (str.indexOf(substr) >= 0) {
    // do something
}

#6


1  

I think there is a String function that does just what you are asking: String.indexOf(String).

我认为有一个String函数可以满足您的要求:String.indexOf(String)。

See this link: http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#indexOf(java.lang.String)

请看这个链接:http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#indexOf(java.lang.String)

So, then you could write this function:

那么,你可以编写这个函数:

public boolean isSubstring(String super, String sub) {
    return super.indexOf(sub) >= 0;
}

#7


1  

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.

String.indexOf(substr)复杂度是O(n2).. Luixv问了一个更便宜的解决方案..但据我所知,没有比现有算法更好的算法。

#8


1  

    public boolean isSubString(String smallStr, String largerStr) {
    char[] larger = largerStr.toCharArray();
    char[] smaller = smallStr.toCharArray();

    int i = 0;

    for (int j = 0; j < larger.length; j++) {
        if(larger[j] == smaller[i]){
            if(i == smaller.length -1){
                //done we found that this string is substring
                return true;
            }
            i++;
            continue;
        }else{
            if(i > 0){
                //that means we encountered a duplicate character before and if string was substring 
                // it shouldn't have hit this condition..
                if(larger.length - j >= smaller.length){
                    i = 0;
                    //reset i here because there are still more characters to check for substring..
                }else{
                    //we don't have enough characters to check for substring.. so done..
                    return false;
                }

            }
        }

    }

    return false;
}

#9


1  

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);

}

#10


0  

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.

这将检查s2是否为s1的子字符串。

#1


19  

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:

有一个contains()方法!它是在Java 1.5中引入的。如果您使用的是早期版本,则可以轻松地将其替换为:

str.indexOf(substr) != -1

#2


4  

 String str="hello world";
        System.out.println(str.contains("world"));//true
        System.out.println(str.contains("world1"));//false

#3


2  

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

使用indexOf它会返回-1如果没有匹配(包含在1.5中添加,也许你使用的是较旧的jdk?)请参阅JDK 1.4.2中String类中的“contains(CharSequence s)”方法了解详细信息

#4


2  

  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


1  

if (str.indexOf(substr) >= 0) {
    // do something
}

#6


1  

I think there is a String function that does just what you are asking: String.indexOf(String).

我认为有一个String函数可以满足您的要求:String.indexOf(String)。

See this link: http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#indexOf(java.lang.String)

请看这个链接:http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#indexOf(java.lang.String)

So, then you could write this function:

那么,你可以编写这个函数:

public boolean isSubstring(String super, String sub) {
    return super.indexOf(sub) >= 0;
}

#7


1  

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.

String.indexOf(substr)复杂度是O(n2).. Luixv问了一个更便宜的解决方案..但据我所知,没有比现有算法更好的算法。

#8


1  

    public boolean isSubString(String smallStr, String largerStr) {
    char[] larger = largerStr.toCharArray();
    char[] smaller = smallStr.toCharArray();

    int i = 0;

    for (int j = 0; j < larger.length; j++) {
        if(larger[j] == smaller[i]){
            if(i == smaller.length -1){
                //done we found that this string is substring
                return true;
            }
            i++;
            continue;
        }else{
            if(i > 0){
                //that means we encountered a duplicate character before and if string was substring 
                // it shouldn't have hit this condition..
                if(larger.length - j >= smaller.length){
                    i = 0;
                    //reset i here because there are still more characters to check for substring..
                }else{
                    //we don't have enough characters to check for substring.. so done..
                    return false;
                }

            }
        }

    }

    return false;
}

#9


1  

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);

}

#10


0  

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.

这将检查s2是否为s1的子字符串。