tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Tools and Libs > Google Guava > Basic > Char Matcher Trim From

Char Matcher Trim From 

Google Guava is a java library with lot of utilities and reusable components. This requires the library guava-10.0.jar to be in classpath. The following example shows using CharMatcher.trimFrom() API. It returns a string by removing matching characters from the beginning and from the end of the string.

File Name  :  
com/bethecoder/tutorials/guava/base_tests/CharMatcherTrimTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.guava.base_tests;

import com.google.common.base.CharMatcher;

public class CharMatcherTrimTest {

  /**
   @param args
   */
  public static void main(String[] args) {
    System.out.println(CharMatcher.JAVA_LETTER.trimFrom("abc1234xyz"));
    System.out.println(CharMatcher.JAVA_LETTER.trimFrom("abc12aaaa34xyz"));
    System.out.println(CharMatcher.anyOf("zxac").trimFrom("abc12aaaa34xyz"));
    
    String str = "  abcd  ";
    System.out.println(CharMatcher.inRange('\0'' ').trimFrom(str));
    System.out.println(str.trim());
    
    System.out.println(CharMatcher.inRange('\0'' ').trimFrom(str).equals(str.trim()));
  }
}
   

It gives the following output,
1234
12aaaa34
bc12aaaa34xy

abcd
abcd

true



 
  


  
bl  br