Java StringUtil 用法示例

package com.xx.zhj.Study;

import junit.framework.TestCase;

import org.apache.commons.lang.StringUtils;

public class StringUtilTest extends TestCase {
	  public void testStringUtils()
	    {
	 	// data setup   
	        String str1 = "";   
	        String str2 = " ";   
	        String str3 = "\t";   
	        String str4 = null;   
	        String str5 = "123";   
	        String str6 = "ABCDEFG";   
	        String str7 = "It feels good to use StringUtils \r\n";   
	           
	        // check for empty strings   检查字符串是否为空或null或仅仅包含空格
	        System.out.println("==============================");   
	        System.out.println("Is str1 blank? " + StringUtils.isBlank(str1));   
	        System.out.println("Is str2 blank? " + StringUtils.isBlank(str2));   
	        System.out.println("Is str3 blank? " + StringUtils.isBlank(str3));   
	        System.out.println("Is str4 blank? " + StringUtils.isBlank(str4));   
	           
	        // check for numerics   检查字符串是否仅仅包含数字
	        System.out.println("==============================");   
	        System.out.println("Is str5 numeric? " + StringUtils.isNumeric(str5));   
	        System.out.println("Is str6 numeric? " + StringUtils.isNumeric(str6));   
	           
	        // reverse strings / whole words   
	        System.out.println("==============================");   
	        System.out.println("str6: " + str6);   
	      //字符的全部反转
	        System.out.println("str6 reversed: " + StringUtils.reverse(str6));   
	        System.out.println("str7: " + str7);   
	        String str8 = StringUtils.chomp(str7);   
	        str8 = StringUtils.reverseDelimited(str8, ' ');   
	        System.out.println("str7 reversed whole words : \r\n" + str8);         
	       
	       	// build header (useful to print log messages that are easy to locate)   
	        System.out.println("==============================");   
	        System.out.println("print header:");   
	        String padding = StringUtils.repeat("=", 50); 
	        String msg = StringUtils.center(" Customised Header ", 50, "%");   //使一个字符串居中(在输出的时候)
	        Object[] raw = new Object[]{padding, msg, padding};   
	        String header = StringUtils.join(raw, "\r\n");   
	        System.out.println(header);   
	        
	        //数字前后加入某符号
	        System.out.println(StringUtils.leftPad("34", 8, "0"));
	    }   
}

 输出

==============================
Is str1 blank? true
Is str2 blank? true
Is str3 blank? true
Is str4 blank? true
==============================
Is str5 numeric? true
Is str6 numeric? false
==============================
str6: ABCDEFG
str6 reversed: GFEDCBA
str7: It feels good to use StringUtils 

str7 reversed whole words : 
StringUtils use to good feels It
==============================
print header:
==================================================
%%%%%%%%%%%%%%% Customised Header %%%%%%%%%%%%%%%%
==================================================
00000034

  

posted @ 2012-07-30 22:34  vigarbuaa  阅读(1460)  评论(0编辑  收藏  举报