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

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

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-20 19:17
    if (str.indexOf(substr) >= 0) {
        // do something
    }
    
    0 讨论(0)
  • 2020-12-20 19:17

    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;
    }
    
    0 讨论(0)
  • 2020-12-20 19:20

    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
    
    0 讨论(0)
  • 2020-12-20 19:21
      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;
            }
    
        }
    
    0 讨论(0)
  • 2020-12-20 19:22
    public class StringIsSubString {
    
        public static void main(String[] args) {
    
            String s1 = "wel";
            String s2 = "12wlecome123";
    
            boolean isSubStr = isSubStr(s1,s2);
            System.out.println(isSubStr);
        }
    
        private static boolean isSubStr(String s1, String s2) {
            String s3 = "";
            int j = 0;
    
            if(s1.length() > s2.length()) {
                return false;
            } else if(s1.equals(s2)){
                return true;
            } else {
                for(int i=0; i<s1.length();i++) {
                    for(; j<s2.length();j++) {
                        if(s1.charAt(i) == s2.charAt(j)) {
                            s3 = s3 + s1.charAt(i);
                            break;
                        }
                    }
                }
                if(s3.equals(s1)) {
                    return true;
                }
                return false;       
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题