StringUtils工具类
StringUtils源码,使用的是commons-lang3-3.1包。
下载地址 http://commons.apache.org/lang/download_lang.cgi
以下是StringUtils的各项用法
1.空字符串检查
使用函数:StringUtils.isBlank(testString)
函数介绍: 当testString为空,长度为零或者仅由空白字符(whitespace)组成时,返回True;否则返回False
例程:
System.out.println(StringUtils.isBlank(""));//true
System.out.println( StringUtils.isBlank("\n\n\t") );//true
System.out.println( StringUtils.isBlank(null) );//true
函数StringUtils.isNotBlank(testString)的功能与StringUtils.isBlank(testString)相反.
2.清除空白字符(String.trim())
使用函数: StringUtils.trimToNull(testString)
函数介绍:清除掉testString首尾的空白字符,如果仅testString全由空白字符(whitespace)组成则返回null
例程:
System.out.println( StringUtils.trimToNull("\t") );//null
System.out.println( StringUtils.trimToNull(" A Test ") );//A Test
System.out.println( StringUtils.trimToNull(null) );//null
注意:函数StringUtils.trim(testString)与StringUtils.trimToNull(testString)功能类似,但testString由空白字符(whitespace)组成时返回零长度字符串。
3.截取字符串(String.subString())
使用函数: StringUtils.abbreviate(testString,width)和StringUtils.abbreviate(testString,offset,width)
函数介绍:在给定的width内取得testString的缩写,当testString的长度小于width则返回原字符串.
例程:
String test = "This is a test of the abbreviation.";
System.out.println( StringUtils.abbreviate( test, 15 ) );//This is a te...
System.out.println( StringUtils.abbreviate( test, 5,15 ) );//...is a test...
System.out.println( StringUtils.abbreviate( "Test", 10 ) );//Test
4.拆分字符串(String.split)
使用函数: StringUtils.split(testString,splitChars,arrayLength)
函数介绍:splitChars中可以包含一系列的字符串来劈分testString,并可以设定得到数组的长度.注意设定长度arrayLength和劈分字符串间有抵触关系,建议一般情况下不要设定长度.
例程:
String input = "A b,c.d|e";
String input2 = "Pharmacy, basketball funky";
String[] array1 = StringUtils.split( input, " ,.|");//{A,b,c,d,e}
String[] array2 = StringUtils.split( input2, " ,", 2 );//{Pharmacy,basketball funky}
5.查找嵌套字符串
使用函数:StringUtils.substringBetween(testString,header,tail)
函数介绍:在testString中取得header和tail之间的字符串。不存在则返回空
例程:
String htmlContent = "ABC1234ABC4567";
System.out.println(StringUtils.substringBetween(htmlContent, "1234", "4567"));//ABC
System.out.println(StringUtils.substringBetween(htmlContent, "12345", "4567"));//null
6.去除尾部换行符
使用函数:StringUtils.chomp(testString)
函数介绍:去除testString尾部的换行符
例程:
String input = "Hello\n";
System.out.println( StringUtils.chomp( input ));//Hello
String input2 = "Another test\r\n";
System.out.println( StringUtils.chomp( input2 ));//Another test
7.重复字符串
使用函数:StringUtils.repeat(repeatString,count)
函数介绍:得到将repeatString重复count次后的字符串
例程:
System.out.println( StringUtils.repeat( "China ", 5));//China China China China China
其他函数:
StringUtils.center( testString, count,repeatString );把testString插入将repeatString重复多次后的字符串中间,得到总长为count的字符串
StringUtils.center(String str, int size); 默认以空格填充
StringUtils.leftPad(String str,int size); 左侧空格填充
StringUtils.leftPad(String str,int size,String padStr);左侧字符串填充
StringUtils.rightPad(String str,int size); 左侧空格填充
StringUtils.rightPad(String str,int size,String padStr);左侧字符串填充
例程:
System.out.println( StringUtils.center( "China", 11,"*"));//***China***
StringUtils.leftPad("abc", 10, "*");//*******abc
8.颠倒字符串顺序
使用函数:StringUtils.reverse(testString)
函数介绍:得到testString中字符颠倒后的字符串
例程:
System.out.println( StringUtils.reverse("ABCDE"));//EDCBA
9.判断字符串内容的类型
函数介绍:
StringUtils.isNumeric( testString ) :如果testString全由数字组成返回True
StringUtils.isAlpha( testString ) :如果testString全由字母组成返回True
StringUtils.isAlphanumeric( testString ) :如果testString全由数字或数字组成返回True
StringUtils.isAlphaspace( testString ) :如果testString全由字母或空格组成返回True
StringUtils.isAlphanumericSpace(String str);如果testString全由字母数字和空格组成返回True
StringUtils.isNumericSpace(String str);如果testString全由数字和空格组成返回True
例程:
String state = "Virginia";
System.out.println( StringUtils.isNumeric(state ) );//false
System.out.println( StringUtils.isAlpha( state ) );//true
System.out.println( StringUtils.isAlphanumeric( state ) );//true
System.out.println( StringUtils.isAlphaSpace( state ) );//true
10.取得某字符串在另一字符串中出现的次数
使用函数:StringUtils.countMatches(testString,seqString)
函数介绍:取得seqString在testString中出现的次数,未发现则返回零
例程:
System.out.println(StringUtils.countMatches( "Chinese People", "e" ));//4
11.部分截取字符串
使用函数:
StringUtils.substringBetween(testString,fromString,toString ):取得两字符之间的字符串
StringUtils.substringAfter( ):取得指定字符串后的字符串
StringUtils.substringBefore( ):取得指定字符串之前的字符串
StringUtils.substringBeforeLast( ):取得最后一个指定字符串之前的字符串
StringUtils.substringAfterLast( ):取得最后一个指定字符串之后的字符串
例程:
String formatted = " 25 * (30,40) [50,60] | 30";
System.out.print(StringUtils.substringBeforeLast( formatted, "*" ));//25
System.out.print(StringUtils.substringBetween( formatted, "(", "," ));//30
System.out.print(StringUtils.substringBetween( formatted, ",", ")" ));//40
System.out.print(StringUtils.substringBetween( formatted, "[", "," ));//50
System.out.print(StringUtils.substringBetween( formatted, ",", "]" ));//40) [50,60
System.out.print(StringUtils.substringAfterLast( formatted, "|" ));//30
12. 缩进字符串:
StringUtils.abbreviate(String str, int maxWidth) 缩进字符串,第二个参数至少为4(包括...)
例程:
StringUtils.abbreviate("abcdefg", 20);//abcdefg (正常显示)
StringUtils.abbreviate("abcdefg", 4);//a...
13. 首字母大写:
StringUtils.capitalize(String str) 首字母大写
StringUtils.uncapitalize(String str)首字母小写
例程:
StringUtils.capitalize("abcdefg");//Abcdefg
14. 是否全是大写,是否全是小写(3.0版本)
StringUtils.isAllLowerCase(String str);//是否全是大写
StringUtils.isAllUpperCase(String str);//是否全是小写
例程:
StringUtils.isAllLowerCase("abC");//false
15. 大小写转换,空格不动
StringUtils.swapCase(String str);//大小写转换
例程:
StringUtils.swapCase("I am a-A*a")//i AM A-a*A
StringUtils工具类的更多相关文章
- Spring的StringUtils工具类
本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:<Spring的StringUtils工具类> org.springframework.util.StringU ...
- StringUtils工具类常用方法汇总2(截取、去除空白、包含、查询索引)
在上一篇中总结了StringUtils工具类在判断字符串为空,大小写转换,移除字符或字符序列,替换,反转,切割合并等方面的方法,这次再汇总一下其它常用的方法. 一.截取 StringUtils ...
- StringUtils工具类常用方法汇总1(判空、转换、移除、替换、反转)
Apache commons lang3包下的StringUtils工具类中封装了一些字符串操作的方法,非常实用,使用起来也非常方便.最近自己也经常在项目中使用到了里面的一些方法,在这里将常用的方 ...
- StringUtils工具类常用方法
前言:工作中看到项目组里的大牛写代码大量的用到了StringUtils工具类来做字符串的操作,便学习整理了一下,方便查阅. isEmpty(String str) 是否为空,空格字符为false is ...
- StringUtils工具类常用方法汇总(判空、转换、移除、替换、反转)
Apache commons lang3包下的StringUtils工具类中封装了一些字符串操作的方法,非常实用,使用起来也非常方便.最近自己也经常在项目中使用到了里面的一些方法,在这里将常用的方法总 ...
- 基于StringUtils工具类的常用方法介绍(必看篇)
前言:工作中看到项目组里的大牛写代码大量的用到了StringUtils工具类来做字符串的操作,便学习整理了一下,方便查阅. isEmpty(String str) 是否为空,空格字符为false is ...
- spring util包 StringUtils工具类中的isEmpty() 方法解析
今天在公司看到同事写的代码,无意发现在判断字符串类型时,使用的是StringUtils工具类中的isEmpty()去判断如下所示 @RequestMapping(value = "/pub/ ...
- StringUtils工具类常用方法汇总:判空、转换、移除、替换、反转。
Apache commons lang3包下的StringUtils工具类中封装了一些字符串操作的方法,非常实用,使用起来也非常方便.最近自己也经常在项目中使用到了里面的一些方法,在这里将常用的方法总 ...
- 通过CollectionUtils工具类判断集合是否为空,通过StringUtils工具类判断字符串是否为空
通过CollectionUtils工具类判断集合是否为空 先引入CollectionUtils工具类: import org.apache.commons.collections4.Collectio ...
- 利用StringUtils工具类进行String为空的判断
利用工具类进行String类型数据的非空判断,让自己的项目代码变得更加的简洁明了. 判断某字符串是否为空,为空的标准是 str==null 或 str.length()==0 下面是 St ...
随机推荐
- ubuntu 配置git公钥
Git是分布式的代码管理工具,远程的代码管理是基于SSH的,所以要使用远程的Git则需要SSH的配置. github的SSH配置如下: 一 . 设置Git的user name和email: $ git ...
- 1Z0-053 争议题目解析419
1Z0-053 争议题目解析419 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 419.In Oracle 11g, by default which one of the fo ...
- 【集合框架】JDK1.8源码分析之TreeMap(五)
一.前言 当我们需要把插入的元素进行排序的时候,就是时候考虑TreeMap了,从名字上来看,TreeMap肯定是和树是脱不了干系的,它是一个排序了的Map,下面我们来着重分析其源码,理解其底层如何实现 ...
- jQuery-1.9.1源码分析系列(三) Sizzle选择器引擎——一些有用的Sizzle API
说一下Sizzle中零碎的API.这些API有的被jQuery接管,直接使用jQuery.xxx就可以使用,有的没有被接管,如果要在jQuery中使用,使用方法是jQuery.find.xxx. 具体 ...
- [VB.NET]调用API获取/设置键盘按键状态
1.调用GetAsyncKeyState()获取指定按键的状态,GetActiveKey()检索指定范围内的按键状态 2.调用keybd_event()可合成一次击键事件,通常两次击键事件间需要设定时 ...
- 五小步让VS Code支持AngularJS智能提示
本文想通过配置VS Code来实现对AngularJS的智能提示.在一般的情况下对于在HTML页面是支持提示的.但是在js页面就不是很友好,它是记忆你之前的输入,要是之后有重复的输入,VS Code会 ...
- 怎样制作web版的folder treeview
文件夹treeview的效果 这样的treeview在实际项目中使用的场景较多. 既然用的多,那就DIY一遍,虽没有面面俱到,但也要将其基本实现完成一遍. 1.先准备图标素材 file.gif,文件 ...
- [Asp.net 5] Localization-Asp.net运行时多语言
本节介绍的是Microsoft.AspNet.Localization工程.该工程是运行在Asp.net 5环境中的运行时多语言设置. ASP.net 5中间件技术 在新的Asp.net 5中,可以将 ...
- 纯css3天气动画场景特效
CSS3超强大,以下是纯用CSS3+HTML实现的场景效果图: 查看效果:http://hovertree.com/h/bjaf/cssrotate.htm css3 3d展示中rotate()介绍与 ...
- div自适应布局
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="OnlineShowPage ...