1. Use the indexof method:
public class Test11 { private static int counter = 0; /** * @param args */ public static void main(String[] args) { String str ="sdSS**&HGJhadHCASch& ^^"; int i = stringNumbers(str); System.out.println(i); } public static int stringNumbers(String str) { if (str.indexOf("java")==-1) { return 0; } else if(str.indexOf("java") != -1) { counter++; stringNumbers(str.substring(str.indexOf("java")+4)); return counter; } return 0; } }
2. If the substring is not a string with the same beginning and end, it can also be implemented like this:
if(str.indexOf("java") != -1) { String[] str1 = str.split("java"); System.out.println(str1.length-1); } else { System.out.println(0); }
The above Java search string contains the number of substrings implementation code is all the content shared by the editor. I hope it can give you a reference, and I hope everyone Duoduo supports PHP Chinese website.
For more java search string implementation codes containing the number of substrings, please pay attention to the PHP Chinese website!