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

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

[复制链接]

尚未签到

发表于 2017-1-12 06:58:04 | 显示全部楼层 |阅读模式
67.public static String deleteWhitespace(String str)
删除字符串中的所有空白符(whitespace),空白符是这样定义的{@link Character#isWhitespace(char)}。
举例(*表示任意):
StringUtils.deleteWhitespace(null)            = null
StringUtils.deleteWhitespace("")              = ""
StringUtils.deleteWhitespace("asd"))          = "asd",
StringUtils.deleteWhitespace("as df"))          =    "asdf"
StringUtils.deleteWhitespace("as\n\r\f\tdf"))    =    "asdf"
StringUtils.deleteWhitespace("as\bdf"))            =    "as\bdf"
StringUtils.deleteWhitespace(" as df "))        =    "asdf"

68.public static String removeStart(String str, String remove)
如果字符串str是以字符串remove开始,则去掉这个开始,然后返回,否则返回原来的串。
举例(*表示任意):
StringUtils.removeStart(null, *)      = null
StringUtils.removeStart("", *)        = ""
StringUtils.removeStart(*, null)      = *
StringUtils.removeStart("asdf",""))   = "asdf"
StringUtils.removeStart("asdf","as"))    = "df"
StringUtils.removeStart("asdf","df"))    = "asdf"
StringUtils.removeStart("asdf","gh"))    = "asdf"

69.public static String removeEnd(String str, String remove)
如果字符串str是以字符串remove结尾,则去掉这个结尾,然后返回,否则返回原来的串。
这里不再举例。

70.public static String remove(String str, String remove)
去掉字符串str中所有包含remove的部分,然后返回。
这里不再举例。

71.public static String remove(String str, char remove)
去掉字符串str中所有包含remove的部分,然后返回。
这里不再举例。

72.public static String replaceOnce(String text, String repl, String with)
在字符串text中用with代替repl,仅一次。
这里不再举例。

73.public static String replace(String text, String repl, String with)
在字符串text中用with代替repl,替换所有。
这里不再举例。

74.public static String replace(String text, String repl, String with, int max)
在字符串text中用with代替repl,max为最大替换次数。
如果max小于0,则替换所有。
这里不再举例。

75. public static String replaceChars(String str, char searchChar, char replaceChar)
在字符串str中用字符replaceChar代替所有字符searchChar,
如果字符串为null或"",则返回它本身。
这里不再举例。

76.public static String replaceChars(String str, String searchChars, String replaceChars)
用replaceChars代替str中的searchChars。
replaceChars的长度应该和searchChars的长度相等,
如果replaceChars的长度大于searchChars的长度,超过长度的字符将被忽略,
如果replaceChars的长度小于searchChars的长度,超过长度的字符将被删除。
举例(*表示任意):
StringUtils.replaceChars(null, *, *)            = null
StringUtils.replaceChars("", *, *)              = ""
StringUtils.replaceChars("asdf", null, *)       = "asdf"
StringUtils.replaceChars("asdf", "", *)         = "asdf"
StringUtils.replaceChars("asdf","s",null))      = "adf"
StringUtils.replaceChars("asdf","s",""))            = "adf"
StringUtils.replaceChars("asdsfsg","s","y"))    = "aydyfyg"
StringUtils.replaceChars("asdf","sd","yy"))        =    "ayyf"
StringUtils.replaceChars("asdf","sd","yyy"))    =    "ayyf"
StringUtils.replaceChars("asssdf","s","yyy"))    =    "ayyydf"
StringUtils.replaceChars("asdf","sd","y"))        = "ayf"
StringUtils.replaceChars("assssddddf","sd","y"))= "ayyyyf"

77.public static String overlay(String str, String overlay, int start, int end)
用字符串overlay覆盖字符串str从start到end之间的串。
如果str为null,则返回null
如果start或end小于0,则设为0
如果start大于end,则两者交换
如果start或end大于str的长度,则认为等于str的长度
举例(*表示任意):
StringUtils.overlay(null, *, *, *)        = null
StringUtils.overlay("","as",0,0))         = "as"
StringUtils.overlay("asdfgh","qq",2,5))        =    "asqqh"
StringUtils.overlay("asdfgh","qq",5,2))        =    "asqqh"
StringUtils.overlay("asdfgh","qq",-1,3))    =    "qqfgh"
StringUtils.overlay("asdfgh","qq",-1,-3))    =    "qqasdfgh"
StringUtils.overlay("asdfgh","qq",7,10))    =    "asdfghqq"
StringUtils.overlay("asdfgh","qq",0,8))        =    "qq"
StringUtils.overlay("asdfgh","qq",2,8))        =    "asqq"
StringUtils.overlay("asdfgh",null,2,5))        =    "ash"
StringUtils.overlay("asdfgh","",2,5))            =    "ash"

