A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

  1. StringUtils工具类的使用
  2. 一、数组转成字符串:
  3. 1、 将数组中的字符转换为一个字符串
  4. 将数组中的字符转换为一个字符串

  5. @param strToConv 要转换的字符串 ,默认以逗号分隔
  6. @return 返回一个字符串
  7. String[3] s={"a","b","c"}
  8. StringUtil.convString(s)="a,b,c"
  9. 2、 static public String converString(String strToConv)
  10. @param strToConv 要转换的字符串 ,
  11. @param conv 分隔符,默认以逗号分隔
  12. @return 同样返回一个字符串

  13. String[3] s={"a","b","c"}
  14. StringUtil.convString(s,"@")="a@b@c"
  15. static public String converString(String strToConv, String conv)


  16. 二、空值检测:
  17. 3、

  18. Checks if a String is empty ("") or null.


  19. 判断一个字符串是否为空,空格作非空处理。 StringUtils.isEmpty(null) = true StringUtils.isEmpty("") = true StringUtils.isEmpty(" ") = false StringUtils.isEmpty("bob") = false StringUtils.isEmpty(" bob ") = false

  20. NOTE: This method changed in Lang version 2.0.

  21. It no longer trims the String.
  22. That functionality is available in isBlank().


  23. @param str the String to check, may be null
  24. @return true if the String is empty or null
  25. public static boolean isEmpty(String str)


  26. 三、非空处理:
  27. 4、
  28. Checks if a String is not empty ("") and not null.


  29. 判断一个字符串是否非空,空格作非空处理. StringUtils.isNotEmpty(null) = false StringUtils.isNotEmpty("") = false StringUtils.isNotEmpty(" ") = true StringUtils.isNotEmpty("bob") = true StringUtils.isNotEmpty(" bob ") = true

  30. @param str the String to check, may be null
  31. @return true if the String is not empty and not null
  32. public static boolean isNotEmpty(String str)

  33. 5、

  34. Checks if a String is not empty (""), not null and not whitespace only.


  35. 判断一个字符串是否非空,空格作空处理. StringUtils.isNotBlank(null) = false StringUtils.isNotBlank("") = false StringUtils.isNotBlank(" ") = false StringUtils.isNotBlank("bob") = true StringUtils.isNotBlank(" bob ") = true

  36. @param str the String to check, may be null
  37. @return true if the String is
  38. not empty and not null and not whitespace
  39. @since 2.0
  40. public static boolean isNotBlank(String str)


  41. 四、 空格处理
  42. 6、
  43. Removes control characters (char <= 32) from both

  44. ends of this String, handling null by returning
  45. null.


  46. The String is trimmed using {@link String#trim()}.

  47. Trim removes start and end characters <= 32.
  48. To strip whitespace use {@link //strip(String)}.


  49. To trim your choice of characters, use the

  50. {@link //strip(String, String)} methods.


  51. 格式化一个字符串中的空格,有非空判断处理; StringUtils.trim(null) = null StringUtils.trim("") = "" StringUtils.trim(" ") = "" StringUtils.trim("abc") = "abc" StringUtils.trim(" abc ") = "abc"

  52. @param str the String to be trimmed, may be null
  53. @return the trimmed string, null if null String input
  54. public static String trim(String str)

  55. 7、


  56. Removes control characters (char <= 32) from both

  57. ends of this String returning null if the String is
  58. empty ("") after the trim or if it is null.

  59. The String is trimmed using {@link String#trim()}.

  60. Trim removes start and end characters <= 32.
  61. To strip whitespace use {@link /stripToNull(String)}.


  62. 格式化一个字符串中的空格,有非空判断处理,如果为空返回null; StringUtils.trimToNull(null) = null StringUtils.trimToNull("") = null StringUtils.trimToNull(" ") = null StringUtils.trimToNull("abc") = "abc" StringUtils.trimToNull(" abc ") = "abc"

  63. @param str the String to be trimmed, may be null
  64. @return the trimmed String,
  65. null if only chars <= 32, empty or null String input
  66. @since 2.0
  67. public static String trimToNull(String str)

  68. 8、


  69. Removes control characters (char <= 32) from both

  70. ends of this String returning an empty String ("") if the String
  71. is empty ("") after the trim or if it is null.

  72. The String is trimmed using {@link String#trim()}.

  73. Trim removes start and end characters <= 32.
  74. To strip whitespace use {@link /stripToEmpty(String)}.


  75. 格式化一个字符串中的空格,有非空判断处理,如果为空返回""; StringUtils.trimToEmpty(null) = "" StringUtils.trimToEmpty("") = "" StringUtils.trimToEmpty(" ") = "" StringUtils.trimToEmpty("abc") = "abc" StringUtils.trimToEmpty(" abc ") = "abc"

  76. @param str the String to be trimmed, may be null
  77. @return the trimmed String, or an empty String if null input
  78. @since 2.0
  79. public static String trimToEmpty(String str)
复制代码

1 个回复

倒序浏览
  1. 五、 字符串比较:
  2. 9、
  3. Compares two Strings, returning true if they are equal.


  4. nulls are handled without exceptions. Two null

  5. references are considered to be equal. The comparison is case sensitive.


  6. 判断两个字符串是否相等,有非空处理。 StringUtils.equals(null, null) = true StringUtils.equals(null, "abc") = false StringUtils.equals("abc", null) = false StringUtils.equals("abc", "abc") = true StringUtils.equals("abc", "ABC") = false

  7. @param str1 the first String, may be null
  8. @param str2 the second String, may be null
  9. @return true if the Strings are equal, case sensitive, or
  10. both null
  11. @see java.lang.String#equals(Object)
  12. public static boolean equals(String str1, String str2)


  13. 10、

  14. Compares two Strings, returning true if they are equal ignoring

  15. the case.


  16. nulls are handled without exceptions. Two null

  17. references are considered equal. Comparison is case insensitive.


  18. 判断两个字符串是否相等,有非空处理。忽略大小写 StringUtils.equalsIgnoreCase(null, null) = true StringUtils.equalsIgnoreCase(null, "abc") = false StringUtils.equalsIgnoreCase("abc", null) = false StringUtils.equalsIgnoreCase("abc", "abc") = true StringUtils.equalsIgnoreCase("abc", "ABC") = true

  19. @param str1 the first String, may be null
  20. @param str2 the second String, may be null
  21. @return true if the Strings are equal, case insensitive, or
  22. both null
  23. @see java.lang.String#equalsIgnoreCase(String)
  24. public static boolean equalsIgnoreCase(String str1, String str2)


  25. 六、 IndexOf 处理
  26. 11、


  27. Finds the first index within a String, handling null.

  28. This method uses {@link String#indexOf(String)}.


  29. A null String will return -1.


  30. 返回要查找的字符串所在位置,有非空处理 StringUtils.indexOf(null, *) = -1 StringUtils.indexOf(*, null) = -1 StringUtils.indexOf("", "") = 0 StringUtils.indexOf("aabaabaa", "a") = 0 StringUtils.indexOf("aabaabaa", "b") = 2 StringUtils.indexOf("aabaabaa", "ab") = 1 StringUtils.indexOf("aabaabaa", "") = 0

  31. @param str the String to check, may be null
  32. @param searchStr the String to find, may be null
  33. @return the first index of the search String,
  34. -1 if no match or null string input
  35. @since 2.0
  36. public static int indexOf(String str, String searchStr)

  37. 12、

  38. Finds the first index within a String, handling null.

  39. This method uses {@link String#indexOf(String, int)}.


  40. A null String will return -1.

  41. A negative start position is treated as zero.
  42. An empty ("") search String always matches.
  43. A start position greater than the string length only matches
  44. an empty search String.


  45. 返回要由指定位置开始查找的字符串所在位置,有非空处理 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

  46. @param str the String to check, may be null
  47. @param searchStr the String to find, may be null
  48. @param startPos the start position, negative treated as zero
  49. @return the first index of the search String,
  50. -1 if no match or null string input
  51. @since 2.0
  52. public static int indexOf(String str, String searchStr, int startPos)
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马