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

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

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

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

    }