tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Tools and Libs > Commons Lang3 > Strings > Count of Matches

Count of Matches 

Apache Commons Lang 3.0 is a java library with lot of utilities and reusable components. This requires the library commons-lang3-3.0.1.jar to be in classpath. The following example shows using StringUtils.countMatches() API. It returns the count of matches of the sub sequence in the given char sequence.

File Name  :  
com/bethecoder/tutorials/commons_lang/tests/strings/CountMatchesTest.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.commons_lang.tests.strings;

import org.apache.commons.lang3.StringUtils;

public class CountMatchesTest {

  /**
   @param args
   */
  public static void main(String[] args) {
         
    System.out.println(StringUtils.countMatches(null, "ab"));
    System.out.println(StringUtils.countMatches("""ab"));
    System.out.println(StringUtils.countMatches("abcaabbabbcaabb""ab"));
    System.out.println(StringUtils.countMatches("abcaabbabbcaabb"""));
    System.out.println(StringUtils.countMatches("abcaabbabbcaabb""xy"));
    System.out.println(StringUtils.countMatches("zxyxzyzxyz""xy"));
  }

}
   

It gives the following output,
0
0
4
0
0
2


                    Print

   



Books Recommendation

 
 
bl  br