设为首页 收藏本站
查看: 767|回复: 0

[经验分享] apache commons-lang-2.3 StringUtils.java 学习笔记(2)

[复制链接]

尚未签到

发表于 2017-1-12 06:55:53 | 显示全部楼层 |阅读模式
以下方法只介绍其功能,不再举例:
11.public static String strip(String str, String stripChars)
去掉str两端的在stripChars中的字符。
如果str为null或等于"",则返回它本身;
如果stripChars为null或"",则返回strip(String str)。

12.public static String stripStart(String str, String stripChars)
和11相似,去掉str前端的在stripChars中的字符。

13.public static String stripEnd(String str, String stripChars)
和11相似,去掉str末端的在stripChars中的字符。

14.public static String[] stripAll(String[] strs)
对字符串数组中的每个字符串进行strip(String str),然后返回。
如果strs为null或strs长度为0,则返回strs本身

15.public static String[] stripAll(String[] strs, String stripChars)
对字符串数组中的每个字符串进行strip(String str, String stripChars),然后返回。
如果strs为null或strs长度为0,则返回strs本身

16.public static boolean equals(String str1, String str2)
比较两个字符串是否相等,如果两个均为空则也认为相等。

17.public static boolean equalsIgnoreCase(String str1, String str2)
比较两个字符串是否相等,不区分大小写,如果两个均为空则也认为相等。

18.public static int indexOf(String str, char searchChar)
返回字符searchChar在字符串str中第一次出现的位置。
如果searchChar没有在str中出现则返回-1,
如果str为null或"",则也返回-1

19.public static int indexOf(String str, char searchChar, int startPos)
返回字符searchChar从startPos开始在字符串str中第一次出现的位置。
如果从startPos开始searchChar没有在str中出现则返回-1,
如果str为null或"",则也返回-1

20.public static int indexOf(String str, String searchStr)
返回字符串searchStr在字符串str中第一次出现的位置。
如果str为null或searchStr为null则返回-1,
如果searchStr为"",且str为不为null,则返回0,
如果searchStr不在str中,则返回-1

21.public static int ordinalIndexOf(String str, String searchStr, int ordinal)
返回字符串searchStr在字符串str中第ordinal次出现的位置。
如果str=null或searchStr=null或ordinal<=0则返回-1
举例(*代表任意字符串):
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", "bc", 1) = -1
StringUtils.ordinalIndexOf("aabaabaa", "", 1)   = 0
StringUtils.ordinalIndexOf("aabaabaa", "", 2)   = 0

22. public static int indexOf(String str, String searchStr, int startPos)
返回字符串searchStr从startPos开始在字符串str中第一次出现的位置。
举例(*代表任意字符串):
StringUtils.indexOf(null, *, *)          = -1
StringUtils.indexOf(*, null, *)          = -1
StringUtils.indexOf("", "", 0)           = 0
StringUtils.indexOf("aabaabaa", "a", 0) = 0
StringUtils.indexOf("aabaabaa", "b", 0) = 2
StringUtils.indexOf("aabaabaa", "ab", 0) = 1
StringUtils.indexOf("aabaabaa", "b", 3) = 5
StringUtils.indexOf("aabaabaa", "b", 9) = -1
StringUtils.indexOf("aabaabaa", "b", -1) = 2
StringUtils.indexOf("aabaabaa", "", 2)   = 2
StringUtils.indexOf("abc", "", 9)        = 3

23.public static int lastIndexOf(String str, char searchChar)
基本原理同18。

24.public static int lastIndexOf(String str, char searchChar, int startPos)
基本原理同19。

25.public static int lastIndexOf(String str, String searchStr)
基本原理同20。

26.public static int lastIndexOf(String str, String searchStr, int startPos)
基本原理同22。

27.public static boolean contains(String str, char searchChar)
判断字符串str中是否包含字符searchChar。
如果str为null或"",返回false;
如果searchChar不在str中,返回false。

28.public static boolean contains(String str, String searchStr)
判断字符串str是否包含字符串searchStr。
如果str为null或searchStr为null,返回false;
如果str为"",并且searchStr为"",返回true
举例:
StringUtils.contains("", "")       = true
StringUtils.contains("dfg", "")    = true
StringUtils.contains("dfg", "d")   = true
StringUtils.contains("dfg", "gz") = false

29.public static boolean containsIgnoreCase(String str, String searchStr)
判断字符串str是否包含字符串searchStr,不区分大小写。
和28类似。

