日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

開源工具 — Apache Commons Lang(2)

系統 1880 0

很多情況下我們都需要將字符串轉換為數字,或判斷字符串是否是數字等等操作,NumberUtils幫助我們方便的從字符串轉換為數字,在不使用NumberUtils情況下,若然字符串值不是數字,使用Integer.parseInt()時會報出java.lang.NumberFormatException,但在NumberUtils的情況下,只會返回0而不產生錯誤 ?

????? NumberUtils? and? RandomUtils ?

Java代碼 ? ?
  1. public ? class ?TestMain?{??
  2. ???? public ? static ? void ?main(String[]?args)? throws ?IllegalAccessException?{??
  3. ????????String?str?=? "12.7" ;??
  4. ???????? /* ?
  5. ?????????*?org.apache.commons.lang.NumberUtils已經被棄用, ?
  6. ?????????*?注意要引入org.apache.commons.lang.math.NumberUtils ?
  7. ?????????*/ ??
  8. ??
  9. ???????? //?判斷字符串是否為整數 ??
  10. ????????NumberUtils.isDigits(str);??
  11. ??
  12. ???????? //?判斷字符串是否為數字 ??
  13. ????????NumberUtils.isNumber(str);??
  14. ??
  15. ???????? //?獲取參數中最大的值,支持傳入數組 ??
  16. ????????NumberUtils.max( 10 ,? 20 ,? 30 );??
  17. ??
  18. ???????? //?獲取參數中最小的值,支持傳入數組 ??
  19. ????????NumberUtils.min( 10 ,? 20 ,? 30 );??
  20. ??
  21. ???????? //?將字符串轉換為int類型,支持float,long,short等數值類型 ??
  22. ????????NumberUtils.toInt(str);??
  23. ??
  24. ???????? //?通過字符串創建BigDecimal類型?,支持int,float,long等數值 ??
  25. ????????NumberUtils.createBigDecimal(str);??
  26. ??
  27. ??
  28. ???????? /* ?
  29. ?????????*?RandomUtils幫助我們產生隨機數,不止是數字類型?,? ?
  30. ?????????*?連boolean類型都可以通過RandomUtils產生 ?
  31. ?????????*/ ??
  32. ????????RandomUtils.nextBoolean();??
  33. ????????RandomUtils.nextDouble();??
  34. ????????RandomUtils.nextLong();??
  35. ???????? //?注意這里傳入的參數不是隨機種子,而是在0~1000之間產生一位隨機數 ??
  36. ????????RandomUtils.nextInt( 1000 );??
  37. ??
  38. ????}??
  39. }??
Java代碼 ? ? 收藏代碼
  1. public ? class ?TestMain?{??
  2. ???? public ? static ? void ?main(String[]?args)? throws ?IllegalAccessException?{??
  3. ????????String?str?=? "12.7" ;??
  4. ???????? /* ?
  5. ?????????*?org.apache.commons.lang.NumberUtils已經被棄用, ?
  6. ?????????*?注意要引入org.apache.commons.lang.math.NumberUtils ?
  7. ?????????*/ ??
  8. ??
  9. ???????? //?判斷字符串是否為整數 ??
  10. ????????NumberUtils.isDigits(str);??
  11. ??
  12. ???????? //?判斷字符串是否為數字 ??
  13. ????????NumberUtils.isNumber(str);??
  14. ??
  15. ???????? //?獲取參數中最大的值,支持傳入數組 ??
  16. ????????NumberUtils.max( 10 ,? 20 ,? 30 );??
  17. ??
  18. ???????? //?獲取參數中最小的值,支持傳入數組 ??
  19. ????????NumberUtils.min( 10 ,? 20 ,? 30 );??
  20. ??
  21. ???????? //?將字符串轉換為int類型,支持float,long,short等數值類型 ??
  22. ????????NumberUtils.toInt(str);??
  23. ??
  24. ???????? //?通過字符串創建BigDecimal類型?,支持int,float,long等數值 ??
  25. ????????NumberUtils.createBigDecimal(str);??
  26. ??
  27. ??
  28. ???????? /* ?
  29. ?????????*?RandomUtils幫助我們產生隨機數,不止是數字類型?,? ?
  30. ?????????*?連boolean類型都可以通過RandomUtils產生 ?
  31. ?????????*/ ??
  32. ????????RandomUtils.nextBoolean();??
  33. ????????RandomUtils.nextDouble();??
  34. ????????RandomUtils.nextLong();??
  35. ???????? //?注意這里傳入的參數不是隨機種子,而是在0~1000之間產生一位隨機數 ??
  36. ????????RandomUtils.nextInt( 1000 );??
  37. ??
  38. ????}??
  39. }??



