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.在编译之后的目录查看加载的包版 ...
随机推荐
- 响应式 和 移动 web
移动web 教程:http://www.imooc.com/learn/494 iphone5 问题一:6401136的图片,能否在iphone5上完全显示? chrome下 iphone5:3205 ...
- [mysql] mysql 查询语句收集
// 1. 筛选出当天的中奖名单 $where = " date_format(f_ctime,'%Y-%m-%d') = current_date()"; ...
- Linux下SVN配置hook经验总结
前几天给实验室搭建了一个内部测试的开发环境,LAMP.svn提交以及自动部署. 之前没干过这事儿,到最终搞定还是颇费了些周折.总结一下我的经验,主要是hook的自动执行问题. 拿我的post-comm ...
- CentOS使用vsftpd开启FTP服务以及配置用户
1.安装服务 #yum install vsftpd 2.配置 #vi /etc/vsftpd/vsftpd.conf # 禁止匿名访问 anonymous_enable=NO # 允许本地用户登录F ...
- 三、Kubernetes之深入了解Pod
1.yaml格式的Pod配置文件内容及注解 深入Pod之前,首先我们来了解下Pod的yaml整体文件内容及功能注解. 如下: # yaml格式的pod定义文件完整内容: apiVersion: v ...
- C#后台代码获取程序集资源文件
资源会被打包在程序集内部. 选择这种生成方式后,该资源文件会被嵌入到该应用的程序集中,就是说打开生成的应用程序目录是看不到这个文件的. 可以用相对于当前的XAML文件的相对Uri访问,<Imag ...
- 一步一步学习Swift之(三):巧用AutoLayout布局
一些初学者经常在使用autoLayout时,做得效果不太理想,经常会出现界面错乱的情况. 本文章用一个小实例说明autoLayout的使用 非常的简单,只要记住 规则就可以使界面适屏布局,适配各种ip ...
- this与$(this)对象
this与$(this)对象.前者是Javascript对象,而后者是jQuery是对象.两者分清楚,它们只能使用自己的方法.Javascript对象使用Javascript的方法,jQuery对象使 ...
- FFmpeg的安装与使用
1.概述 FFmpeg是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.采用LGPL或GPL许可证.它提供了录制.转换以及流化音视频的完整解决方案.它包含了非常先进的音频/视频 ...
- jzoj4235 序列
取前50個數暴力即可 #include<bits/stdc++.h> using namespace std; int n,m,a[100010],q[5]; int main(){ sc ...