ORG.APACHE.COMMONS.LANG3.STRINGUTILS中的STRINGUTILS常用方法

 2023-02-26    283  

https://my.oschina.net/funmo/blog/615202?p=1

ORG.APACHE.COMMONS.LANG3.STRINGUTILS中的STRINGUTILS常用方法
public static void TestStr(){
//null 和 ""操作~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//判断是否Null 或者 "" 【不去空格】为空的标准是 str==null 或 str.length()==0
System.out.println(StringUtils.isEmpty(" ")); //false
System.out.println(StringUtils.isEmpty(null));
System.out.println(StringUtils.isNotEmpty(null));
//判断是否null 或者 "" 【去空格】~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
System.out.println(StringUtils.isBlank(" ")); //true 只要是空白都为true System.out.println(StringUtils.isNotBlank(null)); //去空格.Null返回null~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ System.out.println(StringUtils.trim(null)); //去空格,将Null和"" 转换为Null System.out.println(StringUtils.trimToNull("")); //去空格,将NULL 和 "" 转换为"" System.out.println(StringUtils.trimToEmpty(null)); //可能是对特殊空格符号去除?? System.out.println(StringUtils.strip("大家好 啊 \t")); //同上,将""和null转换为Null System.out.println(StringUtils.stripToNull(" \t")); //同上,将""和null转换为"" System.out.println(StringUtils.stripToEmpty(null)); //将""或者Null 转换为 "" System.out.println(StringUtils.defaultString(null)); //仅当字符串为Null时 转换为指定的字符串(二参数) System.out.println(StringUtils.defaultString("", "df")); //当字符串为null或者""时,转换为指定的字符串(二参数) System.out.println(StringUtils.defaultIfEmpty(null, "sos")); //去空格.去字符~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //如果第二个参数为null去空格(否则去掉字符串2边一样的字符,到不一样为止) System.out.println(StringUtils.strip("fsfsdf", "f")); //如果第二个参数为null只去前面空格(否则去掉字符串前面一样的字符,到不一样为止) System.out.println(StringUtils.stripStart("ddsuuu ", "d")); //如果第二个参数为null只去后面空格,(否则去掉字符串后面一样的字符,到不一样为止) System.out.println(StringUtils.stripEnd("dabads", "das")); //对数组没个字符串进行去空格。 //ArrayToList(StringUtils.stripAll(new String[]{" 中华 ", "民 国 ", "共和 "})); //如果第二个参数为null.对数组每个字符串进行去空格。(否则去掉数组每个元素开始和结尾一样的字符) //ArrayToList(StringUtils.stripAll(new String[]{" 中华 ", "民 国", "国***"}, "国")); //查找,判断~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //判断2个字符串是否相等相等,Null也相等 System.out.println(StringUtils.equals(null, null)); //不区分大小写比较 System.out.println(StringUtils.equalsIgnoreCase("abc", "ABc")); //查找,不知道怎么弄这么多查找,很多不知道区别在哪?费劲~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //普通查找字符,如果一参数为null或者""返回-1 System.out.println(StringUtils.indexOf(null, "a")); //从指定位置(三参数)开始查找,本例从第2个字符开始查找k字符 System.out.println(StringUtils.indexOf("akfekcd中华", "k", 2)); //未发现不同之处 System.out.println(StringUtils.ordinalIndexOf("akfekcd中华", "k", 2)); //查找,不区分大小写 System.out.println(StringUtils.indexOfIgnoreCase("adfs", "D")); //从指定位置(三参数)开始查找,不区分大小写 System.out.println(StringUtils.indexOfIgnoreCase("adfs", "a", 3)); //从后往前查找 System.out.println(StringUtils.lastIndexOf("adfas", "a")); //未理解,此结果为2 System.out.println(StringUtils.lastIndexOf("d饿abasdafs我", "a", 3)); //未解,此结果为-1 System.out.println(StringUtils.lastOrdinalIndexOf("yksdfdht", "f", 2)); //从后往前查,不区分大小写 System.out.println(StringUtils.lastIndexOfIgnoreCase("sdffet", "E")); //未解,此结果为1 System.out.println(StringUtils.lastIndexOfIgnoreCase("efefrfs看", "F" , 2)); //检查是否查到,返回boolean,null返回假 System.out.println(StringUtils.contains("sdf", "dg")); //检查是否查到,返回boolean,null返回假,不区分大小写 System.out.println(StringUtils.containsIgnoreCase("sdf", "D")); //检查是否有含有空格,返回boolean System.out.println(StringUtils.containsWhitespace(" d")); //查询字符串跟数组任一元素相同的第一次相同的位置 System.out.println(StringUtils.indexOfAny("absfekf", new String[]{"f", "b"})); //查询字符串中指定字符串(参数二)出现的次数 System.out.println(StringUtils.indexOfAny("afefes", "e")); //查找字符串中是否有字符数组中相同的字符,返回boolean System.out.println(StringUtils.containsAny("asfsd", new char[]{'k', 'e', 's'})); //未理解与lastIndexOf不同之处。是否查到,返回boolean System.out.println(StringUtils.containsAny("啡f咖啡", "咖")); //未解 System.out.println(StringUtils.indexOfAnyBut("seefaff", "af")); //判断字符串中所有字符,都是出自参数二中。 System.out.println(StringUtils.containsOnly("中华华", "华")); //判断字符串中所有字符,都是出自参数二的数组中。 System.out.println(StringUtils.containsOnly("中华中", new char[]{'中', '华'})); //判断字符串中所有字符,都不在参数二中。 System.out.println(StringUtils.containsNone("中华华", "国")); //判断字符串中所有字符,都不在参数二的数组中。 System.out.println(StringUtils.containsNone("中华中", new char[]{'中', '达人'})); //从后往前查找字符串中与字符数组中相同的元素第一次出现的位置。本例为4 System.out.println(StringUtils.lastIndexOfAny("中国人民***", new String[]{"国人", "共和"})); //未发现与indexOfAny不同之处 查询字符串中指定字符串(参数二)出现的次数 System.out.println(StringUtils.countMatches("中国人民共和中国", "中国")); //检查是否CharSequence的只包含Unicode的字母。空将返回false。一个空的CharSequence(长()= 0)将返回true System.out.println(StringUtils.isAlpha("这是干什么的2")); //检查是否只包含Unicode的CharSequence的字母和空格('')。空将返回一个空的CharSequence假(长()= 0)将返回true。 System.out.println(StringUtils.isAlphaSpace("NBA直播 ")); //检查是否只包含Unicode的CharSequence的字母或数字。空将返回false。一个空的CharSequence(长()= 0)将返回true。 System.out.println(StringUtils.isAlphanumeric("NBA直播")); //如果检查的Unicode CharSequence的只包含字母,数字或空格('')。空将返回false。一个空的CharSequence(长()= 0)将返回true。 System.out.println(StringUtils.isAlphanumericSpace("NBA直播")); //检查是否只包含ASCII可CharSequence的字符。空将返回false。一个空的CharSequence(长()= 0)将返回true。 System.out.println(StringUtils.isAsciiPrintable("NBA直播")); //检查是否只包含数值。 System.out.println(StringUtils.isNumeric("NBA直播")); //检查是否只包含数值或者空格 System.out.println(StringUtils.isNumericSpace("33 545")); //检查是否只是空格或""。 System.out.println(StringUtils.isWhitespace(" ")); //检查是否全是英文小写。 System.out.println(StringUtils.isAllLowerCase("kjk33")); //检查是否全是英文大写。 System.out.println(StringUtils.isAllUpperCase("KJKJ")); //交集操作~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //去掉参数2字符串中在参数一中开头部分共有的部分,结果为:人民共和加油 System.out.println(StringUtils.difference("中国加油", "中国人民共和加油")); //统计2个字符串开始部分共有的字符个数 System.out.println(StringUtils.indexOfDifference("ww.taobao", "www.taobao.com")); //统计数组中各个元素的字符串开始都一样的字符个数 System.out.println(StringUtils.indexOfDifference(new String[] {"中国加油", "中国共和", "中国人民"})); //取数组每个元素共同的部分字符串 System.out.println(StringUtils.getCommonPrefix(new String[] {"中国加油", "中国共和", "中国人民"})); //统计参数一中每个字符与参数二中每个字符不同部分的字符个数 System.out.println(StringUtils.getLevenshteinDistance("中国共和发国人民", "***")); //判断开始部分是否与二参数相同 System.out.println(StringUtils.startsWith("中国***人民", "中国")); //判断开始部分是否与二参数相同。不区分大小写 System.out.println(StringUtils.startsWithIgnoreCase("中国***人民", "中国")); //判断字符串开始部分是否与数组中的某一元素相同 System.out.println(StringUtils.startsWithAny("abef", new String[]{"ge", "af", "ab"})); //判断结尾是否相同 System.out.println(StringUtils.endsWith("abcdef", "def")); //判断结尾是否相同,不区分大小写 System.out.println(StringUtils.endsWithIgnoreCase("abcdef", "Def")); //字符串截取~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //截取指定位置的字符,null返回null.""返回"" System.out.println(StringUtils.substring("***", 2)); //截取指定区间的字符 System.out.println(StringUtils.substring("中国人民***", 2, 4)); //从左截取指定长度的字符串 System.out.println(StringUtils.left("说点什么好呢", 3)); //从右截取指定长度的字符串 System.out.println(StringUtils.right("说点什么好呢", 3)); //从第几个开始截取,三参数表示截取的长度 System.out.println(StringUtils.mid("说点什么好呢", 3, 2)); //截取到等于第二个参数的字符串为止 System.out.println(StringUtils.substringBefore("说点什么好呢", "好")); //从左往右查到相等的字符开始,保留后边的,不包含等于的字符。本例:什么好呢 System.out.println(StringUtils.substringAfter("说点什么好呢", "点")); //这个也是截取到相等的字符,但是是从右往左.本例结果:说点什么好 System.out.println(StringUtils.substringBeforeLast("说点什么好点呢", "点")); //这个截取同上是从右往左。但是保留右边的字符 System.out.println(StringUtils.substringAfterLast("说点什么好点呢?", "点")); //截取查找到第一次的位置,和第二次的位置中间的字符。如果没找到第二个返回null。本例结果:2010世界杯在 System.out.println(StringUtils.substringBetween("南非2010世界杯在南非,在南非", "南非")); //返回参数二和参数三中间的字符串,返回数组形式 //ArrayToList(StringUtils.substringsBetween("[a][b][c]", "[", "]")); //分割~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //用空格分割成数组,null为null //ArrayToList(StringUtils.split("中华 人民 共和")); //以指定字符分割成数组 //ArrayToList(StringUtils.split("中华 ,人民,共和", ",")); //以指定字符分割成数组,第三个参数表示分隔成数组的长度,如果为0全体分割 //ArrayToList(StringUtils.split("中华 :人民:共和", ":", 2)); //未发现不同的地方,指定字符分割成数组 //ArrayToList(StringUtils.splitByWholeSeparator("ab-!-cd-!-ef", "-!-")); //未发现不同的地方,以指定字符分割成数组,第三个参数表示分隔成数组的长度 //ArrayToList(StringUtils.splitByWholeSeparator("ab-!-cd-!-ef", "-!-", 2)); //分割,但" "不会被忽略算一个元素,二参数为null默认为空格分隔 //ArrayToList(StringUtils.splitByWholeSeparatorPreserveAllTokens(" ab de fg ", null)); //同上,分割," "不会被忽略算一个元素。第三个参数代表分割的数组长度。 //ArrayToList(StringUtils.splitByWholeSeparatorPreserveAllTokens("ab de fg", null, 3)); //未发现不同地方,分割 //ArrayToList(StringUtils.splitPreserveAllTokens(" ab de fg ")); //未发现不同地方,指定字符分割成数组 //ArrayToList(StringUtils.splitPreserveAllTokens(" ab de fg ", null)); //未发现不同地方,以指定字符分割成数组,第三个参数表示分隔成数组的长度 //ArrayToList(StringUtils.splitPreserveAllTokens(" ab de fg ", null, 2)); //以不同类型进行分隔 //ArrayToList(StringUtils.splitByCharacterType("AEkjKr i39:。中文")); //未解 //ArrayToList(StringUtils.splitByCharacterTypeCamelCase("ASFSRules234")); //拼接~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //将数组转换为字符串形式 System.out.println(StringUtils.concat(getArrayData())); //拼接时用参数一得字符相连接.注意null也用连接符连接了 System.out.println(StringUtils.concatWith(",", getArrayData())); //也是拼接。未发现区别 System.out.println(StringUtils.join(getArrayData())); //用连接符拼接,为发现区别 System.out.println(StringUtils.join(getArrayData(), ":")); //拼接指定数组下标的开始(三参数)和结束(四参数,不包含)的中间这些元素,用连接符连接 System.out.println(StringUtils.join(getArrayData(), ":", 1, 3)); //用于集合连接字符串.用于集合 System.out.println(StringUtils.join(getListData(), ":")); //移除,删除~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //删除所有空格符 System.out.println(StringUtils.deleteWhitespace(" s 中 你 4j")); //移除开始部分的相同的字符 System.out.println(StringUtils.removeStart("www.baidu.com", "www.")); //移除开始部分的相同的字符,不区分大小写 System.out.println(StringUtils.removeStartIgnoreCase("www.baidu.com", "WWW")); //移除后面相同的部分 System.out.println(StringUtils.removeEnd("www.baidu.com", ".com")); //移除后面相同的部分,不区分大小写 System.out.println(StringUtils.removeEndIgnoreCase("www.baidu.com", ".COM")); //移除所有相同的部分 System.out.println(StringUtils.remove("www.baidu.com/baidu", "bai")); //移除结尾字符为"\n", "\r", 或者 "\r\n". System.out.println(StringUtils.chomp("abcrabc\r")); //也是移除,未解。去结尾相同字符 System.out.println(StringUtils.chomp("baidu.com", "com")); //去掉末尾最后一个字符.如果是"\n", "\r", 或者 "\r\n"也去除 System.out.println(StringUtils.chop("wwe.baidu")); //替换~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //替换指定的字符,只替换第一次出现的 System.out.println(StringUtils.replaceOnce("www.baidu.com/baidu", "baidu", "hao123")); //替换所有出现过的字符 System.out.println(StringUtils.replace("www.baidu.com/baidu", "baidu", "hao123")); //也是替换,最后一个参数表示替换几个 System.out.println(StringUtils.replace("www.baidu.com/baidu", "baidu", "hao123", 1)); //这个有意识,二三参数对应的数组,查找二参数数组一样的值,替换三参数对应数组的值。本例:baidu替换为taobao。com替换为net System.out.println(StringUtils.replaceEach("www.baidu.com/baidu", new String[]{"baidu", "com"}, new String[]{"taobao", "net"})); //同上,未发现不同 System.out.println(StringUtils.replaceEachRepeatedly("www.baidu.com/baidu", new String[]{"baidu", "com"}, new String[]{"taobao", "net"})); //这个更好,不是数组对应,是字符串参数二和参数三对应替换.(二三参数不对应的话,自己看后果) System.out.println(StringUtils.replaceChars("www.baidu.com", "bdm", "qo")); //替换指定开始(参数三)和结束(参数四)中间的所有字符 System.out.println(StringUtils.overlay("www.baidu.com", "hao123", 4, 9)); //添加,增加~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //复制参数一的字符串,参数二为复制的次数 System.out.println(StringUtils.repeat("ba", 3)); //复制参数一的字符串,参数三为复制的次数。参数二为复制字符串中间的连接字符串 System.out.println(StringUtils.repeat("ab", "ou", 3)); //如何字符串长度小于参数二的值,末尾加空格补全。(小于字符串长度不处理返回) System.out.println(StringUtils.rightPad("海川", 4)); //字符串长度小于二参数,末尾用参数三补上,多于的截取(截取补上的字符串) System.out.println(StringUtils.rightPad("海川", 4, "河流啊")); //同上在前面补全空格 System.out.println(StringUtils.leftPad("海川", 4)); //字符串长度小于二参数,前面用参数三补上,多于的截取(截取补上的字符串) System.out.println(StringUtils.leftPad("海川", 4, "大家好")); //字符串长度小于二参数。在两侧用空格平均补全(测试后面补空格优先) System.out.println(StringUtils.center("海川", 3)); //字符串长度小于二参数。在两侧用三参数的字符串平均补全(测试后面补空格优先) System.out.println(StringUtils.center("海川", 5, "流")); //只显示指定数量(二参数)的字符,后面以三个点补充(参数一截取+三个点=二参数) System.out.println(StringUtils.abbreviate("***", 5)); //2头加点这个有点乱。本例结果: ...ijklmno System.out.println(StringUtils.abbreviate("abcdefghijklmno", 12, 10)); //保留指定长度,最后一个字符前加点.本例结果: ab.f System.out.println(StringUtils.abbreviateMiddle("abcdef", ".", 4)); //转换,刷选~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //转换第一个字符为大写.如何第一个字符是大写原始返回 System.out.println(StringUtils.capitalize("Ddf")); //转换第一个字符为大写.如何第一个字符是大写原始返回 System.out.println(StringUtils.uncapitalize("DTf")); //反向转换,大写变小写,小写变大写 System.out.println(StringUtils.swapCase("I am Jiang, Hello")); //将字符串倒序排列 System.out.println(StringUtils.reverse("中国人民")); //根据特定字符(二参数)分隔进行反转 System.out.println(StringUtils.reverseDelimited("中:国:人民", ':')); } //将数组转换为List private static void ArrayToList(String[] str){ System.out.println(Arrays.asList(str) + " 长度:" + str.length); } //获得集合数据 private static List getListData(){ List list = new ArrayList(); list.add("你好"); list.add(null); list.add("他好"); list.add("大家好"); return list; } //获得数组数据 private static String[] getArrayData(){ return (String[]) getListData().toArray(new String[0]); } public static void main(String[] args) { TestStr(); }
ORG.APACHE.COMMONS.LANG3.STRINGUTILS中的STRINGUTILS常用方法

 

 

 

 

https://blog.csdn.net/wuge507639721/article/details/81532438

ORG.APACHE.COMMONS.LANG3.STRINGUTILS中的STRINGUTILS常用方法
public static boolean isEmpty(CharSequence cs)
判断【字符】是否为空,为空的标准是 str==null 或 str.length()==0 
//判断是否Null 或者 "" 【不去空格】 StringUtils.isEmpty(null) = true StringUtils.isEmpty("") = true StringUtils.isEmpty(" ") = false StringUtils.isEmpty("bob") = false StringUtils.isEmpty(" bob ") = false

public static boolean isBlank(CharSequence cs)
判断【字符对象】是不是空字符串,注意与isEmpty的区别 
判断的是空,长度为0,空白字符(包括空格,制表符\t,换行符\n,换页符\f,回车\r)组成的字符串。
//判断是否null 或者 "" 【去空格】 StringUtils.isBlank(null) = true StringUtils.isBlank("") = true  //  StringUtils.isBlank(" ") = true // true,只要为空白字符都为true StringUtils.isBlank("bob") = false StringUtils.isBlank(" bob ") = false
//// 对于制表符、换行符、换页符和回车符StringUtils.isBlank()均识为空白符
System.out.println(StringUtils.isBlank("\t \n \f \r"));


public static boolean isNotEmpty(CharSequence cs) 最常用函数之一,跟上面方法相对 StringUtils.isNotEmpty(null) = false StringUtils.isNotEmpty("") = false StringUtils.isNotEmpty(" ") = true StringUtils.isNotEmpty("bob") = true StringUtils.isNotEmpty(" bob ") = true public static boolean isAnyEmpty(CharSequence... css) 任意一个参数为空的话,返回true,如果这些参数都不为空的话返回false。 在写一些判断条件的时候,这个方法还是很实用的。 StringUtils.isAnyEmpty(null) = true StringUtils.isAnyEmpty(null, "foo") = true StringUtils.isAnyEmpty("", "bar") = true StringUtils.isAnyEmpty("bob", "") = true StringUtils.isAnyEmpty(" bob ", null) = true StringUtils.isAnyEmpty(" ", "bar") = false StringUtils.isAnyEmpty("foo", "bar") = false public static boolean isNoneEmpty(CharSequence... css) 任意一个参数是空,返回false 所有参数都不为空,返回true 注意这些方法的用法 StringUtils.isNoneEmpty(null) = false StringUtils.isNoneEmpty(null, "foo") = false StringUtils.isNoneEmpty("", "bar") = false StringUtils.isNoneEmpty("bob", "") = false StringUtils.isNoneEmpty(" bob ", null) = false StringUtils.isNoneEmpty(" ", "bar") = true StringUtils.isNoneEmpty("foo", "bar") = true


public static boolean isNotBlank(CharSequence cs) StringUtils.isNotBlank(null) = false StringUtils.isNotBlank("") = false StringUtils.isNotBlank(" ") = false StringUtils.isNotBlank("bob") = true StringUtils.isNotBlank(" bob ") = true 原理同上 public static boolean isAnyBlank(CharSequence... css) StringUtils.isAnyBlank(null) = true StringUtils.isAnyBlank(null, "foo") = true StringUtils.isAnyBlank(null, null) = true StringUtils.isAnyBlank("", "bar") = true StringUtils.isAnyBlank("bob", "") = true StringUtils.isAnyBlank(" bob ", null) = true StringUtils.isAnyBlank(" ", "bar") = true StringUtils.isAnyBlank("foo", "bar") = false public static boolean isNoneBlank(CharSequence... css) StringUtils.isNoneBlank(null) = false StringUtils.isNoneBlank(null, "foo") = false StringUtils.isNoneBlank(null, null) = false StringUtils.isNoneBlank("", "bar") = false StringUtils.isNoneBlank("bob", "") = false StringUtils.isNoneBlank(" bob ", null) = false StringUtils.isNoneBlank(" ", "bar") = false StringUtils.isNoneBlank("foo", "bar") = true public static String trim(String str) 移除字符串两端的空字符串,制表符char <= 32如:\n \t 如果为空的话,返回空 如果为""  StringUtils.trim(null) = null StringUtils.trim("") = "" StringUtils.trim(" ") = "" StringUtils.trim("abc") = "abc" StringUtils.trim(" abc ") = "abc" 变体有 public static String trimToNull(String str) public static String trimToEmpty(String str) 不常用,跟trim()方法类似 public static String strip(String str) public static String strip(String str, String stripChars) str:被处理的字符串,可为空 stripChars: 删除的字符串, StringUtils.strip(null, *) = null StringUtils.strip("", *) = "" StringUtils.strip("abc", null) = "abc" StringUtils.strip(" abc", null) = "abc" StringUtils.strip("abc ", null) = "abc" StringUtils.strip(" abc ", null) = "abc" StringUtils.strip(" abcyx", "xyz") = " abc" public static boolean equals(CharSequence cs1, CharSequence cs2) 字符串比对方法,是比较实用的方法之一,两个比较的字符串都能为空,不会报空指针异常。 StringUtils.equals(null, null) = true StringUtils.equals(null, "abc") = false StringUtils.equals("abc", null) = false StringUtils.equals("abc", "abc") = true StringUtils.equals("abc", "ABC") = false public static boolean equalsIgnoreCase(CharSequence str1, CharSequence str2) 上面方法的变体字符串比较(忽略大小写),在验证码……等字符串比较,真是很实用。  StringUtils.equalsIgnoreCase(null, null) = true StringUtils.equalsIgnoreCase(null, "abc") = false StringUtils.equalsIgnoreCase("abc", null) = false StringUtils.equalsIgnoreCase("abc", "abc") = true StringUtils.equalsIgnoreCase("abc", "ABC") = true public static int indexOf(CharSequence seq, int searchChar) indexOf这个方法不必多说,这个方法主要处理掉了空字符串的问题,不会报空指针,有一定用处 StringUtils.indexOf(null, *) = -1 StringUtils.indexOf("", *) = -1 StringUtils.indexOf("aabaabaa", 'a') = 0 StringUtils.indexOf("aabaabaa", 'b') = 2 public static int ordinalIndexOf(CharSequence str, CharSequence searchStr, int ordinal) 字符串在另外一个字符串里,出现第Ordinal次的位置  StringUtils.ordinalIndexOf(null, *, *) = -1 StringUtils.ordinalIndexOf(*, null, *) = -1 StringUtils.ordinalIndexOf("", "", *) = 0 StringUtils.ordinalIndexOf("aabaabaa", "a", 1) = 0 StringUtils.ordinalIndexOf("aabaabaa", "a", 2) = 1 StringUtils.ordinalIndexOf("aabaabaa", "b", 1) = 2 StringUtils.ordinalIndexOf("aabaabaa", "b", 2) = 5 StringUtils.ordinalIndexOf("aabaabaa", "ab", 1) = 1 StringUtils.ordinalIndexOf("aabaabaa", "ab", 2) = 4 StringUtils.ordinalIndexOf("aabaabaa", "", 1) = 0 StringUtils.ordinalIndexOf("aabaabaa", "", 2) = 0 public static int lastIndexOf(CharSequence seq, int searchChar) 字符串最后一次出现的位置 StringUtils.lastIndexOf(null, *) = -1 StringUtils.lastIndexOf("", *) = -1 StringUtils.lastIndexOf("aabaabaa", 'a') = 7 StringUtils.lastIndexOf("aabaabaa", 'b') = 5 public static int lastOrdinalIndexOf(CharSequence str, CharSequence searchStr, int ordinal) 字符串searchStr在str里面出现倒数第ordinal出现的位置 StringUtils.lastOrdinalIndexOf(null, *, *) = -1 StringUtils.lastOrdinalIndexOf(*, null, *) = -1 StringUtils.lastOrdinalIndexOf("", "", *) = 0 StringUtils.lastOrdinalIndexOf("aabaabaa", "a", 1) = 7 StringUtils.lastOrdinalIndexOf("aabaabaa", "a", 2) = 6 StringUtils.lastOrdinalIndexOf("aabaabaa", "b", 1) = 5 StringUtils.lastOrdinalIndexOf("aabaabaa", "b", 2) = 2 StringUtils.lastOrdinalIndexOf("aabaabaa", "ab", 1) = 4 StringUtils.lastOrdinalIndexOf("aabaabaa", "ab", 2) = 1 StringUtils.lastOrdinalIndexOf("aabaabaa", "", 1) = 8 StringUtils.lastOrdinalIndexOf("aabaabaa", "", 2) = 8 public static boolean contains(CharSequence seq, int searchChar) 字符串seq是否包含searchChar StringUtils.contains(null, *) = false StringUtils.contains("", *) = false StringUtils.contains("abc", 'a') = true StringUtils.contains("abc", 'z') = false public static boolean containsAny(CharSequence cs, char... searchChars) 包含后面数组中的任意对象,返回true StringUtils.containsAny(null, *) = false StringUtils.containsAny("", *) = false StringUtils.containsAny(*, null) = false StringUtils.containsAny(*, []) = false StringUtils.containsAny("zzabyycdxx",['z','a']) = true StringUtils.containsAny("zzabyycdxx",['b','y']) = true StringUtils.containsAny("aba", ['z']) = false public static String substring(String str, int start) 字符串截取  StringUtils.substring(null, *) = null StringUtils.substring("", *) = "" StringUtils.substring("abc", 0) = "abc" StringUtils.substring("abc", 2) = "c" StringUtils.substring("abc", 4) = "" StringUtils.substring("abc", -2) = "bc" StringUtils.substring("abc", -4) = "abc" public static String left(String str, int len) public static String right(String str, int len) public static String mid(String str, int pos, int len) 这三个方法类似都是截取字符串 public static String[] split(String str, String separatorChars) 字符串分割  StringUtils.split(null, *) = null StringUtils.split("", *) = [] StringUtils.split("abc def", null) = ["abc", "def"] StringUtils.split("abc def", " ") = ["abc", "def"] StringUtils.split("abc def", " ") = ["abc", "def"] StringUtils.split("ab:cd:ef", ":") = ["ab", "cd", "ef"] public static <T> String join(T... elements) 字符串连接 StringUtils.join(null) = null StringUtils.join([]) = "" StringUtils.join([null]) = "" StringUtils.join(["a", "b", "c"]) = "abc" StringUtils.join([null, "", "a"]) = "a" public static String join(Object[] array, char separator) 特定字符串连接数组,很多情况下还是蛮实用,不用自己取拼字符串  StringUtils.join(null, *) = null StringUtils.join([], *) = "" StringUtils.join([null], *) = "" StringUtils.join(["a", "b", "c"], ';') = "a;b;c" StringUtils.join(["a", "b", "c"], null) = "abc" StringUtils.join([null, "", "a"], ';') = ";;a" public static String deleteWhitespace(String str) 删除空格  StringUtils.deleteWhitespace(null) = null StringUtils.deleteWhitespace("") = "" StringUtils.deleteWhitespace("abc") = "abc" StringUtils.deleteWhitespace(" ab c ") = "abc" public static String removeStart(String str, String remove) 删除以特定字符串开头的字符串,如果没有的话,就不删除。  StringUtils.removeStart(null, *) = null StringUtils.removeStart("", *) = "" StringUtils.removeStart(*, null) = * StringUtils.removeStart("www.domain.com", "www.") = "domain.com" StringUtils.removeStart("domain.com", "www.") = "domain.com" StringUtils.removeStart("www.domain.com", "domain") = "www.domain.com" StringUtils.removeStart("abc", "") = "abc" public static String rightPad(String str,int size,char padChar) 生成订单号,的时候还是很实用的。右边自动补齐。  StringUtils.rightPad(null, *, *) = null StringUtils.rightPad("", 3, 'z') = "zzz" StringUtils.rightPad("bat", 3, 'z') = "bat" StringUtils.rightPad("bat", 5, 'z') = "batzz" StringUtils.rightPad("bat", 1, 'z') = "bat" StringUtils.rightPad("bat", -1, 'z') = "bat" public static String leftPad(String str, int size,char padChar) 左边自动补齐  StringUtils.leftPad(null, *, *) = null StringUtils.leftPad("", 3, 'z') = "zzz" StringUtils.leftPad("bat", 3, 'z') = "bat" StringUtils.leftPad("bat", 5, 'z') = "zzbat" StringUtils.leftPad("bat", 1, 'z') = "bat" StringUtils.leftPad("bat", -1, 'z') = "bat" public static String center(String str,int size) 将字符在某特定长度下,句子  StringUtils.center(null, *) = null StringUtils.center("", 4) = " " StringUtils.center("ab", -1) = "ab" StringUtils.center("ab", 4) = " ab " StringUtils.center("abcd", 2) = "abcd" StringUtils.center("a", 4) = " a " public static String capitalize(String str) 首字母大写 StringUtils.capitalize(null) = null StringUtils.capitalize("") = "" StringUtils.capitalize("cat") = "Cat" StringUtils.capitalize("cAt") = "CAt" public static String swapCase(String str) 反向大小写  StringUtils.swapCase(null) = null StringUtils.swapCase("") = "" StringUtils.swapCase("The dog has a BONE") = "tHE DOG HAS A bone" public static boolean isAlpha(CharSequence cs) 判断字符串是否由字母组成  StringUtils.isAlpha(null) = false StringUtils.isAlpha("") = false StringUtils.isAlpha(" ") = false StringUtils.isAlpha("abc") = true StringUtils.isAlpha("ab2c") = false StringUtils.isAlpha("ab-c") = false public static String defaultString(String str, String defaultStr) 默认字符串,相当于三目运算,前面弱为空,则返回后面一个参数  StringUtils.defaultString(null, "NULL") = "NULL" StringUtils.defaultString("", "NULL") = "" StringUtils.defaultString("bat", "NULL") = "bat" public static String reverse(String str) 字符串翻转 StringUtils.reverse(null) = null StringUtils.reverse("") = "" StringUtils.reverse("bat") = "tab" public static String abbreviate(String str, int maxWidth) 缩略字符串,省略号要占三位。maxWith小于3位会报错。 StringUtils.abbreviate(null, *) = null StringUtils.abbreviate("", 4) = "" StringUtils.abbreviate("abcdefg", 6) = "abc..." StringUtils.abbreviate("abcdefg", 7) = "abcdefg" StringUtils.abbreviate("abcdefg", 8) = "abcdefg" StringUtils.abbreviate("abcdefg", 4) = "a..." StringUtils.abbreviate("abcdefg", 3) = IllegalArgumentException public static String abbreviate(String str, int offset, int maxWidth) 缩略字符串的一些高级用法  StringUtils.abbreviate(null, *, *) = null StringUtils.abbreviate("", 0, 4) = "" StringUtils.abbreviate("abcdefghijklmno", -1, 10) = "abcdefg..." StringUtils.abbreviate("abcdefghijklmno", 0, 10) = "abcdefg..." StringUtils.abbreviate("abcdefghijklmno", 1, 10) = "abcdefg..." StringUtils.abbreviate("abcdefghijklmno", 4, 10) = "abcdefg..." StringUtils.abbreviate("abcdefghijklmno", 5, 10) = "...fghi..." StringUtils.abbreviate("abcdefghijklmno", 6, 10) = "...ghij..." StringUtils.abbreviate("abcdefghijklmno", 8, 10) = "...ijklmno" StringUtils.abbreviate("abcdefghijklmno", 10, 10) = "...ijklmno" StringUtils.abbreviate("abcdefghijklmno", 12, 10) = "...ijklmno" StringUtils.abbreviate("abcdefghij", 0, 3) = IllegalArgumentException StringUtils.abbreviate("abcdefghij", 5, 6) = IllegalArgumentException public static String wrap(String str, char wrapWith) 包装,用后面的字符串对前面的字符串进行包装  StringUtils.wrap(null, *) = null StringUtils.wrap("", *) = "" StringUtils.wrap("ab", '\0') = "ab" StringUtils.wrap("ab", 'x') = "xabx" StringUtils.wrap("ab", '\'') = "'ab'" StringUtils.wrap("\"ab\"", '\"') = "\"\"ab\"\""
ORG.APACHE.COMMONS.LANG3.STRINGUTILS中的STRINGUTILS常用方法
以上所述是小编给大家介绍的ORG.APACHE.COMMONS.LANG3.STRINGUTILS中的STRINGUTILS常用方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:http://www.77isp.com/post/34630.html

=========================================

http://www.77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。