Home > Java > javaTutorial > Java implementation code for finding the number of substrings in a string

Java implementation code for finding the number of substrings in a string

高洛峰
Release: 2017-01-18 16:44:53
Original
2154 people have browsed it

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;
    }
}
Copy after login

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);
    }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template