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的常用方法的更多相关文章

  1. org.apache.commons.lang.StringUtils类

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

  2. org.apache.commons.lang.StringUtils中常用的方法

    org.apache.commons.lang.StringUtils中常用的方法,这里主要列举String中没有,且比较有用的方法: 1. 检查字符串是否为空: static boolean isB ...

  3. java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils

    java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils Caused by: java.lang.ClassNotFou ...

  4. org.apache.commons.lang.StringUtils 中 Join 函数

    转自 http://my.oschina.net/zenglingfan/blog/134872 写代码的时候,经常会碰到需要把一个List中的每个元素,按逗号分隔转成字符串的需求,以前是自己写一段比 ...

  5. 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 ...

  6. org.apache.commons.lang.StringUtils

    org.apache.commons.lang.StringUtils 作为jdk中lang包的补充 检查CharSequence是否为空,null或者空格 CharSequence (CharBuf ...

  7. maven命令行创建web项目报错:java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils

    早上一上班就想新建一个web项目玩玩,没想到一敲命令创建就失败了,真是出师不利.各种折腾无果,当然我也可以用eclipse直接创建的,就是不甘心被这破问题给耍了.刚刚才发现问题原因,这个结果我也是醉了 ...

  8. apache.commons.lang.StringUtils 使用心得

    原文:http://blog.csdn.net/ye_sheng/article/details/48101901?ref=myread 在Java中我们用的最多的类应该就是String了.对于Str ...

  9. java.lang.NoSuchMethodError: org.apache.commons.lang.StringUtils.endsWith(Ljava/lang/String;Ljava/lang/String;)Z

    这是一个包冲突的典型错误,今天搞了一天.从错误信息就能看出是commons.lang出现的问题,解决方案:去掉新增加的conmons.lang依赖,加载其他的版本. 1.在编译之后的目录查看加载的包版 ...

随机推荐

  1. iOS笔记之UIKit_UISlider/UIStepper/UISwitch

    - (void)viewDidLoad { [super viewDidLoad]; self.sp = [[UIStepper alloc]init]; //设置计步器的位置 self.sp.cen ...

  2. 关于cxGrid的排序问题

    当然,这个 dcoAnsiSort也很重要,否者不按拼音排序 1.首先需要开启  Views的 OptionsCustomize.ColumnSorting 2.再设置每列的这个 Sorting 3. ...

  3. 分形之皇冠(Crown)

    皇冠分形曲线 核心代码: static void FractalCrown(const Vector3& vStart, const Vector3& vEnd, Vector3* p ...

  4. jQuery---ajax---error函数及其参数详解

    使用jquery的ajax方法向服务器发送请求的时候,常常需要使用到error函数进行错误信息的处理,本文详细说明了ajax中error函数和函数中各个参数的用法. 一般error函数返回的参数有三个 ...

  5. C#: 以管理员权限运行包含有cd命令的.bat文件

    最近在做项目的时候遇到一种情:用C#程序以管理员权限去执行一个bat文件,且此bat文件里面有cd命令来进入文件的下一级目录,比如: echo test begin cd test1 setup1.e ...

  6. netcore的NLog使用小记

    1. 启动应用程序日志配置 修改Program.cs,在WebHostBuilder构建时配置日志 public static IWebHostBuilder CreateWebHostBuilder ...

  7. 1.翻译:EF基础系列--什么是Entity Framework?

    大家好,好久不见,EF系列之前落下了,还是打算重新整理一下. 先说说目前的打算:先简单了解一下EF基础系列-->然后就是EF 6 Code-First系列-->接着就是EF 6 DB-Fi ...

  8. ZKWeb网页框架1.5正式发布

    本次更新的内容有 明显的改进了IoC容器在部分情况下的性能, 当前的性能和Grace, DryIoC同等 添加IHttpResquestHandlerWrapper接口让重载Http上下文更容易 添加 ...

  9. 浏览器标识ua

    # LinuxLinux / Firefox 29: Mozilla/5.0 (X11; Linux x86_64; rv:29.0) Gecko/20100101 Firefox/29.0Linux ...

  10. raspberry pi wifi

    vim /etc/network/interfaces 修改 wpa-ssid 和 wpa-psk