You can use .substring (int beginIndex, int lastIndex) to test this program. An example code looks like this:
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; } }
source share