检查字符串是否为空或null或仅仅包含空格
  String test = "";
  String test1=" ";
  String test2 = "\n\n\t";
  String test3 = null;
  System.out.println( "test blank? " + StringUtils.isBlank( test ) );
  System.out.println( "test1 blank? " + StringUtils.isBlank( test1 ) );
  System.out.println( "test2 blank? " + StringUtils.isBlank( test2 ) );
  System.out.println( "test3 blank? " + StringUtils.isBlank( test3 ) );
  运行结果:
  test blank? true
  test1 blank? true
  test2 blank? true
  test3 blank? true
  相对应的还有一个StringUtils.isNotBlank(String str)
  StringUtils.isEmpty(String str)则检查字符串是否为空或null(不检查是否仅仅包含空格)
 
  分解字符串
  StringUtils.split(null, *, *)            = null
  StringUtils.split("", *, *)              = []
  StringUtils.split("ab de fg", null, 0)   = ["ab", "cd", "ef"]
  StringUtils.split("ab   de fg", null, 0) = ["ab", "cd", "ef"]
  StringUtils.split("ab:cd:ef", ":", 0)    = ["ab", "cd", "ef"]
  StringUtils.split("ab:cd:ef", ":", 1)    = ["ab:cd:ef"]
  StringUtils.split("ab:cd:ef", ":", 2)    = ["ab", "cd:ef"]
  StringUtils.split(String str,String separatorChars,int max) str为null时返回null
  separatorChars为null时默认为按空格分解,max为0或负数时分解没有限制,max为1时返回整个字符串,max为分解成的个数(大于实际则无效)
 
  去除字符串前后指定的字符
  StringUtils.strip(null, *)          = null
  StringUtils.strip("", *)            = ""
  StringUtils.strip("abc", null)      = "abc"
  StringUtils.strip(" abc ", null)    = "abc"
  StringUtils.strip("  abcyx", "xyz") = "  abc"
  StringUtils.strip(String str,String stripChars) str为null时返回null,stripChars为null时默认为空格

创建醒目的Header(调试时用)
  public String createHeader( String title ) {
    int width = 30;
    String stars = StringUtils.repeat( "*", width);
    String centered = StringUtils.center( title, width, "*" );
    String heading = StringUtils.join(new Object[]{stars, centered, stars}, "\n");
    return heading;
  }
  调用createHeader("TEST")的输出结果:
  ******************************
  ************ TEST ************
  ******************************

字符的全部反转及以单个词为单位的反转
  String original = "In time, I grew tired of his babbling nonsense.";
  StringUtils.reverse( original )   = ".esnesnon gnilbbab sih fo derit werg I ,emit nI"
  以单个词为单位的反转
  public Sentence reverseSentence(String sentence) {
    String reversed = StringUtils.chomp( sentence, "." );
    reversed = StringUtils.reverseDelimited( reversed, ' ' );
    reversed = reversed + ".";
    return reversed;
  }
  String sentence = "I am Susan."
  reverseSentence( sentence ) )   = "Susan am I."

检查字符串是否仅仅包含数字、字母或数字和字母的混合
  String test1 = "ORANGE";
  String test2 = "ICE9";
  String test3 = "ICE CREAM";
  String test4 = "820B Judson Avenue";
  String test5 = "1976";
  结果:
  boolean t1val = StringUtils.isAlpha( test1 ); // returns true
  boolean t2val = StringUtils.isAlphanumeric( test2 ); // returns true
  boolean t3val = StringUtils.isAlphaSpace( test3 ); // returns true
  boolean t4val = StringUtils.isAlphanumericSpace( test4 ); // returns true
  boolean t5val = StringUtils.isNumeric( test5 ); // returns true

