How to implement the strstr() to find the first occurrence of a substring in a string with Java

1 Answer

0 votes
public class MyClass {
    public static String strStr(String str, String tofind) {
		int index = str.indexOf(tofind);

		if (index > -1)
			return str.substring(index);
		else
			return null;
    }
    public static void main(String args[]) {
        String str = "c c++ java rust python java php";
 
        System.out.println(strStr(str, "java"));
    }
}
 
 
 
 
/*
run:
 
java rust python java php
 
*/

 

 



answered Aug 26, 2023 by avibootz
...