how to display total no. of substring in a string

If I understood you correctly this is a solution for your problem:

    String str="This is this my book is This";
    Map<String, Integer> counts = new HashMap<String, Integer>();

    String[] words = str.toLowerCase().split("[\\s\\.,;!\\?]");

    for (String word: words) {
        int count = counts.containsKey(word) ? counts.get(word).intValue() : 0;
        counts.put(word, Integer.valueOf(count + 1));
    }

You just split the string by the delimiters you want to consider and collect the occurrences in a map.

Leave a Comment