StringUtils类使用 (转载)的更多相关文章

  1. StringUtils类中isEmpty与isBlank的区别

    org.apache.commons.lang.StringUtils类提供了String的常用操作,最为常用的判空有如下两种isEmpty(String str)和isBlank(String st ...

  2. StringUtils类中 isEmpty() 与 isBlank()的区别

    org.apache.commons.lang.StringUtils类提供了String的常用操作,最为常用的判空有如下两种isEmpty(String str)和isBlank(String st ...

  3. org.apache.commons.lang.StringUtils类

    org.apache.commons.lang.StringUtils类 本文摘自:(http://www.blogjava.net/japper/archive/2012/05/23/378946. ...

  4. org.apache.commons.lang3.StringUtils类中isBlank和isEmpty方法的区别

    相信很多java程序员在写代码的时候遇到判断某字符串是否为空的时候会用到StringUtils类中isBlank和isEmpty方法,这两个方法到底有什么区别呢?我们用一段代码来阐述这个区别吧: @T ...

  5. 源码分析八(org.springframework.util包之StringUtils类))

    一:spring框架util包中的StringUtils类主要是处理关于字符串 的功能方法,下面直接结合代码分析: //判断字符串是否为空,如果为nul或者""则返回true,否则 ...

  6. StringUtils类API及使用方法详解

    StringUtils类API及使用方法详解 StringUtils方法概览 判空函数 1)StringUtils.isEmpty(String str) 2)StringUtils.isNotEmp ...

  7. ListView优化为何ViewHolder用static类(转载)

    如果有人还不了解ViewHolder为什么可以起到优化作用,我这边再做下简单说明:Android的findViewById动作是比较耗时的,需要遍历布局的树形结构,才能找到相应的视图.所以如果想在这一 ...

  8. 在commons-lang3包中StringUtils类的ordinalIndexOf中有一个错误

    * StringUtils.ordinalIndexOf(null, *, *) = -1 * StringUtils.ordinalIndexOf(*, null, *) = -1 * String ...

  9. C#工具类:Json操作帮助类(转载)

    原文转载自C#工具类:Json操作帮助类_IT技术小趣屋. Json序列化和反序列化在程序开发中时常会遇到,在C#中可以使用很多种方法实现对数据的Json序列化和反序列化,封装一个Json操作工具类来 ...

随机推荐

  1. P2622 关灯问题II (状态压缩,最短路)

    题目链接 Solution 这道题算是很经典的状压问题了,好题. 考虑到 \(n\) 的范围仅为 \(10\) , 那么也就是说所有状态压起来也只有 \(1024\) 种情况. 然后我们发现 \(m\ ...

  2. linux查找文件命令

    (2)find /etc -name httpd.conf #在/etc目录下文件httpd.conf

  3. 使用docker Maven插件本地构建docker镜像并发布到远程服务器

    1.登录网站https://start.spring.io/,生成一个基本的SpringBoot应用. 2.将应用导入Eclipse IDE并创建Application类.目录结构如下: Applic ...

  4. 网络流24题-最长k可重线段集问题

    最长k可重线段集问题 时空限制1000ms / 128MB 题目描述 给定平面 x−O−y 上 n 个开线段组成的集合 I,和一个正整数 k .试设计一个算法,从开线段集合 I 中选取出开线段集合 S ...

  5. 哈工大CSAPP大作业

    第1章 概述 1.1 Hello简介 hello的源码hello.c文件,要生成可执行文件,首先要进行预处理,其次要进行编译生成汇编代码,接着进行汇编处理生成目标文件,目标文件通过链接器形成一个可执行 ...

  6. 一款多功能的移动端滚动选择器,支持单选到多选、支持多级级联、提供自定义回调函数、提供update函数二次渲染、重定位函数、兼容pc端拖拽等等..

    https://github.com/onlyhom/mobileSelect.js/blob/master/docs/README-CN.md mobileSelect.js 一款多功能的移动端滚动 ...

  7. 共享内存之——mmap内存映射

    共享内存允许两个或多个进程共享一给定的存储区,因为数据不需要来回复制,所以是最快的一种进程间通信机制.共享内存可以通过mmap()映射普通文件 (特殊情况下还可以采用匿名映射)机制实现,也可以通过sy ...

  8. #define xxx do{...} while(0) 宏定义

    linux内核和其他一些开源的代码中,经常会遇到这样的代码: do{ ... }while(0) 这样的代码一看就不是一个循环,do..while表面上在这里一点意义都没有,那么为什么要这么用呢? 实 ...

  9. 二、 java中的变量与数据类型及类型转换

    标识符:凡是可以自己命名的地方都叫标识符,如:类名.方法名.接口名... 1.标识符命名的规则: 由26个英文字母大小写,0-9,_或$组成,不遵守会报错. 不可以用数字开头. 不能使用关键字和保留字 ...

  10. LeetCode OJ——Word Ladder2

    http://oj.leetcode.com/problems/word-ladder-ii/ class Solution { public: vector<vector<string& ...