30.public static int indexOfAny(String str, char[] searchChars)
找出字符数组searchChars中的字符第一次出现在字符串str中的位置。
如果字符数组中的字符都不在字符串中,则返回-1
如果字符串为null或"",则返回-1
举例(*表示任意):
StringUtils.indexOfAny(null, *)                 = -1
StringUtils.indexOfAny("", *)                   = -1
StringUtils.indexOfAny(*, [])                   = -1
StringUtils.indexOfAny("asdf", ['a','f',' '])   = 0
StringUtils.indexOfAny("bs df", ['a','f',' ']) = 2
StringUtils.indexOfAny("bsdf", ['a','f',' '])   = 3
StringUtils.indexOfAny("bbeegg", ['a','f',' ']) = -1

31.public static int indexOfAny(String str, String searchChars)
找出字符串searchChars中的字符第一次出现在字符串str中的位置。
如果字符串searchChars中的字符都不在字符串str中,则返回-1
如果searchChars或str为null或为"",则返回-1
举例(*表示任意):
StringUtils.indexOfAny(null, *)         = -1
StringUtils.indexOfAny("", *)           = -1
StringUtils.indexOfAny(*, null)         = -1
StringUtils.indexOfAny(*, "")           = -1
StringUtils.indexOfAny("asdf", "af ")   = 0
StringUtils.indexOfAny("bs df", "af ") = 2
StringUtils.indexOfAny("bsdf", "af ")   = 3
StringUtils.indexOfAny("bbeegg", "af ") = -1

32.public static int indexOfAnyBut(String str, char[] searchChars)
找出字符串str中不在字符数组searchChars中的第一个字符的位置。
如果字符串中的所有字符都在字符数组中,则返回-1
如果字符串为null或"",则返回-1
举例(*表示任意):
StringUtils.indexOfAnyBut(null, *)                 = -1
StringUtils.indexOfAnyBut("", *)                   = -1
StringUtils.indexOfAnyBut(*, [])                   = -1
StringUtils.indexOfAnyBut("asdf", ['a','f',' '])   = 1
StringUtils.indexOfAnyBut("bs df", ['a','f',' ']) = 0
StringUtils.indexOfAnyBut(" aaf", ['a','f',' '])   = -1
StringUtils.indexOfAnyBut("bbeegg", ['a','f',' ']) = 0

33.public static int indexOfAnyBut(String str, String searchChars)
找出字符串str中不在字符串searchChars中的第一个字符的位置。
如果字符串str中的所有字符都在字符串searchChars中,则返回-1
如果字符串str或searchChars为null或"",则返回-1
举例(*表示任意):
StringUtils.indexOfAnyBut(null, *)         = -1
StringUtils.indexOfAnyBut("", *)           = -1
StringUtils.indexOfAnyBut(*, null)         = -1
StringUtils.indexOfAnyBut(*, "")           = -1
StringUtils.indexOfAnyBut("asdf", "af ")   = 1
StringUtils.indexOfAnyBut("bs df", "af ") = 0
StringUtils.indexOfAnyBut(" aaf", "af ")   = -1
StringUtils.indexOfAnyBut("bbeegg", "af ") = 0

34.public static boolean containsOnly(String str, char[] valid)
判断是否字符串str仅包含字符数组valid中的字符,即字符串中的字符是否都在字符数组中。
如果str为null,则返回false;如果str为"",则返回true
举例(*表示任意):
StringUtils.containsOnly(null, *))              = false
StringUtils.containsOnly("", *))                = true
StringUtils.containsOnly("afaf", ['a','f',' ']))= true
StringUtils.containsOnly("af a", ['a','f',' ']))= true
StringUtils.containsOnly("a", ['a','f',' ']))   = true
StringUtils.containsOnly("afg", ['a','f',' '])) = false
StringUtils.containsOnly("bbeegg", []))         = false

35.public static boolean containsOnly(String str, String validChars)
判断是否字符串str仅包含字符串validChars中的字符,
即字符串str中的字符是否都在字符串validChars中。
和34类似,举例(*表示任意):
StringUtils.containsOnly(null, *)       = false
StringUtils.containsOnly(*, null)       = false
StringUtils.containsOnly("", "")        = true
StringUtils.containsOnly("", "a")       = true
StringUtils.containsOnly("afaf", "af ") = true
StringUtils.containsOnly("af a", "af ") = true
StringUtils.containsOnly("afg", "af ") = false
StringUtils.containsOnly("afg", "")     = false

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-327112-1-1.html 上篇帖子: apache commons-lang-2.3 StringUtils.java 学习笔记(1) 下篇帖子: apache commons-lang-2.3 StringUtils.java 学习笔记(3)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表