posts - 41, comments - 15, trackbacks - 0, articles - 1
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

StringUtils一般应用

Posted on 2012-08-30 11:02 yuhaibo736 阅读(566) 评论(0)  编辑  收藏

package test;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

public class TestStringUtils {
 
 public static void main(String[] args) {
  // 空字符串检查
  String t1_1 = "";
  String t1_2 = "\n\n\t";
  String t1_3 = null;
  String t1_4 = "Test";
  System.out.println("t1_1 blank? " + StringUtils.isBlank(t1_1));
  System.out.println("t1_2 blank? " + StringUtils.isBlank(t1_2));
  System.out.println("t1_3 blank? " + StringUtils.isBlank(t1_3));
  System.out.println("t1_4 blank? " + StringUtils.isBlank(t1_4));
  // 清除空白字符
  String t2_1 = "\t";
  String t2_2 = "  A  Test  ";
  String t2_3 = null;
  System.out.println("t2_1 trimToNull: " + StringUtils.trimToNull(t2_1));
  System.out.println("t2_2 trimToNull: " + StringUtils.trimToNull(t2_2));
  System.out.println("t2_3 trimToNull: " + StringUtils.trimToNull(t2_3));
  System.out.println("t2_1 trim: " + StringUtils.trim(t2_1));
  System.out.println("t2_2 trim: " + StringUtils.trim(t2_2));
  System.out.println("t2_3 trim: " + StringUtils.trim(t2_3));
  // 取得字符串的缩写
  String t3_1 = "This is a test of the abbreviation.";
  String t3_2 = "Test";
  System.out.println(StringUtils.abbreviate(t3_1, 15));
  System.out.println(StringUtils.abbreviate(t3_1, 5, 15));
  System.out.println(StringUtils.abbreviate(t3_2, 10));
  // 劈分字符串
  String t4_1 = "A b,c.d|e";
  String t4_2 = "Pharmacy, basketball funky";
  String[] array1 = StringUtils.split(t4_1, " ,.|");
  String[] array2 = StringUtils.split(t4_2, " ,", 2);
  System.out.println(ArrayUtils.toString(array1));
  System.out.println(ArrayUtils.toString(array2));
  // 查找嵌套字符串
  String t5_1 = "ABC1234ABC4567";
  System.out.println(StringUtils.substringBetween(t5_1, "1234", "4567"));
  System.out.println(StringUtils.substringBetween(t5_1, "12345", "4567"));
  // 去除尾部换行符
  String t6_1 = "Hello\n";
  System.out.println(StringUtils.chomp(t6_1));
  String t6_2 = "Another test\r\n";
  System.out.println(StringUtils.chomp(t6_2));
  // 重复字符串
  System.out.println(StringUtils.repeat("*", 10));
  System.out.println(StringUtils.repeat("China ", 5));
  // 颠倒字符串
  System.out.println(StringUtils.reverse("ABCDE"));
  // 判断字符串内容的类型
  String t7_1 = "Virginia";
  System.out.println("Is state number? " + StringUtils.isNumeric(t7_1));// 全由数字组成
  System.out.println("Is state alpha? " + StringUtils.isAlpha(t7_1));// 全由字母组成
  System.out.println("Is state alphanumeric? " + StringUtils.isAlphanumeric(t7_1));// 全由数字或数字组成
  System.out.println("Is state alphaspace? " + StringUtils.isAlphaSpace(t7_1));// 全由字母或空格组成
  // 取得某字符串在另一字符串中出现的次数
  System.out.println(StringUtils.countMatches("Chinese People", "e"));
  // 部分截取字符串
  String t8_1 = " 25 * (30,40) [50,60] | 30";
  System.out.print("N0: " + StringUtils.substringBeforeLast(t8_1, "*"));
  System.out.print(", N1: " + StringUtils.substringBetween(t8_1, "(", ","));
  System.out.print(", N2: " + StringUtils.substringBetween(t8_1, ",", ")"));
  System.out.print(", N3: " + StringUtils.substringBetween(t8_1, "[", ","));
  System.out.print(", N4: " + StringUtils.substringBetween(t8_1, ",", "]"));
  System.out.print(", N5: " + StringUtils.substringAfterLast(t8_1, "|"));
 }

}


只有注册用户登录后才能发表评论。


网站导航: