zoukankan      html  css  js  c++  java
  • 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
    

      

  • 相关阅读:
    Apple Watch知识点总结
    segue场景跳转的使用总结
    iOS地图相关知识点总结
    第三方库AFNetwork的作用和用法详解
    UIImagePickerController的知识点总结
    关于图片的压缩问题
    盒子模型知识
    CSS3新增属性
    ps命令详解
    http请求中的Referer的作用
  • 原文地址:https://www.cnblogs.com/vigarbuaa/p/2616007.html
Copyright © 2011-2022 走看看