78.public static String chop(String str)
去掉字符串str的最后一个字符。
如果字符串以"\r\n"结尾,则去掉它们。
这里不再举例。

79.public static String repeat(String str, int repeat)
重复字符串repeat次,组合成一个新串返回。
如果字符串str为null或"",则返回它本身
如果repeat小于0,则返回""
举例(*表示任意):
StringUtils.repeat(null, *) = null
StringUtils.repeat("", *)   = ""
StringUtils.repeat("a", 3) = "aaa"
StringUtils.repeat("ab", 2) = "abab"
StringUtils.repeat("a", -2) = ""

80.public static String rightPad(String str, int size)
如果str为null,则返回null
如果字符串长度小于size,则在右边补空格使其长度等于size,然后返回
如果字符串长度大于等于size,则返回它本身
这里不再举例。

81.public static String rightPad(String str, int size, char padChar)
和80类似,只是补的字符为padChar。
这里不再举例。

82.public static String rightPad(String str, int size, String padStr)
和80类似,只是补的是字符串padStr。
举例(*表示任意):
StringUtils.rightPad(null, *, *)      = null
StringUtils.rightPad("",0,""))        = ""
StringUtils.rightPad("",3,""))          =    "   "
StringUtils.rightPad("",3,"a"))            =    "aaa"
StringUtils.rightPad("",2,"as"))        =    "as"
StringUtils.rightPad("as",-1,"df"))        =    "as"
StringUtils.rightPad("as",0,"df"))        =    "as"
StringUtils.rightPad("as",3,"df"))        =    "asd"
StringUtils.rightPad("as",8,"df"))        =    "asdfdfdf"
StringUtils.rightPad("as",5,null))        =    "as   "
StringUtils.rightPad("as",5,""))            =    "as   "

83.public static String leftPad(String str, int size)
和80类似,只是补左边。
这里不再举例。

84.public static String leftPad(String str, int size, char padChar)
和81类似。
这里不再举例。

85.public static String leftPad(String str, int size, String padStr)
和82类似。
这里不再举例。

86.public static String center(String str, int size)
产生一个字符串返回,该字符串长度等于size,str位于新串的中心,其他位置补空格。
如果str为null,则返回null
如果size小于str的长度,则返回str本身
举例(*表示任意):
StringUtils.center(null, *)   = null
StringUtils.center("",1))     = " "
StringUtils.center("",2))        =    " "
StringUtils.center("as",-1))    =    "as"
StringUtils.center("as",2))        =    "as"
StringUtils.center("as",3))        =    "as "
StringUtils.center("as",4))        =    " as "
StringUtils.center("as",10))    =    "    as    "

87.public static String center(String str, int size, char padChar)
和86类似,只是其他位置补padChar。
这里不再举例。

88.public static String center(String str, int size, String padStr)
和86类似,只是其他位置补padStr。
这里不再举例。

89.public static String swapCase(String str)
把字符串中的字符大写转换为小写,小写转换为大写。
举例:
StringUtils.swapCase(null)          = null
StringUtils.swapCase("")            = ""
StringUtils.swapCase("Hello Boys")) = "hELLO bOYS"
StringUtils.swapCase("I am 11"))        =    "i AM 11"

90.public static int countMatches(String str, String sub)
计算字符串sub在字符串str中出现的次数。
如果str为null或"",则返回0
举例(*表示任意):
StringUtils.countMatches(null, *)        = 0
StringUtils.countMatches("", *)          = 0
StringUtils.countMatches("asdf","as"))   = 1
StringUtils.countMatches("asdfas","as")) = 2
StringUtils.countMatches("dfgh","as"))   = 0
StringUtils.countMatches("as",""))            = 0
StringUtils.countMatches("as",null))        = 0

剩下的一些方法用的比较少或者比较简单,这里就不再介绍。

本系列的学习笔记已经完成了,如果大家看后有一些意见或者建议或者勘误希望和我联系,多谢!

运维网声明 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-327114-1-1.html 上篇帖子: apache commons-lang-2.3 StringUtils.java 学习笔记(3) 下篇帖子: 使用 Apache OpenJPA 开发 EJB 3.0 应用(下)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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