????? 在開發當中字符串的使用和操作時最為頻繁的,而null的字符串經常讓我們報出NullPointerException,在使用StringUtils后,將不需要為字符串的null值而煩惱,卻又提供了更多的操作讓我們更方便的操作字符串 ?


????? StringUtils ?

Java代碼 ? ?
  1. public ? class ?TestMain?{??
  2. ???? public ? static ? void ?main(String[]?args)? throws ?IllegalAccessException?{??
  3. ????????String?str?=? "Hello?World" ;??
  4. ???????? /* ?
  5. ?????????*?由于StringUtils擁有100+的方法,筆者不逐一列舉用法, ?
  6. ?????????*?只列舉筆者認為常用的或筆者使用過的 ?
  7. ?????????*/ ??
  8. ??
  9. ???????? //?isEmpty和isBlank的區別在于isEmpty不會忽略空格, ??
  10. ???????? //?"?"<--對于這樣的字符串isEmpty會認為不是空, ??
  11. ???????? //?而isBlank會認為是空,isBlank更常用 ??
  12. ????????StringUtils.isEmpty(str);??
  13. ????????StringUtils.isNotEmpty(str);??
  14. ????????StringUtils.isBlank(str);??
  15. ????????StringUtils.isNotBlank(str);??
  16. ??
  17. ??
  18. ???????? //?strip??????-->?去除兩端的aa ??
  19. ???????? //?stripStart?-->?去除開始位置的hell ??
  20. ???????? //?stripEnd???-->?去除結尾位置的orld ??
  21. ????????StringUtils.strip(str,? "aa" );??
  22. ????????StringUtils.stripStart(str,? "hell" );??
  23. ????????StringUtils.stripEnd(str,? "orld" );??
  24. ??
  25. ??
  26. ???????? //?返回字符串在第三次出現A的位置 ??
  27. ????????StringUtils.ordinalIndexOf(str,? "A" ,? 3 );??
  28. ??
  29. ??
  30. ???????? //?獲取str?開始為hello結尾為orld中間的字符串 ??
  31. ???????? //?注意此方法返回字符串??????-->substringBetween ??
  32. ???????? //?注意此方法返回字符串數組(多了個s)?-->?substringsBetween ??
  33. ????????StringUtils.substringBetween(str,? "hell" ,? "orld" );??
  34. ????????StringUtils.substringsBetween(str,? "hell" ,? "orld" );??
  35. ??
  36. ??
  37. ???????? //?重復字符串,第二種重載形式為在重復中用hahah拼接 ??
  38. ????????StringUtils.repeat(str,? 3 );??
  39. ????????StringUtils.repeat(str,? "hahah" ,? 2 );??
  40. ??
  41. ??
  42. ???????? //?統計參數2在字符串中出現的次數 ??
  43. ????????StringUtils.countMatches(str,? "l" );??
  44. ??
  45. ??
  46. ???????? //?判斷字符串是否全小寫或大寫 ??
  47. ????????StringUtils.isAllLowerCase(str);??
  48. ????????StringUtils.isAllUpperCase(str);??
  49. ??
  50. ??
  51. ???????? //?isAlpha????????-->?全部由字母組成返回true ??
  52. ???????? //?isNumeric??????-->全部由數字組成返回true ??
  53. ???????? //?isAlphanumeric?-->全部由字母或數字組成返回true ??
  54. ???????? //?isAlphaSpace???-->全部由字母或空格組成返回true ??
  55. ???????? //?isWhitespace???-->全部由空格組成返回true ??
  56. ????????StringUtils.isAlpha(str);??
  57. ????????StringUtils.isNumeric(str);??
  58. ????????StringUtils.isAlphanumeric(str);??
  59. ????????StringUtils.isAlphaSpace(str);??
  60. ????????StringUtils.isWhitespace(str);??
  61. ??
  62. ??
  63. ???????? //?縮進字符串,第二參數最低為?4,要包含... ??
  64. ???????? //?現在Hello?World輸出為H... ??
  65. ????????StringUtils.abbreviate(str,? 4 );??
  66. ??
  67. ??
  68. ???????? //?首字母大寫或小寫 ??
  69. ????????StringUtils.capitalize(str);??
  70. ????????StringUtils.uncapitalize(str);??
  71. ??
  72. ??
  73. ???????? //?將字符串數組轉變為一個字符串,通過","拼接,支持傳入iterator和collection ??
  74. ????????StringUtils.join( new ?String[]?{? "Hello" ,? "World" ?},? "," );??
  75. ??
  76. ??
  77. ??
  78. ???????? /* ?
  79. ?????????*?經常性要把后臺的字符串傳遞到前提作為html代碼進行解釋, ?
  80. ?????????*?可以使用以下方法進行轉換,以下方法輸出為 ?
  81. ?????????*?<p>Hello</p> ?
  82. ?????????*/ ??
  83. ????????StringEscapeUtils.escapeHtml("Hello??
  84. ");??
  85. ????}??
  86. }??
Java代碼 ? ? 收藏代碼
  1. public ? class ?TestMain?{??
  2. ???? public ? static ? void ?main(String[]?args)? throws ?IllegalAccessException?{??
  3. ????????String?str?=? "Hello?World" ;??
  4. ???????? /* ?
  5. ?????????*?由于StringUtils擁有100+的方法,筆者不逐一列舉用法, ?
  6. ?????????*?只列舉筆者認為常用的或筆者使用過的 ?
  7. ?????????*/ ??
  8. ??
  9. ???????? //?isEmpty和isBlank的區別在于isEmpty不會忽略空格, ??
  10. ???????? //?"?"<--對于這樣的字符串isEmpty會認為不是空, ??
  11. ???????? //?而isBlank會認為是空,isBlank更常用 ??
  12. ????????StringUtils.isEmpty(str);??
  13. ????????StringUtils.isNotEmpty(str);??
  14. ????????StringUtils.isBlank(str);??
  15. ????????StringUtils.isNotBlank(str);??
  16. ??
  17. ??
  18. ???????? //?strip??????-->?去除兩端的aa ??
  19. ???????? //?stripStart?-->?去除開始位置的hell ??
  20. ???????? //?stripEnd???-->?去除結尾位置的orld ??
  21. ????????StringUtils.strip(str,? "aa" );??
  22. ????????StringUtils.stripStart(str,? "hell" );??
  23. ????????StringUtils.stripEnd(str,? "orld" );??
  24. ??
  25. ??
  26. ???????? //?返回字符串在第三次出現A的位置 ??
  27. ????????StringUtils.ordinalIndexOf(str,? "A" ,? 3 );??
  28. ??
  29. ??
  30. ???????? //?獲取str?開始為hello結尾為orld中間的字符串 ??
  31. ???????? //?注意此方法返回字符串??????-->substringBetween ??
  32. ???????? //?注意此方法返回字符串數組(多了個s)?-->?substringsBetween ??
  33. ????????StringUtils.substringBetween(str,? "hell" ,? "orld" );??
  34. ????????StringUtils.substringsBetween(str,? "hell" ,? "orld" );??
  35. ??
  36. ??
  37. ???????? //?重復字符串,第二種重載形式為在重復中用hahah拼接 ??
  38. ????????StringUtils.repeat(str,? 3 );??
  39. ????????StringUtils.repeat(str,? "hahah" ,? 2 );??
  40. ??
  41. ??
  42. ???????? //?統計參數2在字符串中出現的次數 ??
  43. ????????StringUtils.countMatches(str,? "l" );??
  44. ??
  45. ??
  46. ???????? //?判斷字符串是否全小寫或大寫 ??
  47. ????????StringUtils.isAllLowerCase(str);??
  48. ????????StringUtils.isAllUpperCase(str);??
  49. ??
  50. ??
  51. ???????? //?isAlpha????????-->?全部由字母組成返回true ??
  52. ???????? //?isNumeric??????-->全部由數字組成返回true ??
  53. ???????? //?isAlphanumeric?-->全部由字母或數字組成返回true ??
  54. ???????? //?isAlphaSpace???-->全部由字母或空格組成返回true ??
  55. ???????? //?isWhitespace???-->全部由空格組成返回true ??
  56. ????????StringUtils.isAlpha(str);??
  57. ????????StringUtils.isNumeric(str);??
  58. ????????StringUtils.isAlphanumeric(str);??
  59. ????????StringUtils.isAlphaSpace(str);??
  60. ????????StringUtils.isWhitespace(str);??
  61. ??
  62. ??
  63. ???????? //?縮進字符串,第二參數最低為?4,要包含... ??
  64. ???????? //?現在Hello?World輸出為H... ??
  65. ????????StringUtils.abbreviate(str,? 4 );??
  66. ??
  67. ??
  68. ???????? //?首字母大寫或小寫 ??
  69. ????????StringUtils.capitalize(str);??
  70. ????????StringUtils.uncapitalize(str);??
  71. ??
  72. ??
  73. ???????? //?將字符串數組轉變為一個字符串,通過","拼接,支持傳入iterator和collection ??
  74. ????????StringUtils.join( new ?String[]?{? "Hello" ,? "World" ?},? "," );??
  75. ??
  76. ??
  77. ??
  78. ???????? /* ?
  79. ?????????*?經常性要把后臺的字符串傳遞到前提作為html代碼進行解釋, ?
  80. ?????????*?可以使用以下方法進行轉換,以下方法輸出為 ?
  81. ?????????*?<p>Hello</p> ?
  82. ?????????*/ ??
  83. ????????StringEscapeUtils.escapeHtml("Hello??
  84. ");??
  85. ????}??
  86. }??



????? DateUtils and DateFormatUtils ?

Java代碼 ? ?
  1. public ? class ?TestMain?{??
  2. ???? public ? static ? void ?main(String[]?args)? throws ?IllegalAccessException?{??
  3. ????????Date?day1?=? new ?Date();??
  4. ???????? /* ?
  5. ?????????*?由于Aache的DateUtils和DateFormatUtils并沒有Joda強大, ?
  6. ?????????*??所以在這里只作簡單的示例 ?
  7. ?????????*/ ??
  8. ??????????
  9. ???????? //?增加一天 ??
  10. ????????DateUtils.addDays(day1,? 1 );??
  11. ???????? //?減少一年 ??
  12. ????????DateUtils.addYears(day1,?- 1 );??
  13. ??
  14. ???????? //?格式化時間,第三參數為國際化,表示按美國時間顯示 ??
  15. ????????DateFormatUtils.format(day1,? "yyyy-MM-dd" ,?Locale.UK);??
  16. ??
  17. ????}??
  18. }??
Java代碼 ? ? 收藏代碼
  1. public ? class ?TestMain?{??
  2. ???? public ? static ? void ?main(String[]?args)? throws ?IllegalAccessException?{??
  3. ????????Date?day1?=? new ?Date();??
  4. ???????? /* ?
  5. ?????????*?由于Aache的DateUtils和DateFormatUtils并沒有Joda強大, ?
  6. ?????????*??所以在這里只作簡單的示例 ?
  7. ?????????*/ ??
  8. ??????????
  9. ???????? //?增加一天 ??
  10. ????????DateUtils.addDays(day1,? 1 );??
  11. ???????? //?減少一年 ??
  12. ????????DateUtils.addYears(day1,?- 1 );??
  13. ??
  14. ???????? //?格式化時間,第三參數為國際化,表示按美國時間顯示 ??
  15. ????????DateFormatUtils.format(day1,? "yyyy-MM-dd" ,?Locale.UK);??
  16. ??
  17. ????}??
  18. }??



總結: ?
????? commons工具包很多開源組織都有提供,例如google,spring,apache都有各自的工具包,有眾多的選擇,但最終的目的只是為了方便我們程序的開發和維護,簡化我們編寫一些常用的邏輯,提升我們開發的效率,從而達到活在開源,善用開源

開源工具 — Apache Commons Lang(2)


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 吕梁市| 乌拉特中旗| 岳阳县| 蒲江县| 溆浦县| 三门峡市| 松原市| 柏乡县| 柳河县| 顺平县| 怀安县| 淅川县| 三门峡市| 辽宁省| 电白县| 海兴县| 民勤县| 蒙城县| SHOW| 凤翔县| 梁山县| 南部县| 张家口市| 新巴尔虎左旗| 泾阳县| 武隆县| 滨州市| 苗栗市| 湛江市| 台东县| 铜梁县| 清新县| 玉田县| 肇州县| 文化| 收藏| 页游| 陆丰市| 苍梧县| 无为县| 双鸭山市|