Example usage for org.apache.commons.lang CharSet getInstance

List of usage examples for org.apache.commons.lang CharSet getInstance

Introduction

In this page you can find the example usage for org.apache.commons.lang CharSet getInstance.

Prototype

public static CharSet getInstance(String setStr) 

Source Link

Document

Factory method to create a new CharSet using a special syntax.

  • null or empty string ("") - set containing no characters
  • Single character, such as "a" - set containing just that character
  • Multi character, such as "a-e" - set containing characters from one character to the other
  • Negated, such as "^a" or "^a-e" - set containing all characters except those defined
  • Combinations, such as "abe-g" - set containing all the characters from the individual sets

The matching order is:

  1. Negated multi character range, such as "^a-e"
  2. Ordinary multi character range, such as "a-e"
  3. Negated single character, such as "^a"
  4. Ordinary single character, such as "a"

Matching works left to right.

Usage

From source file:MainClass.java

public static void main(String[] args) {
    //Create a new vowel character set
    CharSet vChs = CharSet.getInstance("aeiou");

    String strTest = "The quick brown fox jumps over the lazy dog.";
    int iVowelCnt = 0;
    int iTestLen = strTest.length();

    for (int i = 0; i < iTestLen; i++) {
        if (vChs.contains(strTest.charAt(i))) {
            iVowelCnt++; //increment count on a vowel being found
        }//from  ww  w  .  j ava 2  s .co  m
    }
    System.out.println("String >>" + strTest);
    System.out.println("Number of vowels in the string is " + iVowelCnt);
}

From source file:CharSetExampleV1.java

public static void main(String args[]) {
    CharSet set = CharSet.getInstance("the apprentice");
    System.err.println(set.contains('q'));
    CharRange[] range = set.getCharRanges();

    System.err.println(ArrayUtils.toString(range));
}

From source file:VowelCharSetTrial.java

public static void main(String[] args) {
    // Create a new vowel character set
    CharSet vChs = CharSet.getInstance("aeiou");

    String strTest = "The quick brown fox jumps over the lazy dog.";
    int iVowelCnt = 0;
    int iTestLen = strTest.length();

    for (int i = 0; i < iTestLen; i++) {
        if (vChs.contains(strTest.charAt(i))) {
            iVowelCnt++; // increment count on a vowel being found
        }/*from w ww.  ja  va2  s .com*/
    }
    System.out.println("String >>" + strTest);
    System.out.println("Number of vowels in the string is " + iVowelCnt);
}