apache StringUtils 工具类
// org.apache.commons.lang3.StringUtils // 1.IsEmpty/IsBlank - checks if a String contains text 检查是否为空
boolean empty = StringUtils.isEmpty(""); // 调用cs == null || cs.length() == 0
// System.out.println(empty);
boolean blank = StringUtils.isBlank(" "); // 调用cs == null || cs.length() == 0 和 遍历每个字符Character.isWhitespace(cs.charAt(i))
// System.out.println(blank); // 扩展:isWhitespace() 方法用于判断指定字符是否为空白字符,空白符包含:空格、tab键、换行符。
// http://www.runoob.com/java/character-iswhitespace.html // 2.Trim/Strip - removes leading and trailing whitespace 移除前后空白
String trim = StringUtils.trim(" abc "); // 调用str.trim()
// System.out.println(trim); //"abc"
StringUtils.trimToEmpty(""); // 调用str == null ? "" : str.trim()
StringUtils.trimToNull(""); // 调用 trim(str),再调用isEmpty(ts) String strip = StringUtils.strip("abcdabc", "abc"); // 按指定字符前后截取 ,调用indexOf()和subString()
// System.out.println(strip); //" abc"
// 参考:StringUtils中strip、stripStart、stripEnd剥离方法源码详解
// https://blog.csdn.net/yaomingyang/article/details/79169547 // 3.Equals/Compare - compares two strings null-safe 判断相等/比较
StringUtils.equals("abc", "abc"); // 判断相等,调用==,equals,regionMatches
StringUtils.equalsIgnoreCase("abc", "ABC"); // 判断相等(忽略大小写)
StringUtils.equalsAny("abc", "abc", "def"); // 判断第一个字符与后面任意字符串相等,遍历调用equals判断第一个字符与后面字符是否相等
StringUtils.equalsAnyIgnoreCase("abc", "ABC", "def"); // 判断第一个字符与后面任意字符串相等(忽略大小写) // 扩展:regionMatches() 方法用于检测两个字符串在一个区域内是否相等。
// http://www.runoob.com/java/java-string-regionmatches.html // 4.startsWith - check if a String starts with a prefix null-safe
StringUtils.startsWith("abcdef", "abc"); // true 以什么开头,调用CharSequenceUtils.regionMatches
StringUtils.startsWithIgnoreCase("ABCDEF", "abc"); // true
StringUtils.startsWithAny("abcxyz", new String[] { null, "xyz", "abc" }); // true
StringUtils.startsWithAny("ABCXYZ", null, "xyz", "abc"); // false
// 还可使用: java.lang.String.startsWith
"abcdef".startsWith("abc"); // 5.endsWith - check if a String ends with a suffix null-safe
StringUtils.endsWith("abcdef", "def"); // true
StringUtils.endsWithIgnoreCase("ABCDEF", "def"); // true
StringUtils.endsWithAny("abcxyz", new String[] { null, "xyz", "abc" }); // true // 6.IndexOf/LastIndexOf/Contains - null-safe index-of checks
StringUtils.indexOf("aabaabaa", "ab"); // 1 调用CharSequenceUtils.indexOf
StringUtils.indexOf("aabaabaa", "ab", 0); // 1 从第3位开始查找,"b"首次出现的位置
StringUtils.ordinalIndexOf("aabaabaa", "ab", 1); //
StringUtils.indexOfIgnoreCase("aabaabaa", "AB"); // 1 调用CharSequenceUtils.regionMatches
StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 0); //
StringUtils.lastIndexOf("aabaabaa", "ab"); // 4 调用CharSequenceUtils.lastIndexOf
StringUtils.lastIndexOf("aabaabaa", 'b', 8); //
StringUtils.lastIndexOf("aabaabaa", 'b', 4); //
StringUtils.lastIndexOf("aabaabaa", 'b', 0); // -1
StringUtils.contains("abc", 'a'); // true 调用CharSequenceUtils.indexOf
StringUtils.containsIgnoreCase("abc", "A"); // true
StringUtils.containsWhitespace("1 2"); // true 是否包含空字符,调用Character.isWhitespace
// 还可使用: java.lang.String.indexOf
"aabaabaa".indexOf("ab"); // 7.IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut - index-of any of a set of Strings
StringUtils.indexOfAny("aabaabaa", "ab", "a");
StringUtils.indexOfAny("aabaabaa", new char[] { 'a', 'b' });
StringUtils.lastIndexOfAny("aabaabaa", "ab", "a");
StringUtils.indexOfAnyBut("sdsfhhl0", "h");//结果是0 找出字符串中不在字符数组searchars中的第一个字符出现的位置(从0位开始)
StringUtils.indexOfAnyBut("sdsfhhl0", "s");//结果是1
StringUtils.indexOfAnyBut("aa", "aa");//结果是-1 // 8.ContainsOnly/ContainsNone/ContainsAny - does String contains only/none/any of these characters
StringUtils.containsOnly("abab", new char[] { 'a', 'b', 'c' }); // true 检查字符串(参数1)中的字符是否全为字符串(参数2)中的字符的子集.
StringUtils.containsOnly("abab", "abc");
StringUtils.containsAny("zzabyycdxx", new char[] { 'z', 'a' });// true
StringUtils.containsNone("abab", new char[] { 'x', 'y', 'z' }); // true
// 还可使用: java.lang.String.contains
"aabaabaa".contains("ab"); // 调用String.indexOf // 9.Substring/Left/Right/Mid - null-safe substring extractions
StringUtils.substring("abc", 2); // "c" 调用str.substring
StringUtils.substring("abc", 0, 2); // "ab"
StringUtils.left("abc", 2); // "ab"
StringUtils.right("abc", 2); // "bc"
// 还可使用: java.lang.String.substring
"abc".substring(0, 2); // "ab" 调用new String("abc".getBytes(), 0, 2); // 10.SubstringBefore/SubstringAfter/SubstringBetween - substring extraction relative to other strings
StringUtils.substringBefore("abc", "c"); // "ab" 截取指定字符串之前的内容
StringUtils.substringAfter("abc", "a"); // "bc"
StringUtils.substringBeforeLast("abcba", "b"); // "abc" 一直找到最后一个指定的字符串
StringUtils.substringAfterLast("abcba", "b"); // "a"
StringUtils.substringBetween("tagabctag", "tag"); // "abc"
StringUtils.substringBetween("yabczyabcz", "y", "z"); // "abc"
StringUtils.substringsBetween("[a][b][c]", "[", "]"); // ["a","b","c"] // 11.Split/Join - splits a String into an array of substrings and vice versa
StringUtils.split("abc def"); // ["abc", "def"]
StringUtils.split("a.b.c", '.'); // ["a", "b", "c"]
StringUtils.split("ab:cd:ef", ":", 2); // ["ab", "cd:ef"] 2设定返回数组的最大长度
StringUtils.splitByWholeSeparator("ab-!-cd-!-ef", "-!-");
StringUtils.join("a", "b", "c"); // "abc"
// 还可使用: java.lang.String.split/join
"ab:cd:ef".split(":");
String.join("a", "b", "c"); // 12.Remove/Delete - removes part of a String
StringUtils.remove("queued", 'u'); // "qeed"
StringUtils.removeAll("A<__>\n<__>B", "<.*>"); // "A\nB"
StringUtils.removeAll("A<__>\n<__>B", "(?s)<.*>"); // "AB"
StringUtils.removeAll("ABCabc123abc", "[a-z]"); // "ABC123"
StringUtils.deleteWhitespace(" ab c "); // "abc" // 13.Replace/Overlay - Searches a String and replaces one String with another
StringUtils.replace("aba", "a", "z"); // "zbz"
StringUtils.replaceIgnoreCase("aba", "A", "z"); // "zbz"
StringUtils.replace("abaa", "a", "z", 1); // "zbaa"
StringUtils.replaceEach("abcde", new String[] { "ab", "d" }, new String[] { "d", "t" }); //"dcte"
StringUtils.overlay("abcdef", "zzzz", 2, 4); // "abzzzzef"
// 还可使用: java.lang.String.replace
"aba".replace("a", "z"); System.out.println(); // 官方文档:https://commons.apache.org/proper/commons-lang/javadocs/api-release/index.html
// jdk文档:http://tool.oschina.net/apidocs/apidoc?api=jdk_7u4
// 参考:https://www.cnblogs.com/linjiqin/p/3425359.html
apache StringUtils 工具类的更多相关文章
- StringUtils工具类常用方法汇总1(判空、转换、移除、替换、反转)
Apache commons lang3包下的StringUtils工具类中封装了一些字符串操作的方法,非常实用,使用起来也非常方便.最近自己也经常在项目中使用到了里面的一些方法,在这里将常用的方 ...
- StringUtils工具类常用方法汇总(判空、转换、移除、替换、反转)
Apache commons lang3包下的StringUtils工具类中封装了一些字符串操作的方法,非常实用,使用起来也非常方便.最近自己也经常在项目中使用到了里面的一些方法,在这里将常用的方法总 ...
- StringUtils工具类常用方法汇总:判空、转换、移除、替换、反转。
Apache commons lang3包下的StringUtils工具类中封装了一些字符串操作的方法,非常实用,使用起来也非常方便.最近自己也经常在项目中使用到了里面的一些方法,在这里将常用的方法总 ...
- 通过CollectionUtils工具类判断集合是否为空,通过StringUtils工具类判断字符串是否为空
通过CollectionUtils工具类判断集合是否为空 先引入CollectionUtils工具类: import org.apache.commons.collections4.Collectio ...
- Spring的StringUtils工具类
本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:<Spring的StringUtils工具类> org.springframework.util.StringU ...
- StringUtils工具类常用方法汇总2(截取、去除空白、包含、查询索引)
在上一篇中总结了StringUtils工具类在判断字符串为空,大小写转换,移除字符或字符序列,替换,反转,切割合并等方面的方法,这次再汇总一下其它常用的方法. 一.截取 StringUtils ...
- StringUtils工具类常用方法
前言:工作中看到项目组里的大牛写代码大量的用到了StringUtils工具类来做字符串的操作,便学习整理了一下,方便查阅. isEmpty(String str) 是否为空,空格字符为false is ...
- 基于StringUtils工具类的常用方法介绍(必看篇)
前言:工作中看到项目组里的大牛写代码大量的用到了StringUtils工具类来做字符串的操作,便学习整理了一下,方便查阅. isEmpty(String str) 是否为空,空格字符为false is ...
- spring util包 StringUtils工具类中的isEmpty() 方法解析
今天在公司看到同事写的代码,无意发现在判断字符串类型时,使用的是StringUtils工具类中的isEmpty()去判断如下所示 @RequestMapping(value = "/pub/ ...
随机推荐
- Servlet--HttpServlet实现doGet和doPost请求的原理
转:https://blog.csdn.net/m0_38039437/article/details/75264012 一.HttpServlet简介 1.HttpServlet是GenericSe ...
- Bootstrap 字体图标(Glyphicons)
http://www.runoob.com/bootstrap/bootstrap-glyphicons.html 什么是字体图标? 字体图标是在 Web 项目中使用的图标字体.虽然,Glyphico ...
- mac下virtualbox中centos6.5虚拟机实现全屏和调整分辨率
在visualbox里安装好centos后,发现不能分辨率与原屏幕不一致,很多解决方法是:安装增强包.可是安装增强包后依然达不到效果. 究其原因,原来因为没有安装显卡驱动导致安装了增强包后无法实现分辨 ...
- 数据库语法group by
因为在做pgsql和mysql数据库时group by 有报错,但是在以前做mysql5.6的时候没有问题,虽然知道时违反了sql的语法问题,但是没有搞清楚什么原因,也找了不少资料,查找原因,在盆友的 ...
- Python——WeRobot(微信公众号开发)
模板消息接口 ''' 使用规则 1.所有服务号都可以在功能->添加功能插件处看到申请模板消息功能的入口,但只有认证后的服务号才可以申请模板消息的使用权限并获得该权限: 2.需要选择公众账号服务所 ...
- vim的几个常用操作
现在很少会有人用vim来写代码,所以vim更常用在server上面编辑配置文件或者少量代码编辑: vim操作命令非常之多,如果仅用作一个配置文件的编辑器,掌握几个常用的操作就够了: 常用的操作其实就是 ...
- How to goproxy
brew install python python "/users/cuthead/desktop/phuslu-goproxy-9087f35/uploader.py" sel ...
- cuda编程-并行规约
利用shared memory计算,并避免bank conflict:通过每个block内部规约,然后再把所有block的计算结果在CPU端累加 代码: #include <cuda_runti ...
- servlet篇 之 servlet的访问
三:servlet的访问 使用web.xml文件中的这个<url-pattern>标签中的映射路径,来访问servlet 6.1 在浏览器的地址栏中,直接输入servlet映射的路径来访问 ...
- 实验吧 WEB 貌似有点难
错误!你的IP不在允许列表之内! 提示:代码审计 这个提示可谓是非常良心了,一看源代码是一个识别ip地址的东西,如果IP为1.1.1.1那么就会得到KEY. 第一个if是判断是否有client-ip ...