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. datetime & time

    python有两个和时间相关的模块,datetime和time datetime datetime模块下有四个类 date     日期相关的 time          时间相关的 datetime ...

  2. MySQL1安装

    ---恢复内容开始--- (1)安装MySQL ①ZIP安装 ②MSI安装(Windows Installer) Ⅰ  先从官网下载 目前选择使用Typical安装↑ 目前不勾选配置选项↑ 找到配置向 ...

  3. netcore 发布 到 windows server IIS

    net core 和普通net 发布没有什么不同,只需要在个别地方注意下: 1. 在visual Studio 2017 发布 2. 把发布好的文件copy到服务器上,并新建一个网站,同时要注意选择无 ...

  4. Log4Net记录到数据库

    WinForm下Log4Net的配置 Log4Net 组件下载地址 https://download.csdn.net/download/zgx123zgx123zg/10470986 configS ...

  5. WCF加密传输数据,b并且用户名+密码验证

    在前2个文章的基础上,继续增加对client 端增加username+password的验证 host增加类Validator,需要添加引用 using System.IdentityModel.Se ...

  6. 迁移桌面程序到MS Store(7)——APPX + Service

    本篇我们以一个Sample工程,来说明如何把一个常见结构的desktop application,转制成APPX并在MS Store提供下载. 之前的篇章中,我们已经介绍了一些内容,包括如何通过Vis ...

  7. ConcurrentHashMap源码解析(1)

    此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 注:在看这篇文章之前,如果对HashMap的层不清楚的话,建议先去看看HashMap源码解析. http:/ ...

  8. scanf的拓展用法——匹配特定字符

    scanf的基本用法除了常规的输入操作外还有一些特殊的用法,使用这些用法可以很方便的在输入中读取想要的数据 1.限制输入数据的长度 这个应该算不上拓展用法,大多数读者应该都曾经使用过,这里简单提一下 ...

  9. Swift 里 Array (四) Accessing Elements

    根据下标取值 关键代码如下: func _getElement( _ index: Int, wasNativeTypeChecked: Bool, matchingSubscriptCheck: _ ...

  10. [原创]K8_C段旁注工具6.0 新增SMB漏洞扫描

    工具: K8_C段旁注工具6.0_0510[K.8]编译: 自己查壳组织: K8搞基大队[K8team]作者: K8拉登哥哥博客: http://qqhack8.blog.163.com发布: 201 ...