3
String str = "ABthatCDthatBHthatIOthatoo";     
System.out.println(str.split("that").length-1);

From this I got 4. that is right but if last that doesn't have any letter after it then it shows wrong answer '3' as in :

String str = "ABthatCDthatBHthatIOthat";
System.out.println(str.split("that").length-1);

I want to count the occurrence of "that" word in given String.

Naman
  • 27,789
  • 26
  • 218
  • 353

5 Answers5

4

You could specify a limit to account for the final 'empty' token

System.out.println(str.split("that", -1).length-1);
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • 2
    https://stackoverflow.com/a/770069/1746118 – Naman Sep 03 '17 at 11:40
  • Please explain reason of putting -1 here, ("that", -1) @Reimeus – Khushboo Chaudhary Sep 03 '17 at 12:44
  • 1
    All explained in the [javadoc](http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-int-) _If n is non-positive then the pattern will be applied as many times as possible and the array can have any length_ i.e. don't discard empty tokens – Reimeus Sep 03 '17 at 12:51
3

str.split("that").length doesn't count the number of 'that's . It counts the number of words that have 'that' in between them

For example-

class test
{
 public static void main(String args[]) 
 {
     String s="Hi?bye?hello?goodDay";
     System.out.println(s.split("?").length); 
 }
}

This will return 4, which is the number of words separated by "?". If you return length-1, in this case, it will return 3, which is the correct count of the number of question marks.

But, what if the String is : "Hi????bye????hello?goodDay??"; ?

Even in this case, str.split("?").length-1 will return 3, which is the incorrect count of the number of question marks.

The actual functionality of str.split("that //or anything") is to make a String array which has all those characters/words separated by 'that' (in this case).The split() function returns a String array

So, the above str.split("?") will actually return a String array : {"Hi,bye,hello,goodDay"}

str.split("?").length is returning nothing but the length of the array which has all the words in str separated by '?' .

str.split("that").length is returning nothing but the length of the array which has all the words in str separated by 'that' .

Here is my link for the solution of the problem link

Please tell me if you have any doubt.

Whiplash99
  • 70
  • 5
1

Try this

String fullStr = "ABthatCDthatBHthatIOthatoo";
String that= "that";
System.out.println(StringUtils.countMatches(fullStr, that));

use StringUtils from apache common lang, this one https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/src-html/org/apache/commons/lang/StringUtils.html#line.170

Navneet Krishna
  • 5,009
  • 5
  • 25
  • 44
1

Find out position of substring "that" using lastIndexOf() and if its at last position of the string then increment the cout by 1 of your answer.

Mohit Tyagi
  • 2,788
  • 4
  • 17
  • 29
1

I hope this would help

public static void main(String[] args) throws Exception 


{   int count = 0;
        String str = "ABthatCDthatBHthatIOthat"; 
        StringBuffer sc = new StringBuffer(str);

    while(str.contains("that")){

        int aa = str.indexOf("that");
        count++;
        sc = sc.delete(aa, aa+3);
        str = sc.toString();


    }

    System.out.println("count is:"+count);


 }