很多情況下我們都需要將字符串轉換為數字,或判斷字符串是否是數字等等操作,NumberUtils幫助我們方便的從字符串轉換為數字,在不使用NumberUtils情況下,若然字符串值不是數字,使用Integer.parseInt()時會報出java.lang.NumberFormatException,但在NumberUtils的情況下,只會返回0而不產生錯誤
?
????? NumberUtils? and? RandomUtils
?
- public ? class ?TestMain?{??
- ???? public ? static ? void ?main(String[]?args)? throws ?IllegalAccessException?{??
- ????????String?str?=? "12.7" ;??
- ???????? /* ?
- ?????????*?org.apache.commons.lang.NumberUtils已經被棄用, ?
- ?????????*?注意要引入org.apache.commons.lang.math.NumberUtils ?
- ?????????*/ ??
- ??
- ???????? //?判斷字符串是否為整數 ??
- ????????NumberUtils.isDigits(str);??
- ??
- ???????? //?判斷字符串是否為數字 ??
- ????????NumberUtils.isNumber(str);??
- ??
- ???????? //?獲取參數中最大的值,支持傳入數組 ??
- ????????NumberUtils.max( 10 ,? 20 ,? 30 );??
- ??
- ???????? //?獲取參數中最小的值,支持傳入數組 ??
- ????????NumberUtils.min( 10 ,? 20 ,? 30 );??
- ??
- ???????? //?將字符串轉換為int類型,支持float,long,short等數值類型 ??
- ????????NumberUtils.toInt(str);??
- ??
- ???????? //?通過字符串創建BigDecimal類型?,支持int,float,long等數值 ??
- ????????NumberUtils.createBigDecimal(str);??
- ??
- ??
- ???????? /* ?
- ?????????*?RandomUtils幫助我們產生隨機數,不止是數字類型?,? ?
- ?????????*?連boolean類型都可以通過RandomUtils產生 ?
- ?????????*/ ??
- ????????RandomUtils.nextBoolean();??
- ????????RandomUtils.nextDouble();??
- ????????RandomUtils.nextLong();??
- ???????? //?注意這里傳入的參數不是隨機種子,而是在0~1000之間產生一位隨機數 ??
- ????????RandomUtils.nextInt( 1000 );??
- ??
- ????}??
- }??
- public ? class ?TestMain?{??
- ???? public ? static ? void ?main(String[]?args)? throws ?IllegalAccessException?{??
- ????????String?str?=? "12.7" ;??
- ???????? /* ?
- ?????????*?org.apache.commons.lang.NumberUtils已經被棄用, ?
- ?????????*?注意要引入org.apache.commons.lang.math.NumberUtils ?
- ?????????*/ ??
- ??
- ???????? //?判斷字符串是否為整數 ??
- ????????NumberUtils.isDigits(str);??
- ??
- ???????? //?判斷字符串是否為數字 ??
- ????????NumberUtils.isNumber(str);??
- ??
- ???????? //?獲取參數中最大的值,支持傳入數組 ??
- ????????NumberUtils.max( 10 ,? 20 ,? 30 );??
- ??
- ???????? //?獲取參數中最小的值,支持傳入數組 ??
- ????????NumberUtils.min( 10 ,? 20 ,? 30 );??
- ??
- ???????? //?將字符串轉換為int類型,支持float,long,short等數值類型 ??
- ????????NumberUtils.toInt(str);??
- ??
- ???????? //?通過字符串創建BigDecimal類型?,支持int,float,long等數值 ??
- ????????NumberUtils.createBigDecimal(str);??
- ??
- ??
- ???????? /* ?
- ?????????*?RandomUtils幫助我們產生隨機數,不止是數字類型?,? ?
- ?????????*?連boolean類型都可以通過RandomUtils產生 ?
- ?????????*/ ??
- ????????RandomUtils.nextBoolean();??
- ????????RandomUtils.nextDouble();??
- ????????RandomUtils.nextLong();??
- ???????? //?注意這里傳入的參數不是隨機種子,而是在0~1000之間產生一位隨機數 ??
- ????????RandomUtils.nextInt( 1000 );??
- ??
- ????}??
- }??
????? 在開發當中字符串的使用和操作時最為頻繁的,而null的字符串經常讓我們報出NullPointerException,在使用StringUtils后,將不需要為字符串的null值而煩惱,卻又提供了更多的操作讓我們更方便的操作字符串
?
????? StringUtils
?
- public ? class ?TestMain?{??
- ???? public ? static ? void ?main(String[]?args)? throws ?IllegalAccessException?{??
- ????????String?str?=? "Hello?World" ;??
- ???????? /* ?
- ?????????*?由于StringUtils擁有100+的方法,筆者不逐一列舉用法, ?
- ?????????*?只列舉筆者認為常用的或筆者使用過的 ?
- ?????????*/ ??
- ??
- ???????? //?isEmpty和isBlank的區別在于isEmpty不會忽略空格, ??
- ???????? //?"?"<--對于這樣的字符串isEmpty會認為不是空, ??
- ???????? //?而isBlank會認為是空,isBlank更常用 ??
- ????????StringUtils.isEmpty(str);??
- ????????StringUtils.isNotEmpty(str);??
- ????????StringUtils.isBlank(str);??
- ????????StringUtils.isNotBlank(str);??
- ??
- ??
- ???????? //?strip??????-->?去除兩端的aa ??
- ???????? //?stripStart?-->?去除開始位置的hell ??
- ???????? //?stripEnd???-->?去除結尾位置的orld ??
- ????????StringUtils.strip(str,? "aa" );??
- ????????StringUtils.stripStart(str,? "hell" );??
- ????????StringUtils.stripEnd(str,? "orld" );??
- ??
- ??
- ???????? //?返回字符串在第三次出現A的位置 ??
- ????????StringUtils.ordinalIndexOf(str,? "A" ,? 3 );??
- ??
- ??
- ???????? //?獲取str?開始為hello結尾為orld中間的字符串 ??
- ???????? //?注意此方法返回字符串??????-->substringBetween ??
- ???????? //?注意此方法返回字符串數組(多了個s)?-->?substringsBetween ??
- ????????StringUtils.substringBetween(str,? "hell" ,? "orld" );??
- ????????StringUtils.substringsBetween(str,? "hell" ,? "orld" );??
- ??
- ??
- ???????? //?重復字符串,第二種重載形式為在重復中用hahah拼接 ??
- ????????StringUtils.repeat(str,? 3 );??
- ????????StringUtils.repeat(str,? "hahah" ,? 2 );??
- ??
- ??
- ???????? //?統計參數2在字符串中出現的次數 ??
- ????????StringUtils.countMatches(str,? "l" );??
- ??
- ??
- ???????? //?判斷字符串是否全小寫或大寫 ??
- ????????StringUtils.isAllLowerCase(str);??
- ????????StringUtils.isAllUpperCase(str);??
- ??
- ??
- ???????? //?isAlpha????????-->?全部由字母組成返回true ??
- ???????? //?isNumeric??????-->全部由數字組成返回true ??
- ???????? //?isAlphanumeric?-->全部由字母或數字組成返回true ??
- ???????? //?isAlphaSpace???-->全部由字母或空格組成返回true ??
- ???????? //?isWhitespace???-->全部由空格組成返回true ??
- ????????StringUtils.isAlpha(str);??
- ????????StringUtils.isNumeric(str);??
- ????????StringUtils.isAlphanumeric(str);??
- ????????StringUtils.isAlphaSpace(str);??
- ????????StringUtils.isWhitespace(str);??
- ??
- ??
- ???????? //?縮進字符串,第二參數最低為?4,要包含... ??
- ???????? //?現在Hello?World輸出為H... ??
- ????????StringUtils.abbreviate(str,? 4 );??
- ??
- ??
- ???????? //?首字母大寫或小寫 ??
- ????????StringUtils.capitalize(str);??
- ????????StringUtils.uncapitalize(str);??
- ??
- ??
- ???????? //?將字符串數組轉變為一個字符串,通過","拼接,支持傳入iterator和collection ??
- ????????StringUtils.join( new ?String[]?{? "Hello" ,? "World" ?},? "," );??
- ??
- ??
- ??
- ???????? /* ?
- ?????????*?經常性要把后臺的字符串傳遞到前提作為html代碼進行解釋, ?
- ?????????*?可以使用以下方法進行轉換,以下方法輸出為 ?
- ?????????*?<p>Hello</p> ?
- ?????????*/ ??
- ????????StringEscapeUtils.escapeHtml("Hello??
- ");??
- ????}??
- }??
- public ? class ?TestMain?{??
- ???? public ? static ? void ?main(String[]?args)? throws ?IllegalAccessException?{??
- ????????String?str?=? "Hello?World" ;??
- ???????? /* ?
- ?????????*?由于StringUtils擁有100+的方法,筆者不逐一列舉用法, ?
- ?????????*?只列舉筆者認為常用的或筆者使用過的 ?
- ?????????*/ ??
- ??
- ???????? //?isEmpty和isBlank的區別在于isEmpty不會忽略空格, ??
- ???????? //?"?"<--對于這樣的字符串isEmpty會認為不是空, ??
- ???????? //?而isBlank會認為是空,isBlank更常用 ??
- ????????StringUtils.isEmpty(str);??
- ????????StringUtils.isNotEmpty(str);??
- ????????StringUtils.isBlank(str);??
- ????????StringUtils.isNotBlank(str);??
- ??
- ??
- ???????? //?strip??????-->?去除兩端的aa ??
- ???????? //?stripStart?-->?去除開始位置的hell ??
- ???????? //?stripEnd???-->?去除結尾位置的orld ??
- ????????StringUtils.strip(str,? "aa" );??
- ????????StringUtils.stripStart(str,? "hell" );??
- ????????StringUtils.stripEnd(str,? "orld" );??
- ??
- ??
- ???????? //?返回字符串在第三次出現A的位置 ??
- ????????StringUtils.ordinalIndexOf(str,? "A" ,? 3 );??
- ??
- ??
- ???????? //?獲取str?開始為hello結尾為orld中間的字符串 ??
- ???????? //?注意此方法返回字符串??????-->substringBetween ??
- ???????? //?注意此方法返回字符串數組(多了個s)?-->?substringsBetween ??
- ????????StringUtils.substringBetween(str,? "hell" ,? "orld" );??
- ????????StringUtils.substringsBetween(str,? "hell" ,? "orld" );??
- ??
- ??
- ???????? //?重復字符串,第二種重載形式為在重復中用hahah拼接 ??
- ????????StringUtils.repeat(str,? 3 );??
- ????????StringUtils.repeat(str,? "hahah" ,? 2 );??
- ??
- ??
- ???????? //?統計參數2在字符串中出現的次數 ??
- ????????StringUtils.countMatches(str,? "l" );??
- ??
- ??
- ???????? //?判斷字符串是否全小寫或大寫 ??
- ????????StringUtils.isAllLowerCase(str);??
- ????????StringUtils.isAllUpperCase(str);??
- ??
- ??
- ???????? //?isAlpha????????-->?全部由字母組成返回true ??
- ???????? //?isNumeric??????-->全部由數字組成返回true ??
- ???????? //?isAlphanumeric?-->全部由字母或數字組成返回true ??
- ???????? //?isAlphaSpace???-->全部由字母或空格組成返回true ??
- ???????? //?isWhitespace???-->全部由空格組成返回true ??
- ????????StringUtils.isAlpha(str);??
- ????????StringUtils.isNumeric(str);??
- ????????StringUtils.isAlphanumeric(str);??
- ????????StringUtils.isAlphaSpace(str);??
- ????????StringUtils.isWhitespace(str);??
- ??
- ??
- ???????? //?縮進字符串,第二參數最低為?4,要包含... ??
- ???????? //?現在Hello?World輸出為H... ??
- ????????StringUtils.abbreviate(str,? 4 );??
- ??
- ??
- ???????? //?首字母大寫或小寫 ??
- ????????StringUtils.capitalize(str);??
- ????????StringUtils.uncapitalize(str);??
- ??
- ??
- ???????? //?將字符串數組轉變為一個字符串,通過","拼接,支持傳入iterator和collection ??
- ????????StringUtils.join( new ?String[]?{? "Hello" ,? "World" ?},? "," );??
- ??
- ??
- ??
- ???????? /* ?
- ?????????*?經常性要把后臺的字符串傳遞到前提作為html代碼進行解釋, ?
- ?????????*?可以使用以下方法進行轉換,以下方法輸出為 ?
- ?????????*?<p>Hello</p> ?
- ?????????*/ ??
- ????????StringEscapeUtils.escapeHtml("Hello??
- ");??
- ????}??
- }??
????? DateUtils and DateFormatUtils
?
- public ? class ?TestMain?{??
- ???? public ? static ? void ?main(String[]?args)? throws ?IllegalAccessException?{??
- ????????Date?day1?=? new ?Date();??
- ???????? /* ?
- ?????????*?由于Aache的DateUtils和DateFormatUtils并沒有Joda強大, ?
- ?????????*??所以在這里只作簡單的示例 ?
- ?????????*/ ??
- ??????????
- ???????? //?增加一天 ??
- ????????DateUtils.addDays(day1,? 1 );??
- ???????? //?減少一年 ??
- ????????DateUtils.addYears(day1,?- 1 );??
- ??
- ???????? //?格式化時間,第三參數為國際化,表示按美國時間顯示 ??
- ????????DateFormatUtils.format(day1,? "yyyy-MM-dd" ,?Locale.UK);??
- ??
- ????}??
- }??
- public ? class ?TestMain?{??
- ???? public ? static ? void ?main(String[]?args)? throws ?IllegalAccessException?{??
- ????????Date?day1?=? new ?Date();??
- ???????? /* ?
- ?????????*?由于Aache的DateUtils和DateFormatUtils并沒有Joda強大, ?
- ?????????*??所以在這里只作簡單的示例 ?
- ?????????*/ ??
- ??????????
- ???????? //?增加一天 ??
- ????????DateUtils.addDays(day1,? 1 );??
- ???????? //?減少一年 ??
- ????????DateUtils.addYears(day1,?- 1 );??
- ??
- ???????? //?格式化時間,第三參數為國際化,表示按美國時間顯示 ??
- ????????DateFormatUtils.format(day1,? "yyyy-MM-dd" ,?Locale.UK);??
- ??
- ????}??
- }??
總結:
?
????? commons工具包很多開源組織都有提供,例如google,spring,apache都有各自的工具包,有眾多的選擇,但最終的目的只是為了方便我們程序的開發和維護,簡化我們編寫一些常用的邏輯,提升我們開發的效率,從而達到活在開源,善用開源
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
