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

后端 未结 16 1778
情歌与酒
情歌与酒 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:35

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

提交回复
热议问题