org.apache.commons.lang.StringUtils的常用方法
org.apache.commons.lang.StringUtils是apache的commons-lang-x.x.jar下的包,里面包含很多字符串操作方法,
官网(http://commons.apache.org/proper/commons-lang/javadocs/api-release/index.html)介绍的常用方法如下:
public class StringUtils
extends Object
Operations on String that are null safe.
- IsEmpty/IsBlank - checks if a String contains text
- Trim/Strip - removes leading and trailing whitespace
- Equals/Compare - compares two strings null-safe
- startsWith - check if a String starts with a prefix null-safe
- endsWith - check if a String ends with a suffix null-safe
- IndexOf/LastIndexOf/Contains - null-safe index-of checks
- IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut - index-of any of a set of Strings
- ContainsOnly/ContainsNone/ContainsAny - does String contains only/none/any of these characters
- Substring/Left/Right/Mid - null-safe substring extractions
- SubstringBefore/SubstringAfter/SubstringBetween - substring extraction relative to other strings
- Split/Join - splits a String into an array of substrings and vice versa
- Remove/Delete - removes part of a String
- Replace/Overlay - Searches a String and replaces one String with another
- Chomp/Chop - removes the last part of a String
- AppendIfMissing - appends a suffix to the end of the String if not present
- PrependIfMissing - prepends a prefix to the start of the String if not present
- LeftPad/RightPad/Center/Repeat - pads a String
- UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize - changes the case of a String
- CountMatches - counts the number of occurrences of one String in another
- IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable - checks the characters in a String
- DefaultString - protects against a null input String
- Rotate - rotate (circular shift) a String
- Reverse/ReverseDelimited - reverses a String
- Abbreviate - abbreviates a string using ellipsis or another given String
- Difference - compares Strings and reports on their differences
- LevenshteinDistance - the number of changes needed to change one String into another
部分方法的示例:
package com.led.test;
import org.apache.commons.lang.StringUtils;
public class Test3 {
@SuppressWarnings("deprecation")
public static void main(String[] args) {
//找到2个字符串第一个出现不同的位置(1开始)
String difference = StringUtils.difference("s123", "s13");
System.out.println(difference);//3
//判断2个字符串是否相等
boolean equals = StringUtils.equals("s1", "s1");
System.out.println(equals);//true
//判断字符串里面是否含有特定字符串
boolean b2 = StringUtils.contains("asd", "as");
System.out.println(b2);//true
//把数组的元素用:进行拼接
String concatStr = StringUtils.join(new String[]{"dog", "cat", "monkey"},":");
System.out.println(concatStr);//dog:cat:monkey
//根据特定分隔符对字符串进行拆分
String[] split = StringUtils.split("apple|xiaomi|dell|lenovo", "|");
for (String s1 : split) {
System.out.print(s1 + "、");//apple、xiaomi、dell、lenovo、
}
System.out.println();
//所有单词首字母大写
String capitaliseAllWords = StringUtils.capitaliseAllWords("today i will go to china");
System.out.println(capitaliseAllWords);//Today I Will Go To China
//统计某个字符串在字符串出现的次数
int matchCount = StringUtils.countMatches("Happy Birthday to you", "o");
System.out.println(matchCount);//2
//必须要8位,不够的就拿0去字符串左边补
String leftPad = StringUtils.leftPad("54", 8, "0");
System.out.println(leftPad);//00000054
//必须要8位,不够的就拿0去字符串右边补
String rightPad = StringUtils.rightPad("54", 8, "0");
System.out.println(rightPad);//54000000
//判断字符串是否以特定字符串开头,区分大小写
boolean startsWith = StringUtils.startsWith("GoodMorning", "go");
System.out.println(startsWith);//false
//判断字符串是否以特定字符串开头,区分大小写
boolean endsWith = StringUtils.endsWith("GoodMorning", "ing");
System.out.println(endsWith);//true
}
}
org.apache.commons.lang.StringUtils的常用方法的更多相关文章
- org.apache.commons.lang.StringUtils类
org.apache.commons.lang.StringUtils类 本文摘自:(http://www.blogjava.net/japper/archive/2012/05/23/378946. ...
- org.apache.commons.lang.StringUtils中常用的方法
org.apache.commons.lang.StringUtils中常用的方法,这里主要列举String中没有,且比较有用的方法: 1. 检查字符串是否为空: static boolean isB ...
- java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils
java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils Caused by: java.lang.ClassNotFou ...
- org.apache.commons.lang.StringUtils 中 Join 函数
转自 http://my.oschina.net/zenglingfan/blog/134872 写代码的时候,经常会碰到需要把一个List中的每个元素,按逗号分隔转成字符串的需求,以前是自己写一段比 ...
- idea创建maven项目报错,Error initializing: org.codehaus.plexus.velocity.DefaultVelocityComponent@56da52a7 java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils
学着使用idea,想创建个maven项目,但是出师不利,立马报错,贼尴尬,错误信息如下: D:\Develop\JDK\bin\java.exe -Dmaven.multiModuleProjectD ...
- org.apache.commons.lang.StringUtils
org.apache.commons.lang.StringUtils 作为jdk中lang包的补充 检查CharSequence是否为空,null或者空格 CharSequence (CharBuf ...
- maven命令行创建web项目报错:java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils
早上一上班就想新建一个web项目玩玩,没想到一敲命令创建就失败了,真是出师不利.各种折腾无果,当然我也可以用eclipse直接创建的,就是不甘心被这破问题给耍了.刚刚才发现问题原因,这个结果我也是醉了 ...
- apache.commons.lang.StringUtils 使用心得
原文:http://blog.csdn.net/ye_sheng/article/details/48101901?ref=myread 在Java中我们用的最多的类应该就是String了.对于Str ...
- java.lang.NoSuchMethodError: org.apache.commons.lang.StringUtils.endsWith(Ljava/lang/String;Ljava/lang/String;)Z
这是一个包冲突的典型错误,今天搞了一天.从错误信息就能看出是commons.lang出现的问题,解决方案:去掉新增加的conmons.lang依赖,加载其他的版本. 1.在编译之后的目录查看加载的包版 ...
随机推荐
- centos下网口vlan设置
如果要使vlan之间进行通信,我们通常会使用三层交换机或者路由器子接口模式来做.Linux上关于VLAN与Cisco交换机中继连接,也是可以实现其互相之间的通信的. 环境:RHEL 5.2 最小化安装 ...
- Android-xliff
先看以下这个案例,然后在分析: string.xml 使用xliff,需要导入命名空间 xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2& ...
- SQL Server 阻塞原因分析
这里通过连接在sysprocesses里字段值的组合来分析阻塞源头,可以把阻塞分为以下5种常见的类型(见表).waittype,open_tran,status,都是sysprocesses里的值,“ ...
- C# Winform 换肤
本来计划接着上篇 C# Winform模仿百度日历,发现一时半会写不完,只写了一小半还不全,暂且搁置下.现在计划下班后每天至少写一篇博客,未能完成的等周末(不加班都情况)补充完整. 本篇博客窗体换肤, ...
- EF三种编程方式详细图文教程(C#+EF)之Database First
Entity Framework4.1之前EF支持“Database First”和“Model First”编程方式,从EF4.1开始EF开始支持支持“Code First”编程方式,今天简单看一下 ...
- Markdown的学习笔记一
之前学习看些书籍.学些技术都喜欢用xmind做思维导图的笔记,慢慢的发现想把一些笔记做的详细一些就会变得很复杂,个人觉得误了思维导图本意,而且用手机查看的时候也各种不方便.所以开始学习使用markdo ...
- Meet in the middle
搜索是\(OI\)中一个十分基础也十分重要的部分,近年来搜索题目越来越少,逐渐淡出人们的视野.但一些对搜索的优化,例如\(A\)*,迭代加深依旧会不时出现.本文讨论另一种搜索--折半搜索\((meet ...
- Python 关于 encode与decode 中文乱码问题
字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(en ...
- Tree with Small Distances(cf1029E)(树形动规)
You are given an undirected tree consisting of \(n\) vertices. An undirected tree is a connected und ...
- spring cloud学习(三) 断路器
在Spring Cloud中使用了Hystrix 来实现断路器的功能.Hystrix是Netflix开源的微服务框架套件之一,该框架目标在于通过控制那些访问远程系统.服务和第三方库的节点,从而对延迟和 ...