How to know if a given string is substring from another string in Java

后端 未结 16 1777
情歌与酒
情歌与酒 2020-12-20 18:58

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\"
<
16条回答
  •  无人及你
    2020-12-20 19:39

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

    See this link: 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;
    }
    

提交回复
热议问题