分享一个String工具类,也许你的项目中会用得到

每次做项目都会遇到字符串的处理,每次都会去写一个StringUtil,完成一些功能。

但其实每次要的功能都差不多:

1.判断类(包括NULL和空串、是否是空白字符串等)

2.默认值

3.去空白(trim)

4.比较

5.字符类型判断(是否只包含数字、字母)

6.大小写转换(首字母大小写等)

7.字符串分割

8.字符串连接

9.字符串查找

10.取子串

11.删除字符

12.字符串比较

下面是一个字符串的工具类,涵盖了上面列的功能,算是比较完整的。

* 有关字符串处理的工具类。 3 * 4 * <p> 5 * 这个类中的每个方法都可以“安全”地处理<code>null</code>,而不会抛出<code>NullPointerException</code>。 6 * </p> 7 * StringUtil {String EMPTY_STRING = “”;* 检查字符串是否为<code>null</code>或空字符串<code>””</code>。 28 * <pre> 29 * StringUtil.isEmpty(null)= true 30 * StringUtil.isEmpty(“”)= true 31 * StringUtil.isEmpty(” “)= false 32 * StringUtil.isEmpty(“bob”)= false 33 * StringUtil.isEmpty(” bob “) = false 34 * </pre> 35 * str 要检查的字符串 37 * 如果为空, 则返回<code>true</code> isEmpty(String str) { 41return ((str == null) || (str.length() == 0)); 42 }* 检查字符串是否不是<code>null</code>和空字符串<code>””</code>。 46 * <pre> 47 * StringUtil.isEmpty(null)= false 48 * StringUtil.isEmpty(“”)= false 49 * StringUtil.isEmpty(” “)= true 50 * StringUtil.isEmpty(“bob”)= true 51 * StringUtil.isEmpty(” bob “) = true 52 * </pre> 53 * str 要检查的字符串 55 * 如果不为空, 则返回<code>true</code> isNotEmpty(String str) { 59return ((str != null) && (str.length() > 0)); 60 }* 检查字符串是否是空白:<code>null</code>、空字符串<code>””</code>或只有空白字符。 64 * <pre> 65 * StringUtil.isBlank(null)= true 66 * StringUtil.isBlank(“”)= true 67 * StringUtil.isBlank(” “)= true 68 * StringUtil.isBlank(“bob”)= false 69 * StringUtil.isBlank(” bob “) = false 70 * </pre> 71 * str 要检查的字符串 73 * 如果为空白, 则返回<code>true</code> isBlank(String str) { 77int length;((str == null) || ((length = str.length()) == 0)) {; 81 }(int i = 0; i < length; i++) { 84if (!Character.isWhitespace(str.charAt(i))) {; 86 } 87 }; 90 }* 检查字符串是否不是空白:<code>null</code>、空字符串<code>””</code>或只有空白字符。 94 * <pre> 95 * StringUtil.isBlank(null)= false 96 * StringUtil.isBlank(“”)= false 97 * StringUtil.isBlank(” “)= false 98 * StringUtil.isBlank(“bob”)= true 99 * StringUtil.isBlank(” bob “) = true 100 * </pre> 101 * str 要检查的字符串 103 * 如果为空白, 则返回<code>true</code> isNotBlank(String str) { 107int length;((str == null) || ((length = str.length()) == 0)) {; 111 }(int i = 0; i < length; i++) { 114if (!Character.isWhitespace(str.charAt(i))) {; 116 } 117 }; 120 }* 如果字符串是<code>null</code>,则返回空字符串<code>””</code>,否则返回字符串本身。 130 * <pre> 131 * StringUtil.defaultIfNull(null) = “” 132 * StringUtil.defaultIfNull(“”) = “” 133 * StringUtil.defaultIfNull(” “) = ” ” 134 * StringUtil.defaultIfNull(“bat”) = “bat” 135 * </pre> 136 * str 要转换的字符串 138 * 字符串本身或空字符串<code>””</code> String defaultIfNull(String str) { 142return (str == null) ? EMPTY_STRING : str; 143 }* 如果字符串是<code>null</code>,则返回指定默认字符串,否则返回字符串本身。 147 * <pre> 148 * StringUtil.defaultIfNull(null, “default”) = “default” 149 * StringUtil.defaultIfNull(“”, “default”) = “” 150 * StringUtil.defaultIfNull(” “, “default”) = ” ” 151 * StringUtil.defaultIfNull(“bat”, “default”) = “bat” 152 * </pre> 153 * str 要转换的字符串 defaultStr 默认字符串 156 * 字符串本身或指定的默认字符串 String defaultIfNull(String str, String defaultStr) { 160return (str == null) ? defaultStr : str; 161 }* 如果字符串是<code>null</code>或空字符串<code>””</code>,则返回空字符串<code>””</code>,否则返回字符串本身。 165 * 166 * <p> 167 * 此方法实际上和<code>defaultIfNull(String)</code>等效。 168 * <pre> 169 * StringUtil.defaultIfEmpty(null) = “” 170 * StringUtil.defaultIfEmpty(“”) = “” 171 * StringUtil.defaultIfEmpty(” “) = ” ” 172 * StringUtil.defaultIfEmpty(“bat”) = “bat” 173 * </pre> 174 * </p> 175 * str 要转换的字符串 177 * 字符串本身或空字符串<code>””</code> String defaultIfEmpty(String str) { 181return (str == null) ? EMPTY_STRING : str; 182 }* 如果字符串是<code>null</code>或空字符串<code>””</code>,则返回指定默认字符串,否则返回字符串本身。 186 * <pre> 187 * StringUtil.defaultIfEmpty(null, “default”) = “default” 188 * StringUtil.defaultIfEmpty(“”, “default”) = “default” 189 * StringUtil.defaultIfEmpty(” “, “default”) = ” ” 190 * StringUtil.defaultIfEmpty(“bat”, “default”) = “bat” 191 * </pre> 192 * str 要转换的字符串 defaultStr 默认字符串 195 * 字符串本身或指定的默认字符串 String defaultIfEmpty(String str, String defaultStr) { 199return ((str == null) || (str.length() == 0)) ? defaultStr : str; 200 }* 如果字符串是空白:<code>null</code>、空字符串<code>””</code>或只有空白字符,则返回空字符串<code>””</code>,否则返回字符串本身。 204 * <pre> 205 * StringUtil.defaultIfBlank(null) = “” 206 * StringUtil.defaultIfBlank(“”) = “” 207 * StringUtil.defaultIfBlank(” “) = “” 208 * StringUtil.defaultIfBlank(“bat”) = “bat” 209 * </pre> 210 * str 要转换的字符串 212 * 字符串本身或空字符串<code>””</code> String defaultIfBlank(String str) { 216return isBlank(str) ? EMPTY_STRING : str; 217 }* 如果字符串是<code>null</code>或空字符串<code>””</code>,则返回指定默认字符串,否则返回字符串本身。 221 * <pre> 222 * StringUtil.defaultIfBlank(null, “default”) = “default” 223 * StringUtil.defaultIfBlank(“”, “default”) = “default” 224 * StringUtil.defaultIfBlank(” “, “default”) = “default” 225 * StringUtil.defaultIfBlank(“bat”, “default”) = “bat” 226 * </pre> 227 * str 要转换的字符串 defaultStr 默认字符串 230 * 字符串本身或指定的默认字符串 String defaultIfBlank(String str, String defaultStr) { 234return isBlank(str) ? defaultStr : str; 235 }* 除去字符串头尾部的空白,如果字符串是<code>null</code>,依然返回<code>null</code>。 245 * 246 * <p> 247 * 注意,和<code>String.trim</code>不同,此方法使用<code>Character.isWhitespace</code>来判定空白, 248 * 因而可以除去英文字符集之外的其它空白,如中文空格。 249 * <pre> 250 * StringUtil.trim(null)= null 251 * StringUtil.trim(“”)= “” 252 * StringUtil.trim(“”)= “” 253 * StringUtil.trim(“abc”)= “abc” 254 * StringUtil.trim(” abc “) = “abc” 255 * </pre> 256 * </p> 257 * str 要处理的字符串 259 * 除去空白的字符串,如果原字串为<code>null</code>,则返回<code>null</code> String trim(String str) { 263return trim(str, null, 0); 264 }* 除去字符串头尾部的指定字符,如果字符串是<code>null</code>,依然返回<code>null</code>。 268 * <pre> 269 * StringUtil.trim(null, *)= null 270 * StringUtil.trim(“”, *)= “” 271 * StringUtil.trim(“abc”, null)= “abc” 272 * StringUtil.trim(” abc”, null) = “abc” 273 * StringUtil.trim(“abc “, null) = “abc” 274 * StringUtil.trim(” abc “, null) = “abc” 275 * StringUtil.trim(” abcyx”, “xyz”) = ” abc” 276 * </pre> 277 * str 要处理的字符串 stripChars 要除去的字符,如果为<code>null</code>表示除去空白字符 280 * 除去指定字符后的的字符串,如果原字串为<code>null</code>,则返回<code>null</code> String trim(String str, String stripChars) { 284return trim(str, stripChars, 0); 285 }* 除去字符串头部的空白,如果字符串是<code>null</code>,则返回<code>null</code>。 289 * 290 * <p> 291 * 注意,和<code>String.trim</code>不同,此方法使用<code>Character.isWhitespace</code>来判定空白, 292 * 因而可以除去英文字符集之外的其它空白,如中文空格。 293 * <pre> 294 * StringUtil.trimStart(null)= null 295 * StringUtil.trimStart(“”)= “” 296 * StringUtil.trimStart(“abc”)= “abc” 297 * StringUtil.trimStart(” abc”)= “abc” 298 * StringUtil.trimStart(“abc “)= “abc ” 299 * StringUtil.trimStart(” abc “)= “abc ” 300 * </pre> 301 * </p> 302 * str 要处理的字符串 304 * 除去空白的字符串,如果原字串为<code>null</code>或结果字符串为<code>””</code>,则返回<code>null</code> String trimStart(String str) { 308return trim(str, null, -1); 309 }* 除去字符串头部的指定字符,如果字符串是<code>null</code>,依然返回<code>null</code>。 313 * <pre> 314 * StringUtil.trimStart(null, *)= null 315 * StringUtil.trimStart(“”, *)= “” 316 * StringUtil.trimStart(“abc”, “”)= “abc” 317 * StringUtil.trimStart(“abc”, null)= “abc” 318 * StringUtil.trimStart(” abc”, null) = “abc” 319 * StringUtil.trimStart(“abc “, null) = “abc ” 320 * StringUtil.trimStart(” abc “, null) = “abc ” 321 * StringUtil.trimStart(“yxabc “, “xyz”) = “abc ” 322 * </pre> 323 * str 要处理的字符串 stripChars 要除去的字符,如果为<code>null</code>表示除去空白字符 326 * 除去指定字符后的的字符串,如果原字串为<code>null</code>,则返回<code>null</code> String trimStart(String str, String stripChars) { 330return trim(str, stripChars, -1); 331 }* 除去字符串尾部的空白,如果字符串是<code>null</code>,则返回<code>null</code>。 335 * 336 * <p> 337 * 注意,和<code>String.trim</code>不同,香港服务器租用,此方法使用<code>Character.isWhitespace</code>来判定空白, 338 * 因而可以除去英文字符集之外的其它空白,如中文空格。 339 * <pre> 340 * StringUtil.trimEnd(null)= null 341 * StringUtil.trimEnd(“”)= “” 342 * StringUtil.trimEnd(“abc”)= “abc” 343 * StringUtil.trimEnd(” abc”) = ” abc” 344 * StringUtil.trimEnd(“abc “) = “abc” 345 * StringUtil.trimEnd(” abc “) = ” abc” 346 * </pre> 347 * </p> 348 * str 要处理的字符串 350 * 除去空白的字符串,如果原字串为<code>null</code>或结果字符串为<code>””</code>,则返回<code>null</code> String trimEnd(String str) { 354return trim(str, null, 1); 355 }* 除去字符串尾部的指定字符,如果字符串是<code>null</code>,依然返回<code>null</code>。 359 * <pre> 360 * StringUtil.trimEnd(null, *)= null 361 * StringUtil.trimEnd(“”, *)= “” 362 * StringUtil.trimEnd(“abc”, “”)= “abc” 363 * StringUtil.trimEnd(“abc”, null)= “abc” 364 * StringUtil.trimEnd(” abc”, null) = ” abc” 365 * StringUtil.trimEnd(“abc “, null) = “abc” 366 * StringUtil.trimEnd(” abc “, null) = ” abc” 367 * StringUtil.trimEnd(” abcyx”, “xyz”) = ” abc” 368 * </pre> 369 * str 要处理的字符串 stripChars 要除去的字符,如果为<code>null</code>表示除去空白字符 372 * 除去指定字符后的的字符串,如果原字串为<code>null</code>,则返回<code>null</code> String trimEnd(String str, String stripChars) { 376return trim(str, stripChars, 1); 377 }* 除去字符串头尾部的空白,如果结果字符串是空字符串<code>””</code>,则返回<code>null</code>。 381 * 382 * <p> 383 * 注意,和<code>String.trim</code>不同,此方法使用<code>Character.isWhitespace</code>来判定空白, 384 * 因而可以除去英文字符集之外的其它空白,如中文空格。 385 * <pre> 386 * StringUtil.trimToNull(null)= null 387 * StringUtil.trimToNull(“”)= null 388 * StringUtil.trimToNull(“”)= null 389 * StringUtil.trimToNull(“abc”)= “abc” 390 * StringUtil.trimToNull(” abc “) = “abc” 391 * </pre> 392 * </p> 393 * str 要处理的字符串 395 * 除去空白的字符串,如果原字串为<code>null</code>或结果字符串为<code>””</code>,则返回<code>null</code> String trimToNull(String str) { 399return trimToNull(str, null); 400 }* 除去字符串头尾部的空白,如果结果字符串是空字符串<code>””</code>,则返回<code>null</code>。 404 * 405 * <p> 406 * 注意,和<code>String.trim</code>不同,此方法使用<code>Character.isWhitespace</code>来判定空白, 407 * 因而可以除去英文字符集之外的其它空白,如中文空格。 408 * <pre> 409 * StringUtil.trim(null, *)= null 410 * StringUtil.trim(“”, *)= null 411 * StringUtil.trim(“abc”, null)= “abc” 412 * StringUtil.trim(” abc”, null) = “abc” 413 * StringUtil.trim(“abc “, null) = “abc” 414 * StringUtil.trim(” abc “, null) = “abc” 415 * StringUtil.trim(” abcyx”, “xyz”) = ” abc” 416 * </pre> 417 * </p> 418 * str 要处理的字符串 stripChars 要除去的字符,如果为<code>null</code>表示除去空白字符 421 * 除去空白的字符串,如果原字串为<code>null</code>或结果字符串为<code>””</code>,则返回<code>null</code> String trimToNull(String str, String stripChars) { 425String result = trim(str, stripChars);((result == null) || (result.length() == 0)) {; 429 } result; 432 }* 除去字符串头尾部的空白,如果字符串是<code>null</code>,则返回空字符串<code>””</code>。 436 * 437 * <p> 438 * 注意,和<code>String.trim</code>不同,此方法使用<code>Character.isWhitespace</code>来判定空白, 439 * 因而可以除去英文字符集之外的其它空白,如中文空格。 440 * <pre> 441 * StringUtil.trimToEmpty(null)= “” 442 * StringUtil.trimToEmpty(“”)= “” 443 * StringUtil.trimToEmpty(“”)= “” 444 * StringUtil.trimToEmpty(“abc”)= “abc” 445 * StringUtil.trimToEmpty(” abc “) = “abc” 446 * </pre> 447 * </p> 448 * str 要处理的字符串 450 * 除去空白的字符串,如果原字串为<code>null</code>或结果字符串为<code>””</code>,则返回<code>null</code> String trimToEmpty(String str) { 454return trimToEmpty(str, null); 455 }* 除去字符串头尾部的空白,如果字符串是<code>null</code>,则返回空字符串<code>””</code>。 459 * 460 * <p> 461 * 注意,和<code>String.trim</code>不同,此方法使用<code>Character.isWhitespace</code>来判定空白, 462 * 因而可以除去英文字符集之外的其它空白,如中文空格。 463 * <pre> 464 * StringUtil.trim(null, *)= “” 465 * StringUtil.trim(“”, *)= “” 466 * StringUtil.trim(“abc”, null)= “abc” 467 * StringUtil.trim(” abc”, null) = “abc” 468 * StringUtil.trim(“abc “, null) = “abc” 469 * StringUtil.trim(” abc “, null) = “abc” 470 * StringUtil.trim(” abcyx”, “xyz”) = ” abc” 471 * </pre> 472 * </p> 473 * str 要处理的字符串 475 * 除去空白的字符串,如果原字串为<code>null</code>或结果字符串为<code>””</code>,则返回<code>null</code> String trimToEmpty(String str, String stripChars) { 479String result = trim(str, stripChars);(result == null) { 482return EMPTY_STRING; 483 } result; 486 }* 除去字符串头尾部的指定字符,如果字符串是<code>null</code>,依然返回<code>null</code>。 490 * <pre> 491 * StringUtil.trim(null, *)= null 492 * StringUtil.trim(“”, *)= “” 493 * StringUtil.trim(“abc”, null)= “abc” 494 * StringUtil.trim(” abc”, null) = “abc” 495 * StringUtil.trim(“abc “, null) = “abc” 496 * StringUtil.trim(” abc “, null) = “abc” 497 * StringUtil.trim(” abcyx”, “xyz”) = ” abc” 498 * </pre> 499 * str 要处理的字符串 stripChars 要除去的字符,如果为<code>null</code>表示除去空白字符 mode <code>-1</code>表示trimStart,<code>0</code>表示trim全部,<code>1</code>表示trimEnd 503 * 除去指定字符后的的字符串,如果原字串为<code>null</code>,则返回<code>null</code>String trim(String str, String stripChars, int mode) { 507if (str == null) {; 509 }length = str.length(); 512int start = 0; 513int end = length;(mode <= 0) { 517if (stripChars == null) { 518while ((start < end) && (Character.isWhitespace(str.charAt(start)))) { 519start++; 520 } 521} else if (stripChars.length() == 0) { 522return str; 523} else { 524while ((start < end) && (stripChars.indexOf(str.charAt(start)) != -1)) { 525start++; 526 } 527 } 528 }(mode >= 0) { 532if (stripChars == null) { 533while ((start < end) && (Character.isWhitespace(str.charAt(end – 1)))) { 534end–; 535 } 536} else if (stripChars.length() == 0) { 537return str; 538} else { 539while ((start < end) && (stripChars.indexOf(str.charAt(end – 1)) != -1)) { 540end–; 541 } 542 } 543 }((start > 0) || (end < length)) { 546return str.substring(start, end); 547 } str; 550 }* 比较两个字符串(大小写敏感)。 560 * <pre> 561 * StringUtil.equals(null, null) = true 562 * StringUtil.equals(null, “abc”) = false 563 * StringUtil.equals(“abc”, null) = false 564 * StringUtil.equals(“abc”, “abc”) = true 565 * StringUtil.equals(“abc”, “ABC”) = false 566 * </pre> 567 * str1 要比较的字符串1 str2 要比较的字符串2 570 * 如果两个字符串相同,或者都是<code>null</code>,则返回<code>true</code> equals(String str1, String str2) { 574if (str1 == null) { 575return str2 == null; 576 } str1.equals(str2); 579 }* 比较两个字符串(大小写不敏感)。 583 * <pre> 584 * StringUtil.equalsIgnoreCase(null, null) = true 585 * StringUtil.equalsIgnoreCase(null, “abc”) = false 586 * StringUtil.equalsIgnoreCase(“abc”, null) = false 587 * StringUtil.equalsIgnoreCase(“abc”, “abc”) = true 588 * StringUtil.equalsIgnoreCase(“abc”, “ABC”) = true 589 * </pre> 590 * str1 要比较的字符串1 str2 要比较的字符串2 593 * 如果两个字符串相同,或者都是<code>null</code>,则返回<code>true</code> equalsIgnoreCase(String str1, String str2) { 597if (str1 == null) { 598return str2 == null; 599 } str1.equalsIgnoreCase(str2); 602 }* 判断字符串是否只包含unicode字母。 612 * 613 * <p> 614 * <code>null</code>将返回<code>false</code>,空字符串<code>””</code>将返回<code>true</code>。 615 * </p> 616 * <pre> 617 * StringUtil.isAlpha(null) = false 618 * StringUtil.isAlpha(“”)= true 619 * StringUtil.isAlpha(” “) = false 620 * StringUtil.isAlpha(“abc”) = true 621 * StringUtil.isAlpha(“ab2c”) = false 622 * StringUtil.isAlpha(“ab-c”) = false 623 * </pre> 624 * str 要检查的字符串 626 * 如果字符串非<code>null</code>并且全由unicode字母组成,则返回<code>true</code> isAlpha(String str) { 630if (str == null) {; 632 }length = str.length();(int i = 0; i < length; i++) { 637if (!Character.isLetter(str.charAt(i))) {; 639 } 640 }; 643 }* 判断字符串是否只包含unicode字母和空格<code>’ ‘</code>。 647 * 648 * <p> 649 * <code>null</code>将返回<code>false</code>,空字符串<code>””</code>将返回<code>true</code>。 650 * </p> 651 * <pre> 652 * StringUtil.isAlphaSpace(null) = false 653 * StringUtil.isAlphaSpace(“”)= true 654 * StringUtil.isAlphaSpace(” “) = true 655 * StringUtil.isAlphaSpace(“abc”) = true 656 * StringUtil.isAlphaSpace(“ab c”) = true 657 * StringUtil.isAlphaSpace(“ab2c”) = false 658 * StringUtil.isAlphaSpace(“ab-c”) = false 659 * </pre> 660 * str 要检查的字符串 662 * 如果字符串非<code>null</code>并且全由unicode字母和空格组成,则返回<code>true</code> isAlphaSpace(String str) { 666if (str == null) {; 668 }length = str.length();(int i = 0; i < length; i++) { 673if (!Character.isLetter(str.charAt(i)) && (str.charAt(i) != ‘ ‘)) {; 675 } 676 }; 679 }* 判断字符串是否只包含unicode字母和数字。 683 * 684 * <p> 685 * <code>null</code>将返回<code>false</code>,空字符串<code>””</code>将返回<code>true</code>。 686 * </p> 687 * <pre> 688 * StringUtil.isAlphanumeric(null) = false 689 * StringUtil.isAlphanumeric(“”)= true 690 * StringUtil.isAlphanumeric(” “) = false 691 * StringUtil.isAlphanumeric(“abc”) = true 692 * StringUtil.isAlphanumeric(“ab c”) = false 693 * StringUtil.isAlphanumeric(“ab2c”) = true 694 * StringUtil.isAlphanumeric(“ab-c”) = false 695 * </pre> 696 * str 要检查的字符串 698 * 如果字符串非<code>null</code>并且全由unicode字母数字组成,则返回<code>true</code> isAlphanumeric(String str) { 702if (str == null) {; 704 }length = str.length();(int i = 0; i < length; i++) { 709if (!Character.isLetterOrDigit(str.charAt(i))) {; 711 } 712 }; 715 }* 判断字符串是否只包含unicode字母数字和空格<code>’ ‘</code>。 719 * 720 * <p> 721 * <code>null</code>将返回<code>false</code>,空字符串<code>””</code>将返回<code>true</code>。 722 * </p> 723 * <pre> 724 * StringUtil.isAlphanumericSpace(null) = false 725 * StringUtil.isAlphanumericSpace(“”)= true 726 * StringUtil.isAlphanumericSpace(” “) = true 727 * StringUtil.isAlphanumericSpace(“abc”) = true 728 * StringUtil.isAlphanumericSpace(“ab c”) = true 729 * StringUtil.isAlphanumericSpace(“ab2c”) = true 730 * StringUtil.isAlphanumericSpace(“ab-c”) = false 731 * </pre> 732 * str 要检查的字符串 734 * 如果字符串非<code>null</code>并且全由unicode字母数字和空格组成,则返回<code>true</code> isAlphanumericSpace(String str) { 738if (str == null) {; 740 }length = str.length();(int i = 0; i < length; i++) { 745if (!Character.isLetterOrDigit(str.charAt(i)) && (str.charAt(i) != ‘ ‘)) {; 747 } 748 }; 751 }* 判断字符串是否只包含unicode数字。 755 * 756 * <p> 757 * <code>null</code>将返回<code>false</code>,空字符串<code>””</code>将返回<code>true</code>。 758 * </p> 759 * <pre> 760 * StringUtil.isNumeric(null) = false 761 * StringUtil.isNumeric(“”)= true 762 * StringUtil.isNumeric(” “) = false 763 * StringUtil.isNumeric(“123”) = true 764 * StringUtil.isNumeric(“12 3”) = false 765 * StringUtil.isNumeric(“ab2c”) = false 766 * StringUtil.isNumeric(“12-3”) = false 767 * StringUtil.isNumeric(“12.3”) = false 768 * </pre> 769 * str 要检查的字符串 771 * 如果字符串非<code>null</code>并且全由unicode数字组成,则返回<code>true</code> isNumeric(String str) { 775if (str == null) {; 777 }length = str.length();(int i = 0; i < length; i++) { 782if (!Character.isDigit(str.charAt(i))) {; 784 } 785 }; 788 }* 判断字符串是否只包含unicode数字,包括小数。 792 * 793 * <p> 794 * <code>null</code>将返回<code>false</code>,空字符串<code>””</code>将返回<code>true</code>。 795 * </p> 796 * <pre> 797 * StringUtil.isNumeric(null) = false 798 * StringUtil.isNumeric(“”)= false 799 * StringUtil.isNumeric(” “) = false 800 * StringUtil.isNumeric(“123”) = true 801 * StringUtil.isNumeric(“12 3”) = false 802 * StringUtil.isNumeric(“ab2c”) = false 803 * StringUtil.isNumeric(“12-3”) = false 804 * StringUtil.isNumeric(“12.3”) = true 805 * </pre> 806 * str 要检查的字符串 808 * 如果字符串非<code>null</code>并且全由unicode数字组成,则返回<code>true</code> isNumber(String str) { 812if (isBlank(str)) {; 814 } 815int index = str.indexOf(“.”); 816if (index < 0) { 817return isNumeric(str); 818} else { 819String num1 = str.substring(0, index); 820String num2 = str.substring(index + 1); 821return isNumeric(num1) && isNumeric(num2); 822 } 823 }* 判断字符串是否只包含unicode数字和空格<code>’ ‘</code>。 828 * 829 * <p> 830 * <code>null</code>将返回<code>false</code>,空字符串<code>””</code>将返回<code>true</code>。 831 * </p> 832 * <pre> 833 * StringUtil.isNumericSpace(null) = false 834 * StringUtil.isNumericSpace(“”)= true 835 * StringUtil.isNumericSpace(” “) = true 836 * StringUtil.isNumericSpace(“123”) = true 837 * StringUtil.isNumericSpace(“12 3”) = true 838 * StringUtil.isNumericSpace(“ab2c”) = false 839 * StringUtil.isNumericSpace(“12-3”) = false 840 * StringUtil.isNumericSpace(“12.3”) = false 841 * </pre> 842 * str 要检查的字符串 844 * 如果字符串非<code>null</code>并且全由unicode数字和空格组成,则返回<code>true</code> isNumericSpace(String str) { 848if (str == null) {; 850 }length = str.length();(int i = 0; i < length; i++) { 855if (!Character.isDigit(str.charAt(i)) && (str.charAt(i) != ‘ ‘)) {; 857 } 858 }; 861 }* 判断字符串是否只包含unicode空白。 865 * 866 * <p> 867 * <code>null</code>将返回<code>false</code>,空字符串<code>””</code>将返回<code>true</code>。 868 * </p> 869 * <pre> 870 * StringUtil.isWhitespace(null) = false 871 * StringUtil.isWhitespace(“”)= true 872 * StringUtil.isWhitespace(” “) = true 873 * StringUtil.isWhitespace(“abc”) = false 874 * StringUtil.isWhitespace(“ab2c”) = false 875 * StringUtil.isWhitespace(“ab-c”) = false 876 * </pre> 877 * str 要检查的字符串 879 * 如果字符串非<code>null</code>并且全由unicode空白组成,则返回<code>true</code> isWhitespace(String str) { 883if (str == null) {; 885 }length = str.length();(int i = 0; i < length; i++) { 890if (!Character.isWhitespace(str.charAt(i))) {; 892 } 893 }; 896 }* 将字符串转换成大写。 904 * 905 * <p> 906 * 如果字符串是<code>null</code>则返回<code>null</code>。 907 * <pre> 908 * StringUtil.toUpperCase(null) = null 909 * StringUtil.toUpperCase(“”) = “” 910 * StringUtil.toUpperCase(“aBc”) = “ABC” 911 * </pre> 912 * </p> 913 * str 要转换的字符串 915 * 大写字符串,如果原字符串为<code>null</code>,则返回<code>null</code> String toUpperCase(String str) { 919if (str == null) {; 921 } str.toUpperCase(); 924 }* 将字符串转换成小写。 928 * 929 * <p> 930 * 如果字符串是<code>null</code>则返回<code>null</code>。 931 * <pre> 932 * StringUtil.toLowerCase(null) = null 933 * StringUtil.toLowerCase(“”) = “” 934 * StringUtil.toLowerCase(“aBc”) = “abc” 935 * </pre> 936 * </p> 937 * str 要转换的字符串 939 * 大写字符串,如果原字符串为<code>null</code>,则返回<code>null</code> String toLowerCase(String str) { 943if (str == null) {; 945 } str.toLowerCase(); 948 }* 将字符串的首字符转成大写(<code>Character.toTitleCase</code>),其它字符不变。 952 * 953 * <p> 954 * 如果字符串是<code>null</code>则返回<code>null</code>。 955 * <pre> 956 * StringUtil.capitalize(null) = null 957 * StringUtil.capitalize(“”) = “” 958 * StringUtil.capitalize(“cat”) = “Cat” 959 * StringUtil.capitalize(“cAt”) = “CAt” 960 * </pre> 961 * </p> 962 * str 要转换的字符串 964 * 首字符为大写的字符串,如果原字符串为<code>null</code>,则返回<code>null</code> String capitalize(String str) { 968int strLen;((str == null) || ((strLen = str.length()) == 0)) { 971return str; 972 }StringBuffer(strLen).append(Character.toTitleCase(str.charAt(0))).append( 975str.substring(1)).toString(); 976 }* 将字符串的首字符转成小写,其它字符不变。 980 * 981 * <p> 982 * 如果字符串是<code>null</code>则返回<code>null</code>。 983 * <pre> 984 * StringUtil.uncapitalize(null) = null 985 * StringUtil.uncapitalize(“”) = “” 986 * StringUtil.uncapitalize(“Cat”) = “cat” 987 * StringUtil.uncapitalize(“CAT”) = “cAT” 988 * </pre> 989 * </p> 990 * str 要转换的字符串 992 * 首字符为小写的字符串,如果原字符串为<code>null</code>,则返回<code>null</code> String uncapitalize(String str) { 996int strLen;((str == null) || ((strLen = str.length()) == 0)) { 999return str;1000 }StringBuffer(strLen).append(Character.toLowerCase(str.charAt(0))).append(1003str.substring(1)).toString();1004 }* 反转字符串的大小写。1008 *1009 * <p>1010 * 如果字符串是<code>null</code>则返回<code>null</code>。1011 * <pre>1012 * StringUtil.swapCase(null)= null1013 * StringUtil.swapCase(“”)= “”1014 * StringUtil.swapCase(“The dog has a BONE”) = “tHE DOG HAS A bone”1015 * </pre>1016 * </p>1017 * str 要转换的字符串1019 * 大小写被反转的字符串,如果原字符串为<code>null</code>,则返回<code>null</code> String swapCase(String str) {1023int strLen;((str == null) || ((strLen = str.length()) == 0)) {1026return str;1027 }1028 1029StringBuffer buffer = new StringBuffer(strLen);ch = 0;(int i = 0; i < strLen; i++) {1034ch = str.charAt(i); (Character.isUpperCase(ch)) {1037ch = Character.toLowerCase(ch);1038} else if (Character.isTitleCase(ch)) {1039ch = Character.toLowerCase(ch);1040} else if (Character.isLowerCase(ch)) {1041ch = Character.toUpperCase(ch);1042 }1043 1044 buffer.append(ch);1045 } buffer.toString();1048 }* 将字符串转换成camel case。1052 *1053 * <p>1054 * 如果字符串是<code>null</code>则返回<code>null</code>。1055 * <pre>1056 * StringUtil.toCamelCase(null) = null1057 * StringUtil.toCamelCase(“”) = “”1058 * StringUtil.toCamelCase(“aBc”) = “aBc”1059 * StringUtil.toCamelCase(“aBc def”) = “aBcDef”1060 * StringUtil.toCamelCase(“aBc def_ghi”) = “aBcDefGhi”1061 * StringUtil.toCamelCase(“aBc def_ghi 123”) = “aBcDefGhi123″1062 * </pre>1063 * </p>1064 *1065 * <p>1066 * 此方法会保留除了下划线和空白以外的所有分隔符。1067 * </p>1068 * str 要转换的字符串1070 * camel case字符串,如果原字符串为<code>null</code>,则返回<code>null</code> String toCamelCase(String str) {1074return CAMEL_CASE_TOKENIZER.parse(str);1075 }* 将字符串转换成pascal case。1079 *1080 * <p>1081 * 如果字符串是<code>null</code>则返回<code>null</code>。1082 * <pre>1083 * StringUtil.toPascalCase(null) = null1084 * StringUtil.toPascalCase(“”) = “”1085 * StringUtil.toPascalCase(“aBc”) = “ABc”1086 * StringUtil.toPascalCase(“aBc def”) = “ABcDef”1087 * StringUtil.toPascalCase(“aBc def_ghi”) = “ABcDefGhi”1088 * StringUtil.toPascalCase(“aBc def_ghi 123”) = “aBcDefGhi123″1089 * </pre>1090 * </p>1091 *1092 * <p>1093 * 此方法会保留除了下划线和空白以外的所有分隔符。1094 * </p>1095 * str 要转换的字符串1097 * pascal case字符串,如果原字符串为<code>null</code>,则返回<code>null</code> String toPascalCase(String str) {1101return PASCAL_CASE_TOKENIZER.parse(str);1102 }* 将字符串转换成下划线分隔的大写字符串。1106 *1107 * <p>1108 * 如果字符串是<code>null</code>则返回<code>null</code>。1109 * <pre>1110 * StringUtil.toUpperCaseWithUnderscores(null) = null1111 * StringUtil.toUpperCaseWithUnderscores(“”) = “”1112 * StringUtil.toUpperCaseWithUnderscores(“aBc”) = “A_BC”1113 * StringUtil.toUpperCaseWithUnderscores(“aBc def”) = “A_BC_DEF”1114 * StringUtil.toUpperCaseWithUnderscores(“aBc def_ghi”) = “A_BC_DEF_GHI”1115 * StringUtil.toUpperCaseWithUnderscores(“aBc def_ghi 123”) = “A_BC_DEF_GHI_123″1116 * StringUtil.toUpperCaseWithUnderscores(“__a__Bc__”) = “__A__BC__”1117 * </pre>1118 * </p>1119 *1120 * <p>1121 * 此方法会保留除了空白以外的所有分隔符。1122 * </p>1123 * str 要转换的字符串1125 * 下划线分隔的大写字符串,如果原字符串为<code>null</code>,则返回<code>null</code> String toUpperCaseWithUnderscores(String str) {1129return UPPER_CASE_WITH_UNDERSCORES_TOKENIZER.parse(str);1130 }* 将字符串转换成下划线分隔的小写字符串。1134 *1135 * <p>1136 * 如果字符串是<code>null</code>则返回<code>null</code>。1137 * <pre>1138 * StringUtil.toLowerCaseWithUnderscores(null) = null1139 * StringUtil.toLowerCaseWithUnderscores(“”) = “”1140 * StringUtil.toLowerCaseWithUnderscores(“aBc”) = “a_bc”1141 * StringUtil.toLowerCaseWithUnderscores(“aBc def”) = “a_bc_def”1142 * StringUtil.toLowerCaseWithUnderscores(“aBc def_ghi”) = “a_bc_def_ghi”1143 * StringUtil.toLowerCaseWithUnderscores(“aBc def_ghi 123”) = “a_bc_def_ghi_123″1144 * StringUtil.toLowerCaseWithUnderscores(“__a__Bc__”) = “__a__bc__”1145 * </pre>1146 * </p>1147 *1148 * <p>1149 * 此方法会保留除了空白以外的所有分隔符。1150 * </p>1151 * str 要转换的字符串1153 * 下划线分隔的小写字符串,如果原字符串为<code>null</code>,则返回<code>null</code> String toLowerCaseWithUnderscores(String str) {1157return LOWER_CASE_WITH_UNDERSCORES_TOKENIZER.parse(str);1158 }WordTokenizer CAMEL_CASE_TOKENIZER= new WordTokenizer() { startSentence(1163 StringBuffer buffer,1164char ch) {1165 buffer1166 .append(Character1167 .toLowerCase(ch));1168 } startWord(1171 StringBuffer buffer,1172char ch) {1173if (!isDelimiter(buffer1174 .charAt(buffer1175.length() – 1))) {1176 buffer1177 .append(Character1178 .toUpperCase(ch));1179} else {1180 buffer1181 .append(Character1182 .toLowerCase(ch));1183 }1184 } inWord(1187 StringBuffer buffer,1188char ch) {1189 buffer1190 .append(Character1191 .toLowerCase(ch));1192 } startDigitSentence(1195 StringBuffer buffer,1196char ch) {1197 buffer1198 .append(ch);1199 } startDigitWord(1202 StringBuffer buffer,1203char ch) {1204 buffer1205 .append(ch);1206 } inDigitWord(1209 StringBuffer buffer,1210char ch) {1211 buffer1212 .append(ch);1213 } inDelimiter(1216 StringBuffer buffer,1217char ch) {1218if (ch != UNDERSCORE) {1219 buffer1220 .append(ch);1221 }1222 }1223 };WordTokenizer PASCAL_CASE_TOKENIZER= new WordTokenizer() { startSentence(1227 StringBuffer buffer,1228char ch) {1229 buffer1230 .append(Character1231 .toUpperCase(ch));1232 } startWord(1235 StringBuffer buffer,1236char ch) {1237 buffer1238 .append(Character1239 .toUpperCase(ch));1240 } inWord(1243 StringBuffer buffer,1244char ch) {1245 buffer1246 .append(Character1247 .toLowerCase(ch));1248 } startDigitSentence(1251 StringBuffer buffer,1252char ch) {1253 buffer1254 .append(ch);1255 } startDigitWord(1258 StringBuffer buffer,1259char ch) {1260 buffer1261 .append(ch);1262 } inDigitWord(1265 StringBuffer buffer,1266char ch) {1267 buffer1268 .append(ch);1269 } inDelimiter(1272 StringBuffer buffer,1273char ch) {1274if (ch != UNDERSCORE) {1275 buffer1276 .append(ch);1277 }1278 }1279 };WordTokenizer UPPER_CASE_WITH_UNDERSCORES_TOKENIZER = new WordTokenizer() { startSentence(1283 StringBuffer buffer,1284char ch) {1285 buffer1286 .append(Character1287 .toUpperCase(ch));1288 } startWord(1291 StringBuffer buffer,1292char ch) {1293if (!isDelimiter(buffer1294 .charAt(buffer1295.length() – 1))) {1296 buffer1297 .append(UNDERSCORE);1298 }1299 1300 buffer1301 .append(Character1302 .toUpperCase(ch));1303 } inWord(1306 StringBuffer buffer,1307char ch) {1308 buffer1309 .append(Character1310 .toUpperCase(ch));1311 } startDigitSentence(1314 StringBuffer buffer,1315char ch) {1316 buffer1317 .append(ch);1318 } startDigitWord(1321 StringBuffer buffer,1322char ch) {1323if (!isDelimiter(buffer1324 .charAt(buffer1325.length() – 1))) {1326 buffer1327 .append(UNDERSCORE);1328 }1329 1330 buffer1331 .append(ch);1332 } inDigitWord(1335 StringBuffer buffer,1336char ch) {1337 buffer1338 .append(ch);1339 } inDelimiter(1342 StringBuffer buffer,1343char ch) {1344 buffer1345 .append(ch);1346 }1347 };WordTokenizer LOWER_CASE_WITH_UNDERSCORES_TOKENIZER = new WordTokenizer() { startSentence(1351 StringBuffer buffer,1352char ch) {1353 buffer1354 .append(Character1355 .toLowerCase(ch));1356 } startWord(1359 StringBuffer buffer,1360char ch) {1361if (!isDelimiter(buffer1362 .charAt(buffer1363.length() – 1))) {1364 buffer1365 .append(UNDERSCORE);1366 }1367 1368 buffer1369 .append(Character1370 .toLowerCase(ch));1371 } inWord(1374 StringBuffer buffer,1375char ch) {1376 buffer1377 .append(Character1378 .toLowerCase(ch));1379 } startDigitSentence(1382 StringBuffer buffer,1383char ch) {1384 buffer1385 .append(ch);1386 } startDigitWord(1389 StringBuffer buffer,1390char ch) {1391if (!isDelimiter(buffer1392 .charAt(buffer1393.length() – 1))) {1394 buffer1395 .append(UNDERSCORE);1396 }1397 1398 buffer1399 .append(ch);1400 } inDigitWord(1403 StringBuffer buffer,1404char ch) {1405 buffer1406 .append(ch);1407 } inDelimiter(1410 StringBuffer buffer,1411char ch) {1412 buffer1413 .append(ch);1414 }1415 };* 解析出下列语法所构成的<code>SENTENCE</code>。1419 * <pre>1420 * SENTENCE = WORD (DELIMITER* WORD)*1421 *1422 * WORD = UPPER_CASE_WORD | LOWER_CASE_WORD | TITLE_CASE_WORD | DIGIT_WORD1423 *1424 * UPPER_CASE_WORD = UPPER_CASE_LETTER+1425 * LOWER_CASE_WORD = LOWER_CASE_LETTER+1426 * TITLE_CASE_WORD = UPPER_CASE_LETTER LOWER_CASE_LETTER+1427 * DIGIT_WORD= DIGIT+1428 *1429 * UPPER_CASE_LETTER = Character.isUpperCase()1430 * LOWER_CASE_LETTER = Character.isLowerCase()1431 * DIGIT= Character.isDigit()1432 * NON_LETTER_DIGIT = !Character.isUpperCase() && !Character.isLowerCase() && !Character.isDigit()1433 *1434 * DELIMITER = WHITESPACE | NON_LETTER_DIGIT1435 * </pre> WordTokenizer {UNDERSCORE = ‘_’;* Parse sentence。 String parse(String str) {1444if (StringUtil.isEmpty(str)) {1445return str;1446 }length = str.length();1449StringBuffer buffer = new StringBuffer(length);(int index = 0; index < length; index++) {1452char ch = str.charAt(index); (Character.isWhitespace(ch)) {1456continue;1457 } (Character.isUpperCase(ch)) {1461int wordIndex = index + 1;(wordIndex < length) {1464char wordChar = str.charAt(wordIndex); (Character.isUpperCase(wordChar)) {1467wordIndex++;1468} else if (Character.isLowerCase(wordChar)) {1469wordIndex–;1470break;1471} else {1472break;1473 }1474 } 1. wordIndex == length,说明最后一个字母为大写,以upperCaseWord处理之。1477// 2. wordIndex == index,网站空间,说明index处为一个titleCaseWord。((wordIndex == length) || (wordIndex > index)) {1480index = parseUpperCaseWord(buffer, str, index, wordIndex);1481} else {1482index = parseTitleCaseWord(buffer, str, index);1483 };1486 } (Character.isLowerCase(ch)) {1490index = parseLowerCaseWord(buffer, str, index);1491continue;1492 } (Character.isDigit(ch)) {1496index = parseDigitWord(buffer, str, index);1497continue;1498 }inDelimiter(buffer, ch);1502 } buffer.toString();1505 }parseUpperCaseWord(StringBuffer buffer, String str, int index, int length) {1508char ch = str.charAt(index++);(buffer.length() == 0) {1512 startSentence(buffer, ch);1513} else {1514 startWord(buffer, ch);1515 }(; index < length; index++) {1519ch = str.charAt(index);1520 inWord(buffer, ch);1521 }index – 1;1524 }parseLowerCaseWord(StringBuffer buffer, String str, int index) {1527char ch = str.charAt(index++);(buffer.length() == 0) {1531 startSentence(buffer, ch);1532} else {1533 startWord(buffer, ch);1534 }length = str.length();(; index < length; index++) {1540ch = str.charAt(index); (Character.isLowerCase(ch)) {1543 inWord(buffer, ch);1544} else {1545break;1546 }1547 }index – 1;1550 }parseTitleCaseWord(StringBuffer buffer, String str, int index) {1553char ch = str.charAt(index++);(buffer.length() == 0) {1557 startSentence(buffer, ch);1558} else {1559 startWord(buffer, ch);1560 }length = str.length();(; index < length; index++) {1566ch = str.charAt(index); (Character.isLowerCase(ch)) {1569 inWord(buffer, ch);1570} else {1571break;1572 }1573 }index – 1;1576 }parseDigitWord(StringBuffer buffer, String str, int index) {1579char ch = str.charAt(index++);(buffer.length() == 0) {1583 startDigitSentence(buffer, ch);1584} else {1585 startDigitWord(buffer, ch);1586 }length = str.length();(; index < length; index++) {1592ch = str.charAt(index); (Character.isDigit(ch)) {1595 inDigitWord(buffer, ch);1596} else {1597break;1598 }1599 }index – 1;1602 }isDelimiter(char ch) {1605return !Character.isUpperCase(ch) && !Character.isLowerCase(ch)1606&& !Character.isDigit(ch);1607 }startSentence(StringBuffer buffer, char ch);startWord(StringBuffer buffer, char ch);inWord(StringBuffer buffer, char ch);startDigitSentence(StringBuffer buffer, char ch);startDigitWord(StringBuffer buffer, char ch);inDigitWord(StringBuffer buffer, char ch);inDelimiter(StringBuffer buffer, char ch);1622 }* 将字符串按空白字符分割。1632 *1633 * <p>1634 * 分隔符不会出现在目标数组中,连续的分隔符就被看作一个。如果字符串为<code>null</code>,则返回<code>null</code>。1635 * <pre>1636 * StringUtil.split(null)= null1637 * StringUtil.split(“”)= []1638 * StringUtil.split(“abc def”) = [“abc”, “def”]1639 * StringUtil.split(“abc def”) = [“abc”, “def”]1640 * StringUtil.split(” abc “) = [“abc”]1641 * </pre>1642 * </p>1643 * str 要分割的字符串1645 * 分割后的字符串数组,如果原字符串为<code>null</code>,则返回<code>null</code> String[] split(String str) {1649return split(str, null, -1);1650 }* 将字符串按指定字符分割。1654 *1655 * <p>1656 * 分隔符不会出现在目标数组中,连续的分隔符就被看作一个。如果字符串为<code>null</code>,则返回<code>null</code>。1657 * <pre>1658 * StringUtil.split(null, *)= null1659 * StringUtil.split(“”, *)= []1660 * StringUtil.split(“a.b.c”, ‘.’) = [“a”, “b”, “c”]1661 * StringUtil.split(“a..b.c”, ‘.’) = [“a”, “b”, “c”]1662 * StringUtil.split(“a:b:c”, ‘.’) = [“a:b:c”]1663 * StringUtil.split(“a b c”, ‘ ‘) = [“a”, “b”, “c”]1664 * </pre>1665 * </p>1666 * str 要分割的字符串 separatorChar 分隔符1669 * 分割后的字符串数组,如果原字符串为<code>null</code>,则返回<code>null</code>String[] split(String str, char separatorChar) {1673if (str == null) {;1675 }length = str.length();(length == 0) {1680return ArrayUtil.EMPTY_STRING_ARRAY;1681 }1682 1683List list = new ArrayList();1684int i = 0;1685int start = 0;1686boolean match = false;(i < length) {1689if (str.charAt(i) == separatorChar) {1690if (match) {1691 list.add(str.substring(start, i));1692match = false;1693 }1694 1695start = ++i;1696continue;1697 }1698 1699match = true;1700i++;1701 } (match) {1704 list.add(str.substring(start, i));1705 }(String[]) list.toArray(new String[list.size()]);1708 }* 将字符串按指定字符分割。1712 *1713 * <p>1714 * 分隔符不会出现在目标数组中,连续的分隔符就被看作一个。如果字符串为<code>null</code>,则返回<code>null</code>。1715 * <pre>1716 * StringUtil.split(null, *)= null1717 * StringUtil.split(“”, *)= []1718 * StringUtil.split(“abc def”, null)= [“abc”, “def”]1719 * StringUtil.split(“abc def”, ” “)= [“abc”, “def”]1720 * StringUtil.split(“abc def”, ” “)= [“abc”, “def”]1721 * StringUtil.split(” ab: cd::ef “, “:”) = [“ab”, “cd”, “ef”]1722 * StringUtil.split(“abc.def”, “”)= [“abc.def”]1723 * </pre>1724 * </p>1725 * str 要分割的字符串 separatorChars 分隔符1728 * 分割后的字符串数组,如果原字符串为<code>null</code>,则返回<code>null</code> String[] split(String str, String separatorChars) {1732return split(str, separatorChars, -1);1733 }* 将字符串按指定字符分割。1737 *1738 * <p>1739 * 分隔符不会出现在目标数组中,连续的分隔符就被看作一个。如果字符串为<code>null</code>,则返回<code>null</code>。1740 * <pre>1741 * StringUtil.split(null, *, *)= null1742 * StringUtil.split(“”, *, *)= []1743 * StringUtil.split(“ab cd ef”, null, 0)= [“ab”, “cd”, “ef”]1744 * StringUtil.split(” ab cd ef “, null, 0) = [“ab”, “cd”, “ef”]1745 * StringUtil.split(“ab:cd::ef”, “:”, 0)= [“ab”, “cd”, “ef”]1746 * StringUtil.split(“ab:cd:ef”, “:”, 2)= [“ab”, “cdef”]1747 * StringUtil.split(“abc.def”, “”, 2)= [“abc.def”]1748 * </pre>1749 * </p>1750 * str 要分割的字符串 separatorChars 分隔符 max 返回的数组的最大个数,如果小于等于0,则表示无限制1754 * 分割后的字符串数组,如果原字符串为<code>null</code>,则返回<code>null</code>String[] split(String str, String separatorChars, int max) {1758if (str == null) {;1760 }length = str.length();(length == 0) {1765return ArrayUtil.EMPTY_STRING_ARRAY;1766 }1767 1768List list = new ArrayList();1769int sizePlus1 = 1;1770int i = 0;1771int start = 0;1772boolean match = false;(separatorChars == null) {(i < length) {1777if (Character.isWhitespace(str.charAt(i))) {1778if (match) {1779if (sizePlus1++ == max) {1780i = length;1781 }1782 1783 list.add(str.substring(start, i));1784match = false;1785 }1786 1787start = ++i;1788continue;1789 }1790 1791match = true;1792i++;1793 }1794} else if (separatorChars.length() == 1) {sep = separatorChars.charAt(0);(i < length) {1799if (str.charAt(i) == sep) {1800if (match) {1801if (sizePlus1++ == max) {1802i = length;1803 }1804 1805 list.add(str.substring(start, i));1806match = false;1807 }1808 1809start = ++i;1810continue;1811 }1812 1813match = true;1814i++;1815 }1816} else {(i < length) {1819if (separatorChars.indexOf(str.charAt(i)) >= 0) {1820if (match) {1821if (sizePlus1++ == max) {1822i = length;1823 }1824 1825 list.add(str.substring(start, i));1826match = false;1827 }1828 1829start = ++i;1830continue;1831 }1832 1833match = true;1834i++;1835 }1836 } (match) {1839 list.add(str.substring(start, i));1840 }(String[]) list.toArray(new String[list.size()]);1843 }* 将数组中的元素连接成一个字符串。1853 * <pre>1854 * StringUtil.join(null)= null1855 * StringUtil.join([])= “”1856 * StringUtil.join([null])= “”1857 * StringUtil.join([“a”, “b”, “c”]) = “abc”1858 * StringUtil.join([null, “”, “a”]) = “a”1859 * </pre>1860 * array 要连接的数组1862 * 连接后的字符串,如果原数组为<code>null</code>,则返回<code>null</code> String join(Object[] array) {1866return join(array, null);1867 }* 将数组中的元素连接成一个字符串。1871 * <pre>1872 * StringUtil.join(null, *)= null1873 * StringUtil.join([], *)= “”1874 * StringUtil.join([null], *)= “”1875 * StringUtil.join([“a”, “b”, “c”], ‘;’) = “a;b;c”1876 * StringUtil.join([“a”, “b”, “c”], null) = “abc”1877 * StringUtil.join([null, “”, “a”], ‘;’) = “;;a”1878 * </pre>1879 * array 要连接的数组 separator 分隔符1882 * 连接后的字符串,如果原数组为<code>null</code>,则返回<code>null</code>String join(Object[] array, char separator) {1886if (array == null) {;1888 }arraySize = array.length;1891int bufSize = (arraySize == 0) ? 0 : ((((array[0] == null) ? 16 : array[0].toString()1892.length()) + 1) * arraySize);1893StringBuffer buf = new StringBuffer(bufSize);(int i = 0; i < arraySize; i++) {1896if (i > 0) {1897 buf.append(separator);1898 }(array[i] != null) {1901 buf.append(array[i]);1902 }1903 } buf.toString();1906 }* 将数组中的元素连接成一个字符串。1910 * <pre>1911 * StringUtil.join(null, *)= null1912 * StringUtil.join([], *)= “”1913 * StringUtil.join([null], *)= “”1914 * StringUtil.join([“a”, “b”, “c”], “–“) = “a–b–c”1915 * StringUtil.join([“a”, “b”, “c”], null) = “abc”1916 * StringUtil.join([“a”, “b”, “c”], “”) = “abc”1917 * StringUtil.join([null, “”, “a”], ‘,’) = “,,a”1918 * </pre>1919 * array 要连接的数组 separator 分隔符1922 * 连接后的字符串,如果原数组为<code>null</code>,则返回<code>null</code> String join(Object[] array, String separator) {1926if (array == null) {;1928 }(separator == null) {1931separator = EMPTY_STRING;1932 }arraySize = array.length; ArraySize == 0: Len = 01937// ArraySize > 0: Len = NofStrings *(len(firstString) + len(separator))bufSize = (arraySize == 0) ? 0 : (arraySize * (((array[0] == null) ? 16 : array[0]1940.toString().length()) + ((separator != null) ? separator.length() : 0)));1941 1942StringBuffer buf = new StringBuffer(bufSize);(int i = 0; i < arraySize; i++) {1945if ((separator != null) && (i > 0)) {1946 buf.append(separator);1947 }(array[i] != null) {1950 buf.append(array[i]);1951 }1952 } buf.toString();1955 }* 将<code>Iterator</code>中的元素连接成一个字符串。1959 * <pre>1960 * StringUtil.join(null, *)= null1961 * StringUtil.join([], *)= “”1962 * StringUtil.join([null], *)= “”1963 * StringUtil.join([“a”, “b”, “c”], “–“) = “a–b–c”1964 * StringUtil.join([“a”, “b”, “c”], null) = “abc”1965 * StringUtil.join([“a”, “b”, “c”], “”) = “abc”1966 * StringUtil.join([null, “”, “a”], ‘,’) = “,,a”1967 * </pre>1968 * iterator 要连接的<code>Iterator</code> separator 分隔符1971 * 连接后的字符串,如果原数组为<code>null</code>,则返回<code>null</code>String join(Iterator iterator, char separator) {1975if (iterator == null) {;1977 } (iterator.hasNext()) {1982Object obj = iterator.next();(obj != null) {1985 buf.append(obj);1986 } (iterator.hasNext()) {1989 buf.append(separator);1990 }1991 } buf.toString();1994 }* 将<code>Iterator</code>中的元素连接成一个字符串。1998 * <pre>1999 * StringUtil.join(null, *)= null2000 * StringUtil.join([], *)= “”2001 * StringUtil.join([null], *)= “”2002 * StringUtil.join([“a”, “b”, “c”], “–“) = “a–b–c”2003 * StringUtil.join([“a”, “b”, “c”], null) = “abc”2004 * StringUtil.join([“a”, “b”, “c”], “”) = “abc”2005 * StringUtil.join([null, “”, “a”], ‘,’) = “,,a”2006 * </pre>2007 * iterator 要连接的<code>Iterator</code> separator 分隔符2010 * 连接后的字符串,如果原数组为<code>null</code>,则返回<code>null</code> String join(Iterator iterator, String separator) {2014if (iterator == null) {;2016 } (iterator.hasNext()) {2021Object obj = iterator.next();(obj != null) {2024 buf.append(obj);2025 }((separator != null) && iterator.hasNext()) {2028 buf.append(separator);2029 }2030 } buf.toString();2033 }* 在字符串中查找指定字符,并返回第一个匹配的索引值。如果字符串为<code>null</code>或未找到,则返回<code>-1</code>。2043 * <pre>2044 * StringUtil.indexOf(null, *)= -12045 * StringUtil.indexOf(“”, *)= -12046 * StringUtil.indexOf(“aabaabaa”, ‘a’) = 02047 * StringUtil.indexOf(“aabaabaa”, ‘b’) = 22048 * </pre>2049 * str 要扫描的字符串 searchChar 要查找的字符2052 * 第一个匹配的索引值。如果字符串为<code>null</code>或未找到,则返回<code>-1</code>indexOf(String str, char searchChar) {2056if ((str == null) || (str.length() == 0)) {2057return -1;2058 } str.indexOf(searchChar);2061 }* 在字符串中查找指定字符,并返回第一个匹配的索引值。如果字符串为<code>null</code>或未找到,则返回<code>-1</code>。2065 * <pre>2066 * StringUtil.indexOf(null, *, *)= -12067 * StringUtil.indexOf(“”, *, *)= -12068 * StringUtil.indexOf(“aabaabaa”, ‘b’, 0) = 22069 * StringUtil.indexOf(“aabaabaa”, ‘b’, 3) = 52070 * StringUtil.indexOf(“aabaabaa”, ‘b’, 9) = -12071 * StringUtil.indexOf(“aabaabaa”, ‘b’, -1) = 22072 * </pre>2073 * str 要扫描的字符串 searchChar 要查找的字符 startPos 开始搜索的索引值,如果小于0,则看作02077 * 第一个匹配的索引值。如果字符串为<code>null</code>或未找到,则返回<code>-1</code>indexOf(String str, char searchChar, int startPos) {2081if ((str == null) || (str.length() == 0)) {2082return -1;2083 } str.indexOf(searchChar, startPos);2086 }* 在字符串中查找指定字符串,并返回第一个匹配的索引值。如果字符串为<code>null</code>或未找到,则返回<code>-1</code>。2090 * <pre>2091 * StringUtil.indexOf(null, *)= -12092 * StringUtil.indexOf(*, null)= -12093 * StringUtil.indexOf(“”, “”)= 02094 * StringUtil.indexOf(“aabaabaa”, “a”) = 02095 * StringUtil.indexOf(“aabaabaa”, “b”) = 22096 * StringUtil.indexOf(“aabaabaa”, “ab”) = 12097 * StringUtil.indexOf(“aabaabaa”, “”) = 02098 * </pre>2099 * str 要扫描的字符串 searchStr 要查找的字符串2102 * 第一个匹配的索引值。如果字符串为<code>null</code>或未找到,则返回<code>-1</code> indexOf(String str, String searchStr) {2106if ((str == null) || (searchStr == null)) {2107return -1;2108 } str.indexOf(searchStr);2111 }* 在字符串中查找指定字符串,并返回第一个匹配的索引值。如果字符串为<code>null</code>或未找到,则返回<code>-1</code>。2115 * <pre>2116 * StringUtil.indexOf(null, *, *)= -12117 * StringUtil.indexOf(*, null, *)= -12118 * StringUtil.indexOf(“”, “”, 0)= 02119 * StringUtil.indexOf(“aabaabaa”, “a”, 0) = 02120 * StringUtil.indexOf(“aabaabaa”, “b”, 0) = 22121 * StringUtil.indexOf(“aabaabaa”, “ab”, 0) = 12122 * StringUtil.indexOf(“aabaabaa”, “b”, 3) = 52123 * StringUtil.indexOf(“aabaabaa”, “b”, 9) = -12124 * StringUtil.indexOf(“aabaabaa”, “b”, -1) = 22125 * StringUtil.indexOf(“aabaabaa”, “”, 2) = 22126 * StringUtil.indexOf(“abc”, “”, 9)= 32127 * </pre>2128 * str 要扫描的字符串 searchStr 要查找的字符串 startPos 开始搜索的索引值,如果小于0,则看作02132 * 第一个匹配的索引值。如果字符串为<code>null</code>或未找到,则返回<code>-1</code>indexOf(String str, String searchStr, int startPos) {2136if ((str == null) || (searchStr == null)) {2137return -1;2138 }((searchStr.length() == 0) && (startPos >= str.length())) {2142return str.length();2143 } str.indexOf(searchStr, startPos);2146 }* 在字符串中查找指定字符集合中的字符,并返回第一个匹配的起始索引。 如果字符串为<code>null</code>,则返回<code>-1</code>。2150 * 如果字符集合为<code>null</code>或空,也返回<code>-1</code>。2151 * <pre>2152 * StringUtil.indexOfAny(null, *)= -12153 * StringUtil.indexOfAny(“”, *)= -12154 * StringUtil.indexOfAny(*, null)= -12155 * StringUtil.indexOfAny(*, [])= -12156 * StringUtil.indexOfAny(“zzabyycdxx”,[‘z’,’a’]) = 02157 * StringUtil.indexOfAny(“zzabyycdxx”,[‘b’,’y’]) = 32158 * StringUtil.indexOfAny(“aba”, [‘z’])= -12159 * </pre>2160 * str 要扫描的字符串 searchChars 要搜索的字符集合2163 * 第一个匹配的索引值。如果字符串为<code>null</code>或未找到,则返回<code>-1</code>indexOfAny(String str, char[] searchChars) {2167if ((str == null) || (str.length() == 0) || (searchChars == null)2168|| (searchChars.length == 0)) {2169return -1;2170 }(int i = 0; i < str.length(); i++) {2173char ch = str.charAt(i);(int j = 0; j < searchChars.length; j++) {2176if (searchChars[j] == ch) {2177return i;2178 }2179 }2180 }-1;2183 }* 在字符串中查找指定字符集合中的字符,并返回第一个匹配的起始索引。 如果字符串为<code>null</code>,则返回<code>-1</code>。2187 * 如果字符集合为<code>null</code>或空,也返回<code>-1</code>。2188 * <pre>2189 * StringUtil.indexOfAny(null, *)= -12190 * StringUtil.indexOfAny(“”, *)= -12191 * StringUtil.indexOfAny(*, null)= -12192 * StringUtil.indexOfAny(*, “”)= -12193 * StringUtil.indexOfAny(“zzabyycdxx”, “za”) = 02194 * StringUtil.indexOfAny(“zzabyycdxx”, “by”) = 32195 * StringUtil.indexOfAny(“aba”,”z”)= -12196 * </pre>2197 * str 要扫描的字符串 searchChars 要搜索的字符集合2200 * 第一个匹配的索引值。如果字符串为<code>null</code>或未找到,则返回<code>-1</code> indexOfAny(String str, String searchChars) {2204if ((str == null) || (str.length() == 0) || (searchChars == null)2205|| (searchChars.length() == 0)) {2206return -1;2207 }(int i = 0; i < str.length(); i++) {2210char ch = str.charAt(i);(int j = 0; j < searchChars.length(); j++) {2213if (searchChars.charAt(j) == ch) {2214return i;2215 }2216 }2217 }-1;2220 }* 在字符串中查找指定字符串集合中的字符串,并返回第一个匹配的起始索引。 如果字符串为<code>null</code>,则返回<code>-1</code>。2224 * 如果字符串集合为<code>null</code>或空,也返回<code>-1</code>。2225 * 如果字符串集合包括<code>””</code>,并且字符串不为<code>null</code>,则返回<code>str.length()</code>2226 * <pre>2227 * StringUtil.indexOfAny(null, *)= -12228 * StringUtil.indexOfAny(*, null)= -12229 * StringUtil.indexOfAny(*, [])= -12230 * StringUtil.indexOfAny(“zzabyycdxx”, [“ab”,”cd”]) = 22231 * StringUtil.indexOfAny(“zzabyycdxx”, [“cd”,”ab”]) = 22232 * StringUtil.indexOfAny(“zzabyycdxx”, [“mn”,”op”]) = -12233 * StringUtil.indexOfAny(“zzabyycdxx”, [“zab”,”aby”]) = 12234 * StringUtil.indexOfAny(“zzabyycdxx”, [“”])= 02235 * StringUtil.indexOfAny(“”, [“”])= 02236 * StringUtil.indexOfAny(“”, [“a”])= -12237 * </pre>2238 * str 要扫描的字符串 searchStrs 要搜索的字符串集合2241 * 第一个匹配的索引值。如果字符串为<code>null</code>或未找到,则返回<code>-1</code> indexOfAny(String str, String[] searchStrs) {2245if ((str == null) || (searchStrs == null)) {2246return -1;2247 }sz = searchStrs.length;ret = Integer.MAX_VALUE;tmp = 0;(int i = 0; i < sz; i++) {2257String search = searchStrs[i];(search == null) {2260continue;2261 }2262 2263tmp = str.indexOf(search);(tmp == -1) {2266continue;2267 }(tmp < ret) {2270ret = tmp;2271 }2272 }(ret == Integer.MAX_VALUE) ? (-1) : ret;2275 }* 在字符串中查找不在指定字符集合中的字符,并返回第一个匹配的起始索引。 如果字符串为<code>null</code>,则返回<code>-1</code>。2279 * 如果字符集合为<code>null</code>或空,也返回<code>-1</code>。2280 * <pre>2281 * StringUtil.indexOfAnyBut(null, *)= -12282 * StringUtil.indexOfAnyBut(“”, *)= -12283 * StringUtil.indexOfAnyBut(*, null)= -12284 * StringUtil.indexOfAnyBut(*, [])= -12285 * StringUtil.indexOfAnyBut(“zzabyycdxx”,’za’) = 32286 * StringUtil.indexOfAnyBut(“zzabyycdxx”, ‘by’) = 02287 * StringUtil.indexOfAnyBut(“aba”, ‘ab’)= -12288 * </pre>2289 * str 要扫描的字符串 searchChars 要搜索的字符集合2292 * 第一个匹配的索引值。如果字符串为<code>null</code>或未找到,则返回<code>-1</code>indexOfAnyBut(String str, char[] searchChars) {2296if ((str == null) || (str.length() == 0) || (searchChars == null)2297|| (searchChars.length == 0)) {2298return -1;2299 }2300 2301outer: for (int i = 0; i < str.length(); i++) {2302char ch = str.charAt(i);(int j = 0; j < searchChars.length; j++) {2305if (searchChars[j] == ch) {2306continue outer;2307 }2308 } i;2311 }-1;2314 }* 在字符串中查找不在指定字符集合中的字符,并返回第一个匹配的起始索引。 如果字符串为<code>null</code>,则返回<code>-1</code>。2318 * 如果字符集合为<code>null</code>或空,也返回<code>-1</code>。2319 * <pre>2320 * StringUtil.indexOfAnyBut(null, *)= -12321 * StringUtil.indexOfAnyBut(“”, *)= -12322 * StringUtil.indexOfAnyBut(*, null)= -12323 * StringUtil.indexOfAnyBut(*, “”)= -12324 * StringUtil.indexOfAnyBut(“zzabyycdxx”, “za”) = 32325 * StringUtil.indexOfAnyBut(“zzabyycdxx”, “by”) = 02326 * StringUtil.indexOfAnyBut(“aba”,”ab”)= -12327 * </pre>2328 * str 要扫描的字符串 searchChars 要搜索的字符集合2331 * 第一个匹配的索引值。如果字符串为<code>null</code>或未找到,则返回<code>-1</code> indexOfAnyBut(String str, String searchChars) {2335if ((str == null) || (str.length() == 0) || (searchChars == null)2336|| (searchChars.length() == 0)) {2337return -1;2338 }(int i = 0; i < str.length(); i++) {2341if (searchChars.indexOf(str.charAt(i)) < 0) {2342return i;2343 }2344 }-1;2347 }* 从字符串尾部开始查找指定字符,并返回第一个匹配的索引值。如果字符串为<code>null</code>或未找到,则返回<code>-1</code>。2351 * <pre>2352 * StringUtil.lastIndexOf(null, *)= -12353 * StringUtil.lastIndexOf(“”, *)= -12354 * StringUtil.lastIndexOf(“aabaabaa”, ‘a’) = 72355 * StringUtil.lastIndexOf(“aabaabaa”, ‘b’) = 52356 * </pre>2357 * str 要扫描的字符串 searchChar 要查找的字符2360 * 第一个匹配的索引值。如果字符串为<code>null</code>或未找到,则返回<code>-1</code>lastIndexOf(String str, char searchChar) {2364if ((str == null) || (str.length() == 0)) {2365return -1;2366 } str.lastIndexOf(searchChar);2369 }* 从字符串尾部开始查找指定字符,并返回第一个匹配的索引值。如果字符串为<code>null</code>或未找到,则返回<code>-1</code>。2373 * <pre>2374 * StringUtil.lastIndexOf(null, *, *)= -12375 * StringUtil.lastIndexOf(“”, *, *)= -12376 * StringUtil.lastIndexOf(“aabaabaa”, ‘b’, 8) = 52377 * StringUtil.lastIndexOf(“aabaabaa”, ‘b’, 4) = 22378 * StringUtil.lastIndexOf(“aabaabaa”, ‘b’, 0) = -12379 * StringUtil.lastIndexOf(“aabaabaa”, ‘b’, 9) = 52380 * StringUtil.lastIndexOf(“aabaabaa”, ‘b’, -1) = -12381 * StringUtil.lastIndexOf(“aabaabaa”, ‘a’, 0) = 02382 * </pre>2383 * str 要扫描的字符串 searchChar 要查找的字符 startPos 从指定索引开始向前搜索2387 * 第一个匹配的索引值。如果字符串为<code>null</code>或未找到,则返回<code>-1</code>lastIndexOf(String str, char searchChar, int startPos) {2391if ((str == null) || (str.length() == 0)) {2392return -1;2393 } str.lastIndexOf(searchChar, startPos);2396 }* 从字符串尾部开始查找指定字符串,并返回第一个匹配的索引值。如果字符串为<code>null</code>或未找到,则返回<code>-1</code>。2400 * <pre>2401 * StringUtil.lastIndexOf(null, *)= -12402 * StringUtil.lastIndexOf(“”, *)= -12403 * StringUtil.lastIndexOf(“aabaabaa”, ‘a’) = 72404 * StringUtil.lastIndexOf(“aabaabaa”, ‘b’) = 52405 * </pre>2406 * str 要扫描的字符串 searchStr 要查找的字符串2409 * 第一个匹配的索引值。如果字符串为<code>null</code>或未找到,则返回<code>-1</code> lastIndexOf(String str, String searchStr) {2413if ((str == null) || (searchStr == null)) {2414return -1;2415 } str.lastIndexOf(searchStr);2418 }* 从字符串尾部开始查找指定字符串,并返回第一个匹配的索引值。如果字符串为<code>null</code>或未找到,则返回<code>-1</code>。2422 * <pre>2423 * StringUtil.lastIndexOf(null, *, *)= -12424 * StringUtil.lastIndexOf(*, null, *)= -12425 * StringUtil.lastIndexOf(“aabaabaa”, “a”, 8) = 72426 * StringUtil.lastIndexOf(“aabaabaa”, “b”, 8) = 52427 * StringUtil.lastIndexOf(“aabaabaa”, “ab”, 8) = 42428 * StringUtil.lastIndexOf(“aabaabaa”, “b”, 9) = 52429 * StringUtil.lastIndexOf(“aabaabaa”, “b”, -1) = -12430 * StringUtil.lastIndexOf(“aabaabaa”, “a”, 0) = 02431 * StringUtil.lastIndexOf(“aabaabaa”, “b”, 0) = -12432 * </pre>2433 * str 要扫描的字符串 searchStr 要查找的字符串 startPos 从指定索引开始向前搜索2437 * 第一个匹配的索引值。如果字符串为<code>null</code>或未找到,则返回<code>-1</code>lastIndexOf(String str, String searchStr, int startPos) {2441if ((str == null) || (searchStr == null)) {2442return -1;2443 } str.lastIndexOf(searchStr, startPos);2446 }* 从字符串尾部开始查找指定字符串集合中的字符串,并返回第一个匹配的起始索引。 如果字符串为<code>null</code>,则返回<code>-1</code>。2450 * 如果字符串集合为<code>null</code>或空,也返回<code>-1</code>。2451 * 如果字符串集合包括<code>””</code>,并且字符串不为<code>null</code>,则返回<code>str.length()</code>2452 * <pre>2453 * StringUtil.lastIndexOfAny(null, *)= -12454 * StringUtil.lastIndexOfAny(*, null)= -12455 * StringUtil.lastIndexOfAny(*, [])= -12456 * StringUtil.lastIndexOfAny(*, [null])= -12457 * StringUtil.lastIndexOfAny(“zzabyycdxx”, [“ab”,”cd”]) = 62458 * StringUtil.lastIndexOfAny(“zzabyycdxx”, [“cd”,”ab”]) = 62459 * StringUtil.lastIndexOfAny(“zzabyycdxx”, [“mn”,”op”]) = -12460 * StringUtil.lastIndexOfAny(“zzabyycdxx”, [“mn”,”op”]) = -12461 * StringUtil.lastIndexOfAny(“zzabyycdxx”, [“mn”,””]) = 102462 * </pre>2463 * str 要扫描的字符串 searchStrs 要搜索的字符串集合2466 * 第一个匹配的索引值。如果字符串为<code>null</code>或未找到,则返回<code>-1</code> lastIndexOfAny(String str, String[] searchStrs) {2470if ((str == null) || (searchStrs == null)) {2471return -1;2472 }searchStrsLength = searchStrs.length;2475int index = -1;2476int tmp = 0;(int i = 0; i < searchStrsLength; i++) {2479String search = searchStrs[i];(search == null) {2482continue;2483 }2484 2485tmp = str.lastIndexOf(search);(tmp > index) {2488index = tmp;2489 }2490 } index;2493 }* 检查字符串中是否包含指定的字符。如果字符串为<code>null</code>,将返回<code>false</code>。2497 * <pre>2498 * StringUtil.contains(null, *) = false2499 * StringUtil.contains(“”, *)= false2500 * StringUtil.contains(“abc”, ‘a’) = true2501 * StringUtil.contains(“abc”, ‘z’) = false2502 * </pre>2503 * str 要扫描的字符串 searchChar 要查找的字符2506 * 如果找到,则返回<code>true</code>contains(String str, char searchChar) {2510if ((str == null) || (str.length() == 0)) {;2512 }str.indexOf(searchChar) >= 0;2515 }* 检查字符串中是否包含指定的字符串。如果字符串为<code>null</code>,将返回<code>false</code>。2519 * <pre>2520 * StringUtil.contains(null, *)= false2521 * StringUtil.contains(*, null)= false2522 * StringUtil.contains(“”, “”)= true2523 * StringUtil.contains(“abc”, “”) = true2524 * StringUtil.contains(“abc”, “a”) = true2525 * StringUtil.contains(“abc”, “z”) = false2526 * </pre>2527 * str 要扫描的字符串 searchStr 要查找的字符串2530 * 如果找到,则返回<code>true</code> contains(String str, String searchStr) {2534if ((str == null) || (searchStr == null)) {;2536 }str.indexOf(searchStr) >= 0;2539 }* 检查字符串是是否只包含指定字符集合中的字符。2543 *2544 * <p>2545 * 如果字符串为<code>null</code>,则返回<code>false</code>。2546 * 如果字符集合为<code>null</code>则返回<code>false</code>。 但是空字符串永远返回<code>true</code>.2547 * </p>2548 * <pre>2549 * StringUtil.containsOnly(null, *)= false2550 * StringUtil.containsOnly(*, null)= false2551 * StringUtil.containsOnly(“”, *)= true2552 * StringUtil.containsOnly(“ab”, ”)= false2553 * StringUtil.containsOnly(“abab”, ‘abc’) = true2554 * StringUtil.containsOnly(“ab1”, ‘abc’) = false2555 * StringUtil.containsOnly(“abz”, ‘abc’) = false2556 * </pre>2557 * str 要扫描的字符串 valid 要查找的字符串2560 * 如果找到,则返回<code>true</code>containsOnly(String str, char[] valid) {2564if ((valid == null) || (str == null)) {;2566 }(str.length() == 0) {;2570 }(valid.length == 0) {;2574 }indexOfAnyBut(str, valid) == -1;2577 }* 检查字符串是是否只包含指定字符集合中的字符。2581 *2582 * <p>2583 * 如果字符串为<code>null</code>,则返回<code>false</code>。2584 * 如果字符集合为<code>null</code>则返回<code>false</code>。 但是空字符串永远返回<code>true</code>.2585 * </p>2586 * <pre>2587 * StringUtil.containsOnly(null, *)= false2588 * StringUtil.containsOnly(*, null)= false2589 * StringUtil.containsOnly(“”, *)= true2590 * StringUtil.containsOnly(“ab”, “”)= false2591 * StringUtil.containsOnly(“abab”, “abc”) = true2592 * StringUtil.containsOnly(“ab1”, “abc”) = false2593 * StringUtil.containsOnly(“abz”, “abc”) = false2594 * </pre>2595 * str 要扫描的字符串 valid 要查找的字符串2598 * 如果找到,则返回<code>true</code> containsOnly(String str, String valid) {2602if ((str == null) || (valid == null)) {;2604 } containsOnly(str, valid.toCharArray());2607 }* 检查字符串是是否不包含指定字符集合中的字符。2611 *2612 * <p>2613 * 如果字符串为<code>null</code>,则返回<code>false</code>。 如果字符集合为<code>null</code>则返回<code>true</code>。2614 * 但是空字符串永远返回<code>true</code>.2615 * </p>2616 * <pre>2617 * StringUtil.containsNone(null, *)= true2618 * StringUtil.containsNone(*, null)= true2619 * StringUtil.containsNone(“”, *)= true2620 * StringUtil.containsNone(“ab”, ”)= true2621 * StringUtil.containsNone(“abab”, ‘xyz’) = true2622 * StringUtil.containsNone(“ab1”, ‘xyz’) = true2623 * StringUtil.containsNone(“abz”, ‘xyz’) = false2624 * </pre>2625 * str 要扫描的字符串 invalid 要查找的字符串2628 * 如果找到,则返回<code>true</code>containsNone(String str, char[] invalid) {2632if ((str == null) || (invalid == null)) {;2634 }strSize = str.length();2637int validSize = invalid.length;(int i = 0; i < strSize; i++) {2640char ch = str.charAt(i);(int j = 0; j < validSize; j++) {2643if (invalid[j] == ch) {;2645 }2646 }2647 };2650 }* 检查字符串是是否不包含指定字符集合中的字符。2654 *2655 * <p>2656 * 如果字符串为<code>null</code>,则返回<code>false</code>。 如果字符集合为<code>null</code>则返回<code>true</code>。2657 * 但是空字符串永远返回<code>true</code>.2658 * </p>2659 * <pre>2660 * StringUtil.containsNone(null, *)= true2661 * StringUtil.containsNone(*, null)= true2662 * StringUtil.containsNone(“”, *)= true2663 * StringUtil.containsNone(“ab”, “”)= true2664 * StringUtil.containsNone(“abab”, “xyz”) = true2665 * StringUtil.containsNone(“ab1”, “xyz”) = true2666 * StringUtil.containsNone(“abz”, “xyz”) = false2667 * </pre>2668 * str 要扫描的字符串 invalidChars 要查找的字符串2671 * 如果找到,则返回<code>true</code> containsNone(String str, String invalidChars) {2675if ((str == null) || (invalidChars == null)) {;2677 } containsNone(str, invalidChars.toCharArray());2680 }* 取得指定子串在字符串中出现的次数。2684 *2685 * <p>2686 * 如果字符串为<code>null</code>或空,则返回<code>0</code>。2687 * <pre>2688 * StringUtil.countMatches(null, *)= 02689 * StringUtil.countMatches(“”, *)= 02690 * StringUtil.countMatches(“abba”, null) = 02691 * StringUtil.countMatches(“abba”, “”) = 02692 * StringUtil.countMatches(“abba”, “a”) = 22693 * StringUtil.countMatches(“abba”, “ab”) = 12694 * StringUtil.countMatches(“abba”, “xxx”) = 02695 * </pre>2696 * </p>2697 * str 要扫描的字符串 subStr 子字符串2700 * 子串在字符串中出现的次数,如果字符串为<code>null</code>或空,则返回<code>0</code> countMatches(String str, String subStr) {2704if ((str == null) || (str.length() == 0) || (subStr == null) || (subStr.length() == 0)) {2705return 0;2706 }count = 0;2709int index = 0;((index = str.indexOf(subStr, index)) != -1) {2712count++;2713index += subStr.length();2714 } count;2717 }* 取指定字符串的子串。2725 *2726 * <p>2727 * 负的索引代表从尾部开始计算。如果字符串为<code>null</code>,则返回<code>null</code>。2728 * <pre>2729 * StringUtil.substring(null, *) = null2730 * StringUtil.substring(“”, *)= “”2731 * StringUtil.substring(“abc”, 0) = “abc”2732 * StringUtil.substring(“abc”, 2) = “c”2733 * StringUtil.substring(“abc”, 4) = “”2734 * StringUtil.substring(“abc”, -2) = “bc”2735 * StringUtil.substring(“abc”, -4) = “abc”2736 * </pre>2737 * </p>2738 * str 字符串 start 起始索引,如果为负数,表示从尾部查找2741 * 子串,如果原始串为<code>null</code>,则返回<code>null</code>String substring(String str, int start) {2745if (str == null) {;2747 }(start < 0) {2750start = str.length() + start;2751 }(start < 0) {2754start = 0;2755 }(start > str.length()) {2758return EMPTY_STRING;2759 } str.substring(start);2762 }* 取指定字符串的子串。2766 *2767 * <p>2768 * 负的索引代表从尾部开始计算。如果字符串为<code>null</code>,则返回<code>null</code>。2769 * <pre>2770 * StringUtil.substring(null, *, *) = null2771 * StringUtil.substring(“”, * , *) = “”;2772 * StringUtil.substring(“abc”, 0, 2) = “ab”2773 * StringUtil.substring(“abc”, 2, 0) = “”2774 * StringUtil.substring(“abc”, 2, 4) = “c”2775 * StringUtil.substring(“abc”, 4, 6) = “”2776 * StringUtil.substring(“abc”, 2, 2) = “”2777 * StringUtil.substring(“abc”, -2, -1) = “b”2778 * StringUtil.substring(“abc”, -4, 2) = “ab”2779 * </pre>2780 * </p>2781 * str 字符串 start 起始索引,如果为负数,表示从尾部计算 end 结束索引(不含),如果为负数,表示从尾部计算2785 * 子串,如果原始串为<code>null</code>,则返回<code>null</code>String substring(String str, int start, int end) {2789if (str == null) {;2791 }(end < 0) {2794end = str.length() + end;2795 }(start < 0) {2798start = str.length() + start;2799 }(end > str.length()) {2802end = str.length();2803 }(start > end) {2806return EMPTY_STRING;2807 }(start < 0) {2810start = 0;2811 }(end < 0) {2814end = 0;2815 } str.substring(start, end);2818 }* 取得长度为指定字符数的最左边的子串。2822 * <pre>2823 * StringUtil.left(null, *) = null2824 * StringUtil.left(*, -ve)= “”2825 * StringUtil.left(“”, *)= “”2826 * StringUtil.left(“abc”, 0) = “”2827 * StringUtil.left(“abc”, 2) = “ab”2828 * StringUtil.left(“abc”, 4) = “abc”2829 * </pre>2830 * str 字符串 len 最左子串的长度2833 * 子串,如果原始字串为<code>null</code>,则返回<code>null</code>String left(String str, int len) {2837if (str == null) {;2839 }(len < 0) {2842return EMPTY_STRING;2843 }(str.length() <= len) {2846return str;2847} else {2848return str.substring(0, len);2849 }2850 }* 取得长度为指定字符数的最右边的子串。2854 * <pre>2855 * StringUtil.right(null, *) = null2856 * StringUtil.right(*, -ve)= “”2857 * StringUtil.right(“”, *)= “”2858 * StringUtil.right(“abc”, 0) = “”2859 * StringUtil.right(“abc”, 2) = “bc”2860 * StringUtil.right(“abc”, 4) = “abc”2861 * </pre>2862 * str 字符串 len 最右子串的长度2865 * 子串,如果原始字串为<code>null</code>,则返回<code>null</code>String right(String str, int len) {2869if (str == null) {;2871 }(len < 0) {2874return EMPTY_STRING;2875 }(str.length() <= len) {2878return str;2879} else {2880return str.substring(str.length() – len);2881 }2882 }* 取得从指定索引开始计算的、长度为指定字符数的子串。2886 * <pre>2887 * StringUtil.mid(null, *, *) = null2888 * StringUtil.mid(*, *, -ve)= “”2889 * StringUtil.mid(“”, 0, *)= “”2890 * StringUtil.mid(“abc”, 0, 2) = “ab”2891 * StringUtil.mid(“abc”, 0, 4) = “abc”2892 * StringUtil.mid(“abc”, 2, 4) = “c”2893 * StringUtil.mid(“abc”, 4, 2) = “”2894 * StringUtil.mid(“abc”, -2, 2) = “ab”2895 * </pre>2896 * str 字符串 pos 起始索引,如果为负数,则看作<code>0</code> len 子串的长度,如果为负数,则看作长度为<code>0</code>2900 * 子串,如果原始字串为<code>null</code>,则返回<code>null</code>String mid(String str, int pos, int len) {2904if (str == null) {;2906 }((len < 0) || (pos > str.length())) {2909return EMPTY_STRING;2910 }(pos < 0) {2913pos = 0;2914 }(str.length() <= (pos + len)) {2917return str.substring(pos);2918} else {2919return str.substring(pos, pos + len);2920 }2921 }* 取得第一个出现的分隔子串之前的子串。2929 *2930 * <p>2931 * 如果字符串为<code>null</code>,则返回<code>null</code>。 如果分隔子串为<code>null</code>或未找到该子串,则返回原字符串。2932 * <pre>2933 * StringUtil.substringBefore(null, *)= null2934 * StringUtil.substringBefore(“”, *)= “”2935 * StringUtil.substringBefore(“abc”, “a”) = “”2936 * StringUtil.substringBefore(“abcba”, “b”) = “a”2937 * StringUtil.substringBefore(“abc”, “c”) = “ab”2938 * StringUtil.substringBefore(“abc”, “d”) = “abc”2939 * StringUtil.substringBefore(“abc”, “”) = “”2940 * StringUtil.substringBefore(“abc”, null) = “abc”2941 * </pre>2942 * </p>2943 * str 字符串 separator 要搜索的分隔子串2946 * 子串,如果原始串为<code>null</code>,则返回<code>null</code> String substringBefore(String str, String separator) {2950if ((str == null) || (separator == null) || (str.length() == 0)) {2951return str;2952 }(separator.length() == 0) {2955return EMPTY_STRING;2956 }pos = str.indexOf(separator);(pos == -1) {2961return str;2962 }str.substring(0, pos);2965 }* 取得第一个出现的分隔子串之后的子串。2969 *2970 * <p>2971 * 如果字符串为<code>null</code>,则返回<code>null</code>。 如果分隔子串为<code>null</code>或未找到该子串,则返回原字符串。2972 * <pre>2973 * StringUtil.substringAfter(null, *)= null2974 * StringUtil.substringAfter(“”, *)= “”2975 * StringUtil.substringAfter(*, null)= “”2976 * StringUtil.substringAfter(“abc”, “a”) = “bc”2977 * StringUtil.substringAfter(“abcba”, “b”) = “cba”2978 * StringUtil.substringAfter(“abc”, “c”) = “”2979 * StringUtil.substringAfter(“abc”, “d”) = “”2980 * StringUtil.substringAfter(“abc”, “”) = “abc”2981 * </pre>2982 * </p>2983 * str 字符串 separator 要搜索的分隔子串2986 * 子串,如果原始串为<code>null</code>,则返回<code>null</code> String substringAfter(String str, String separator) {2990if ((str == null) || (str.length() == 0)) {2991return str;2992 }(separator == null) {2995return EMPTY_STRING;2996 }pos = str.indexOf(separator);(pos == -1) {3001return EMPTY_STRING;3002 }str.substring(pos + separator.length());3005 }* 取得最后一个的分隔子串之前的子串。3009 *3010 * <p>3011 * 如果字符串为<code>null</code>,则返回<code>null</code>。 如果分隔子串为<code>null</code>或未找到该子串,则返回原字符串。3012 * <pre>3013 * StringUtil.substringBeforeLast(null, *)= null3014 * StringUtil.substringBeforeLast(“”, *)= “”3015 * StringUtil.substringBeforeLast(“abcba”, “b”) = “abc”3016 * StringUtil.substringBeforeLast(“abc”, “c”) = “ab”3017 * StringUtil.substringBeforeLast(“a”, “a”)= “”3018 * StringUtil.substringBeforeLast(“a”, “z”)= “a”3019 * StringUtil.substringBeforeLast(“a”, null) = “a”3020 * StringUtil.substringBeforeLast(“a”, “”)= “a”3021 * </pre>3022 * </p>3023 * str 字符串 separator 要搜索的分隔子串3026 * 子串,如果原始串为<code>null</code>,则返回<code>null</code> String substringBeforeLast(String str, String separator) {3030if ((str == null) || (separator == null) || (str.length() == 0)3031|| (separator.length() == 0)) {3032return str;3033 }pos = str.lastIndexOf(separator);(pos == -1) {3038return str;3039 }str.substring(0, pos);3042 }* 取得最后一个的分隔子串之后的子串。3046 *3047 * <p>3048 * 如果字符串为<code>null</code>,则返回<code>null</code>。 如果分隔子串为<code>null</code>或未找到该子串,则返回原字符串。3049 * <pre>3050 * StringUtil.substringAfterLast(null, *)= null3051 * StringUtil.substringAfterLast(“”, *)= “”3052 * StringUtil.substringAfterLast(*, “”)= “”3053 * StringUtil.substringAfterLast(*, null)= “”3054 * StringUtil.substringAfterLast(“abc”, “a”) = “bc”3055 * StringUtil.substringAfterLast(“abcba”, “b”) = “a”3056 * StringUtil.substringAfterLast(“abc”, “c”) = “”3057 * StringUtil.substringAfterLast(“a”, “a”)= “”3058 * StringUtil.substringAfterLast(“a”, “z”)= “”3059 * </pre>3060 * </p>3061 * str 字符串 separator 要搜索的分隔子串3064 * 子串,如果原始串为<code>null</code>,则返回<code>null</code> String substringAfterLast(String str, String separator) {3068if ((str == null) || (str.length() == 0)) {3069return str;3070 }((separator == null) || (separator.length() == 0)) {3073return EMPTY_STRING;3074 }pos = str.lastIndexOf(separator);((pos == -1) || (pos == (str.length() – separator.length()))) {3079return EMPTY_STRING;3080 }str.substring(pos + separator.length());3083 }* 取得指定分隔符的前两次出现之间的子串。3087 *3088 * <p>3089 * 如果字符串为<code>null</code>,则返回<code>null</code>。 如果分隔子串为<code>null</code>,则返回<code>null</code>。3090 * <pre>3091 * StringUtil.substringBetween(null, *)= null3092 * StringUtil.substringBetween(“”, “”)= “”3093 * StringUtil.substringBetween(“”, “tag”)= null3094 * StringUtil.substringBetween(“tagabctag”, null) = null3095 * StringUtil.substringBetween(“tagabctag”, “”) = “”3096 * StringUtil.substringBetween(“tagabctag”, “tag”) = “abc”3097 * </pre>3098 * </p>3099 * str 字符串 tag 要搜索的分隔子串3102 * 子串,如果原始串为<code>null</code>或未找到分隔子串,则返回<code>null</code> String substringBetween(String str, String tag) {3106return substringBetween(str, tag, tag, 0);3107 }* 取得两个分隔符之间的子串。3111 *3112 * <p>3113 * 如果字符串为<code>null</code>,则返回<code>null</code>。 如果分隔子串为<code>null</code>,则返回<code>null</code>。3114 * <pre>3115 * StringUtil.substringBetween(null, *, *)= null3116 * StringUtil.substringBetween(“”, “”, “”)= “”3117 * StringUtil.substringBetween(“”, “”, “tag”)= null3118 * StringUtil.substringBetween(“”, “tag”, “tag”) = null3119 * StringUtil.substringBetween(“yabcz”, null, null) = null3120 * StringUtil.substringBetween(“yabcz”, “”, “”)= “”3121 * StringUtil.substringBetween(“yabcz”, “y”, “z”) = “abc”3122 * StringUtil.substringBetween(“yabczyabcz”, “y”, “z”) = “abc”3123 * </pre>3124 * </p>3125 * str 字符串 open 要搜索的分隔子串1 close 要搜索的分隔子串23129 * 子串,如果原始串为<code>null</code>或未找到分隔子串,则返回<code>null</code> String substringBetween(String str, String open, String close) {3133return substringBetween(str, open, close, 0);3134 }* 取得两个分隔符之间的子串。3138 *3139 * <p>3140 * 如果字符串为<code>null</code>,则返回<code>null</code>。 如果分隔子串为<code>null</code>,则返回<code>null</code>。3141 * <pre>3142 * StringUtil.substringBetween(null, *, *)= null3143 * StringUtil.substringBetween(“”, “”, “”)= “”3144 * StringUtil.substringBetween(“”, “”, “tag”)= null3145 * StringUtil.substringBetween(“”, “tag”, “tag”) = null3146 * StringUtil.substringBetween(“yabcz”, null, null) = null3147 * StringUtil.substringBetween(“yabcz”, “”, “”)= “”3148 * StringUtil.substringBetween(“yabcz”, “y”, “z”) = “abc”3149 * StringUtil.substringBetween(“yabczyabcz”, “y”, “z”) = “abc”3150 * </pre>3151 * </p>3152 * str 字符串 open 要搜索的分隔子串1 close 要搜索的分隔子串2 fromIndex 从指定index处搜索3157 * 子串,如果原始串为<code>null</code>或未找到分隔子串,则返回<code>null</code>String substringBetween(String str, String open, String close, int fromIndex) {3161if ((str == null) || (open == null) || (close == null)) {;3163 }start = str.indexOf(open, fromIndex);(start != -1) {3168int end = str.indexOf(close, start + open.length());(end != -1) {3171return str.substring(start + open.length(), end);3172 }3173 };3176 }* 删除所有在<code>Character.isWhitespace(char)</code>中所定义的空白。3184 * <pre>3185 * StringUtil.deleteWhitespace(null)= null3186 * StringUtil.deleteWhitespace(“”)= “”3187 * StringUtil.deleteWhitespace(“abc”)= “abc”3188 * StringUtil.deleteWhitespace(” ab c “) = “abc”3189 * </pre>3190 * str 要处理的字符串3192 * 去空白后的字符串,如果原始字符串为<code>null</code>,则返回<code>null</code> String deleteWhitespace(String str) {3196if (str == null) {;3198 }sz = str.length();3201StringBuffer buffer = new StringBuffer(sz);(int i = 0; i < sz; i++) {3204if (!Character.isWhitespace(str.charAt(i))) {3205 buffer.append(str.charAt(i));3206 }3207 } buffer.toString();3210 }* 替换指定的子串,只替换第一个出现的子串。3218 *3219 * <p>3220 * 如果字符串为<code>null</code>则返回<code>null</code>,如果指定子串为<code>null</code>,则返回原字符串。3221 * <pre>3222 * StringUtil.replaceOnce(null, *, *)= null3223 * StringUtil.replaceOnce(“”, *, *)= “”3224 * StringUtil.replaceOnce(“aba”, null, null) = “aba”3225 * StringUtil.replaceOnce(“aba”, null, null) = “aba”3226 * StringUtil.replaceOnce(“aba”, “a”, null) = “aba”3227 * StringUtil.replaceOnce(“aba”, “a”, “”) = “ba”3228 * StringUtil.replaceOnce(“aba”, “a”, “z”) = “zba”3229 * </pre>3230 * </p>3231 * text 要扫描的字符串 repl 要搜索的子串 with 替换字符串3235 * 被替换后的字符串,如果原始字符串为<code>null</code>,则返回<code>null</code> String replaceOnce(String text, String repl, String with) {3239return replace(text, repl, with, 1);3240 }* 替换指定的子串,替换所有出现的子串。3244 *3245 * <p>3246 * 如果字符串为<code>null</code>则返回<code>null</code>,如果指定子串为<code>null</code>,则返回原字符串。3247 * <pre>3248 * StringUtil.replace(null, *, *)= null3249 * StringUtil.replace(“”, *, *)= “”3250 * StringUtil.replace(“aba”, null, null) = “aba”3251 * StringUtil.replace(“aba”, null, null) = “aba”3252 * StringUtil.replace(“aba”, “a”, null) = “aba”3253 * StringUtil.replace(“aba”, “a”, “”) = “b”3254 * StringUtil.replace(“aba”, “a”, “z”) = “zbz”3255 * </pre>3256 * </p>3257 * text 要扫描的字符串 repl 要搜索的子串 with 替换字符串3261 * 被替换后的字符串,如果原始字符串为<code>null</code>,则返回<code>null</code> String replace(String text, String repl, String with) {3265return replace(text, repl, with, -1);3266 }* 替换指定的子串,替换指定的次数。3270 *3271 * <p>3272 * 如果字符串为<code>null</code>则返回<code>null</code>,如果指定子串为<code>null</code>,则返回原字符串。3273 * <pre>3274 * StringUtil.replace(null, *, *, *)= null3275 * StringUtil.replace(“”, *, *, *)= “”3276 * StringUtil.replace(“abaa”, null, null, 1) = “abaa”3277 * StringUtil.replace(“abaa”, null, null, 1) = “abaa”3278 * StringUtil.replace(“abaa”, “a”, null, 1) = “abaa”3279 * StringUtil.replace(“abaa”, “a”, “”, 1) = “baa”3280 * StringUtil.replace(“abaa”, “a”, “z”, 0) = “abaa”3281 * StringUtil.replace(“abaa”, “a”, “z”, 1) = “zbaa”3282 * StringUtil.replace(“abaa”, “a”, “z”, 2) = “zbza”3283 * StringUtil.replace(“abaa”, “a”, “z”, -1) = “zbzz”3284 * </pre>3285 * </p>3286 * text 要扫描的字符串 repl 要搜索的子串 with 替换字符串 max maximum number of values to replace, or <code>-1</code> if no maximum3291 * 被替换后的字符串,如果原始字符串为<code>null</code>,则返回<code>null</code>String replace(String text, String repl, String with, int max) {3295if ((text == null) || (repl == null) || (with == null) || (repl.length() == 0)3296|| (max == 0)) {3297return text;3298 }3299 3300StringBuffer buf = new StringBuffer(text.length());3301int start = 0;3302int end = 0;((end = text.indexOf(repl, start)) != -1) {3305 buf.append(text.substring(start, end)).append(with);3306start = end + repl.length();(–max == 0) {3309break;3310 }3311 }3312 3313 buf.append(text.substring(start));3314return buf.toString();3315 }* 将字符串中所有指定的字符,替换成另一个。3319 *3320 * <p>3321 * 如果字符串为<code>null</code>则返回<code>null</code>。3322 * <pre>3323 * StringUtil.replaceChars(null, *, *)= null3324 * StringUtil.replaceChars(“”, *, *)= “”3325 * StringUtil.replaceChars(“abcba”, ‘b’, ‘y’) = “aycya”3326 * StringUtil.replaceChars(“abcba”, ‘z’, ‘y’) = “abcba”3327 * </pre>3328 * </p>3329 * str 要扫描的字符串 searchChar 要搜索的字符 replaceChar 替换字符3333 * 被替换后的字符串,如果原始字符串为<code>null</code>,则返回<code>null</code>String replaceChars(String str, char searchChar, char replaceChar) {3337if (str == null) {;3339 } str.replace(searchChar, replaceChar);3342 }* 将字符串中所有指定的字符,替换成另一个。3346 *3347 * <p>3348 * 如果字符串为<code>null</code>则返回<code>null</code>。如果搜索字符串为<code>null</code>或空,则返回原字符串。3349 * </p>3350 *3351 * <p>3352 * 例如: <code>replaceChars(&quot;hello&quot;, &quot;ho&quot;, &quot;jy&quot;) = jelly</code>。3353 * </p>3354 *3355 * <p>3356 * 通常搜索字符串和替换字符串是等长的,如果搜索字符串比替换字符串长,则多余的字符将被删除。 如果搜索字符串比替换字符串短,则缺少的字符将被忽略。3357 * <pre>3358 * StringUtil.replaceChars(null, *, *)= null3359 * StringUtil.replaceChars(“”, *, *)= “”3360 * StringUtil.replaceChars(“abc”, null, *)= “abc”3361 * StringUtil.replaceChars(“abc”, “”, *)= “abc”3362 * StringUtil.replaceChars(“abc”, “b”, null)= “ac”3363 * StringUtil.replaceChars(“abc”, “b”, “”)= “ac”3364 * StringUtil.replaceChars(“abcba”, “bc”, “yz”) = “ayzya”3365 * StringUtil.replaceChars(“abcba”, “bc”, “y”) = “ayya”3366 * StringUtil.replaceChars(“abcba”, “bc”, “yzx”) = “ayzya”3367 * </pre>3368 * </p>3369 * str 要扫描的字符串 searchChars 要搜索的字符串 replaceChars 替换字符串3373 * 被替换后的字符串,如果原始字符串为<code>null</code>,则返回<code>null</code> String replaceChars(String str, String searchChars, String replaceChars) {3377if ((str == null) || (str.length() == 0) || (searchChars == null)3378|| (searchChars.length() == 0)) {3379return str;3380 }[] chars = str.toCharArray();3383int len = chars.length;3384boolean modified = false;(int i = 0, isize = searchChars.length(); i < isize; i++) {3387char searchChar = searchChars.charAt(i);((replaceChars == null) || (i >= replaceChars.length())) {pos = 0;(int j = 0; j < len; j++) {3394if (chars[j] != searchChar) {3395chars[pos++] = chars[j];3396} else {3397modified = true;3398 }3399 }3400 3401len = pos;3402} else {(int j = 0; j < len; j++) {3405if (chars[j] == searchChar) {3406chars[j] = replaceChars.charAt(i);3407modified = true;3408 }3409 }3410 }3411 }(!modified) {3414return str;3415 }String(chars, 0, len);3418 }* 将指定的子串用另一指定子串覆盖。3422 *3423 * <p>3424 * 如果字符串为<code>null</code>,则返回<code>null</code>。 负的索引值将被看作<code>0</code>,越界的索引值将被设置成字符串的长度相同的值。3425 * <pre>3426 * StringUtil.overlay(null, *, *, *)= null3427 * StringUtil.overlay(“”, “abc”, 0, 0)= “abc”3428 * StringUtil.overlay(“abcdef”, null, 2, 4)= “abef”3429 * StringUtil.overlay(“abcdef”, “”, 2, 4)= “abef”3430 * StringUtil.overlay(“abcdef”, “”, 4, 2)= “abef”3431 * StringUtil.overlay(“abcdef”, “zzzz”, 2, 4) = “abzzzzef”3432 * StringUtil.overlay(“abcdef”, “zzzz”, 4, 2) = “abzzzzef”3433 * StringUtil.overlay(“abcdef”, “zzzz”, -1, 4) = “zzzzef”3434 * StringUtil.overlay(“abcdef”, “zzzz”, 2, 8) = “abzzzz”3435 * StringUtil.overlay(“abcdef”, “zzzz”, -2, -3) = “zzzzabcdef”3436 * StringUtil.overlay(“abcdef”, “zzzz”, 8, 10) = “abcdefzzzz”3437 * </pre>3438 * </p>3439 * str 要扫描的字符串 overlay 用来覆盖的字符串 start 起始索引 end 结束索引3444 * 被覆盖后的字符串,如果原始字符串为<code>null</code>,则返回<code>null</code>String overlay(String str, String overlay, int start, int end) {3448if (str == null) {;3450 }(overlay == null) {3453overlay = EMPTY_STRING;3454 }len = str.length();(start < 0) {3459start = 0;3460 }(start > len) {3463start = len;3464 }(end < 0) {3467end = 0;3468 }(end > len) {3471end = len;3472 }(start > end) {3475int temp = start;3476 3477start = end;3478end = temp;3479 }StringBuffer((len + start) – end + overlay.length() + 1).append(3482str.substring(0, start)).append(overlay).append(str.substring(end)).toString();3483 }* 删除字符串末尾的换行符。如果字符串不以换行结尾,则什么也不做。3491 *3492 * <p>3493 * 换行符有三种情形:&quot;<code>\n</code>&quot;、&quot;<code>\r</code>&quot;、&quot;<code>\r\n</code>&quot;。3494 * <pre>3495 * StringUtil.chomp(null)= null3496 * StringUtil.chomp(“”)= “”3497 * StringUtil.chomp(“abc \r”)= “abc “3498 * StringUtil.chomp(“abc\n”)= “abc”3499 * StringUtil.chomp(“abc\r\n”)= “abc”3500 * StringUtil.chomp(“abc\r\n\r\n”) = “abc\r\n”3501 * StringUtil.chomp(“abc\n\r”)= “abc\n”3502 * StringUtil.chomp(“abc\n\rabc”) = “abc\n\rabc”3503 * StringUtil.chomp(“\r”)= “”3504 * StringUtil.chomp(“\n”)= “”3505 * StringUtil.chomp(“\r\n”)= “”3506 * </pre>3507 * </p>3508 * str 要处理的字符串3510 * 不以换行结尾的字符串,如果原始字串为<code>null</code>,则返回<code>null</code> String chomp(String str) {3514if ((str == null) || (str.length() == 0)) {3515return str;3516 }(str.length() == 1) {3519char ch = str.charAt(0);((ch == ‘\r’) || (ch == ‘\n’)) {3522return EMPTY_STRING;3523} else {3524return str;3525 }3526 }lastIdx = str.length() – 1;3529char last = str.charAt(lastIdx);(last == ‘\n’) {3532if (str.charAt(lastIdx – 1) == ‘\r’) {3533lastIdx–;3534 }3535} else if (last == ‘\r’) {3536} else {3537lastIdx++;3538 }str.substring(0, lastIdx);3541 }* 删除字符串末尾的指定字符串。如果字符串不以该字符串结尾,则什么也不做。3545 * <pre>3546 * StringUtil.chomp(null, *)= null3547 * StringUtil.chomp(“”, *)= “”3548 * StringUtil.chomp(“foobar”, “bar”) = “foo”3549 * StringUtil.chomp(“foobar”, “baz”) = “foobar”3550 * StringUtil.chomp(“foo”, “foo”) = “”3551 * StringUtil.chomp(“foo “, “foo”) = “foo “3552 * StringUtil.chomp(” foo”, “foo”) = ” “3553 * StringUtil.chomp(“foo”, “foooo”) = “foo”3554 * StringUtil.chomp(“foo”, “”)= “foo”3555 * StringUtil.chomp(“foo”, null)= “foo”3556 * </pre>3557 * str 要处理的字符串 separator 要删除的字符串3560 * 不以指定字符串结尾的字符串,如果原始字串为<code>null</code>,则返回<code>null</code> String chomp(String str, String separator) {3564if ((str == null) || (str.length() == 0) || (separator == null)) {3565return str;3566 } (str.endsWith(separator)) {3569return str.substring(0, str.length() – separator.length());3570 } str;3573 }* 删除最后一个字符。3577 *3578 * <p>3579 * 如果字符串以<code>\r\n</code>结尾,则同时删除它们。3580 * <pre>3581 * StringUtil.chop(null)= null3582 * StringUtil.chop(“”)= “”3583 * StringUtil.chop(“abc \r”)= “abc “3584 * StringUtil.chop(“abc\n”)= “abc”3585 * StringUtil.chop(“abc\r\n”)= “abc”3586 * StringUtil.chop(“abc”)= “ab”3587 * StringUtil.chop(“abc\nabc”) = “abc\nab”3588 * StringUtil.chop(“a”)= “”3589 * StringUtil.chop(“\r”)= “”3590 * StringUtil.chop(“\n”)= “”3591 * StringUtil.chop(“\r\n”)= “”3592 * </pre>3593 * </p>3594 * str 要处理的字符串3596 * 删除最后一个字符的字符串,如果原始字符串为<code>null</code>,则返回<code>null</code> String chop(String str) {3600if (str == null) {;3602 }strLen = str.length();(strLen < 2) {3607return EMPTY_STRING;3608 }lastIdx = strLen – 1;3611String ret = str.substring(0, lastIdx);3612char last = str.charAt(lastIdx);(last == ‘\n’) {3615if (ret.charAt(lastIdx – 1) == ‘\r’) {3616return ret.substring(0, lastIdx – 1);3617 }3618 } ret;3621 }* 将指定字符串重复n遍。3629 * <pre>3630 * StringUtil.repeat(null, 2) = null3631 * StringUtil.repeat(“”, 0)= “”3632 * StringUtil.repeat(“”, 2)= “”3633 * StringUtil.repeat(“a”, 3) = “aaa”3634 * StringUtil.repeat(“ab”, 2) = “abab”3635 * StringUtil.repeat(“abcd”, 2) = “abcdabcd”3636 * StringUtil.repeat(“a”, -2) = “”3637 * </pre>3638 * str 要重复的字符串 repeat 重复次数,如果小于<code>0</code>,则看作<code>0</code>3641 * 重复n次的字符串,如果原始字符串为<code>null</code>,则返回<code>null</code>String repeat(String str, int repeat) {3645if (str == null) {;3647 }(repeat <= 0) {3650return EMPTY_STRING;3651 }inputLength = str.length();((repeat == 1) || (inputLength == 0)) {3656return str;3657 }outputLength = inputLength * repeat; (inputLength) {3662case 1:ch = str.charAt(0);[outputLength];(int i = repeat – 1; i >= 0; i–) {3668output1[i] = ch;3669 } String(output1);2:ch0 = str.charAt(0);3676char ch1 = str.charAt(1);[outputLength];(int i = (repeat * 2) – 2; i >= 0; i–, i–) {3680output2[i] = ch0;3681output2[i + 1] = ch1;3682 } String(output2);:3687 3688StringBuffer buf = new StringBuffer(outputLength);(int i = 0; i < repeat; i++) {3691 buf.append(str);3692 } buf.toString();3695 }3696 }* 扩展并左对齐字符串,用空格<code>’ ‘</code>填充右边。3700 * <pre>3701 * StringUtil.alignLeft(null, *) = null3702 * StringUtil.alignLeft(“”, 3)= ” “3703 * StringUtil.alignLeft(“bat”, 3) = “bat”3704 * StringUtil.alignLeft(“bat”, 5) = “bat “3705 * StringUtil.alignLeft(“bat”, 1) = “bat”3706 * StringUtil.alignLeft(“bat”, -1) = “bat”3707 * </pre>3708 * str 要对齐的字符串 size 扩展字符串到指定宽度3711 * 扩展后的字符串,如果字符串为<code>null</code>,则返回<code>null</code>String alignLeft(String str, int size) {3715return alignLeft(str, size, ‘ ‘);3716 }* 扩展并左对齐字符串,用指定字符填充右边。3720 * <pre>3721 * StringUtil.alignLeft(null, *, *)= null3722 * StringUtil.alignLeft(“”, 3, ‘z’)= “zzz”3723 * StringUtil.alignLeft(“bat”, 3, ‘z’) = “bat”3724 * StringUtil.alignLeft(“bat”, 5, ‘z’) = “batzz”3725 * StringUtil.alignLeft(“bat”, 1, ‘z’) = “bat”3726 * StringUtil.alignLeft(“bat”, -1, ‘z’) = “bat”3727 * </pre>3728 * str 要对齐的字符串 size 扩展字符串到指定宽度 padChar 填充字符3732 * 扩展后的字符串,如果字符串为<code>null</code>,则返回<code>null</code>String alignLeft(String str, int size, char padChar) {3736if (str == null) {;3738 }pads = size – str.length();(pads <= 0) {3743return str;3744 } alignLeft(str, size, String.valueOf(padChar));3747 }* 扩展并左对齐字符串,用指定字符串填充右边。3751 * <pre>3752 * StringUtil.alignLeft(null, *, *)= null3753 * StringUtil.alignLeft(“”, 3, “z”)= “zzz”3754 * StringUtil.alignLeft(“bat”, 3, “yz”) = “bat”3755 * StringUtil.alignLeft(“bat”, 5, “yz”) = “batyz”3756 * StringUtil.alignLeft(“bat”, 8, “yz”) = “batyzyzy”3757 * StringUtil.alignLeft(“bat”, 1, “yz”) = “bat”3758 * StringUtil.alignLeft(“bat”, -1, “yz”) = “bat”3759 * StringUtil.alignLeft(“bat”, 5, null) = “bat “3760 * StringUtil.alignLeft(“bat”, 5, “”) = “bat “3761 * </pre>3762 * str 要对齐的字符串 size 扩展字符串到指定宽度 padStr 填充字符串3766 * 扩展后的字符串,如果字符串为<code>null</code>,则返回<code>null</code>String alignLeft(String str, int size, String padStr) {3770if (str == null) {;3772 }((padStr == null) || (padStr.length() == 0)) {3775padStr = ” “;3776 }padLen = padStr.length();3779int strLen = str.length();3780int pads = size – strLen;(pads <= 0) {3783return str;3784 }(pads == padLen) {3787return str.concat(padStr);3788} else if (pads < padLen) {3789return str.concat(padStr.substring(0, pads));3790} else {[pads];3792char[] padChars = padStr.toCharArray();(int i = 0; i < pads; i++) {3795padding[i] = padChars[i % padLen];3796 }str.concat(new String(padding));3799 }3800 }* 扩展并右对齐字符串,用空格<code>’ ‘</code>填充左边。3804 * <pre>3805 * StringUtil.alignRight(null, *) = null3806 * StringUtil.alignRight(“”, 3)= ” “3807 * StringUtil.alignRight(“bat”, 3) = “bat”3808 * StringUtil.alignRight(“bat”, 5) = ” bat”3809 * StringUtil.alignRight(“bat”, 1) = “bat”3810 * StringUtil.alignRight(“bat”, -1) = “bat”3811 * </pre>3812 * str 要对齐的字符串 size 扩展字符串到指定宽度3815 * 扩展后的字符串,如果字符串为<code>null</code>,则返回<code>null</code>String alignRight(String str, int size) {3819return alignRight(str, size, ‘ ‘);3820 }* 扩展并右对齐字符串,用指定字符填充左边。3824 * <pre>3825 * StringUtil.alignRight(null, *, *)= null3826 * StringUtil.alignRight(“”, 3, ‘z’)= “zzz”3827 * StringUtil.alignRight(“bat”, 3, ‘z’) = “bat”3828 * StringUtil.alignRight(“bat”, 5, ‘z’) = “zzbat”3829 * StringUtil.alignRight(“bat”, 1, ‘z’) = “bat”3830 * StringUtil.alignRight(“bat”, -1, ‘z’) = “bat”3831 * </pre>3832 * str 要对齐的字符串 size 扩展字符串到指定宽度 padChar 填充字符3836 * 扩展后的字符串,如果字符串为<code>null</code>,则返回<code>null</code>String alignRight(String str, int size, char padChar) {3840if (str == null) {;3842 }pads = size – str.length();(pads <= 0) {3847return str;3848 } alignRight(str, size, String.valueOf(padChar));3851 }* 扩展并右对齐字符串,用指定字符串填充左边。3855 * <pre>3856 * StringUtil.alignRight(null, *, *)= null3857 * StringUtil.alignRight(“”, 3, “z”)= “zzz”3858 * StringUtil.alignRight(“bat”, 3, “yz”) = “bat”3859 * StringUtil.alignRight(“bat”, 5, “yz”) = “yzbat”3860 * StringUtil.alignRight(“bat”, 8, “yz”) = “yzyzybat”3861 * StringUtil.alignRight(“bat”, 1, “yz”) = “bat”3862 * StringUtil.alignRight(“bat”, -1, “yz”) = “bat”3863 * StringUtil.alignRight(“bat”, 5, null) = ” bat”3864 * StringUtil.alignRight(“bat”, 5, “”) = ” bat”3865 * </pre>3866 * str 要对齐的字符串 size 扩展字符串到指定宽度 padStr 填充字符串3870 * 扩展后的字符串,如果字符串为<code>null</code>,则返回<code>null</code>String alignRight(String str, int size, String padStr) {3874if (str == null) {;3876 }((padStr == null) || (padStr.length() == 0)) {3879padStr = ” “;3880 }padLen = padStr.length();3883int strLen = str.length();3884int pads = size – strLen;(pads <= 0) {3887return str;3888 }(pads == padLen) {3891return padStr.concat(str);3892} else if (pads < padLen) {3893return padStr.substring(0, pads).concat(str);3894} else {[pads];3896char[] padChars = padStr.toCharArray();(int i = 0; i < pads; i++) {3899padding[i] = padChars[i % padLen];3900 } String(padding).concat(str);3903 }3904 }* 扩展并居中字符串,用空格<code>’ ‘</code>填充两边。3908 * <pre>3909 * StringUtil.center(null, *) = null3910 * StringUtil.center(“”, 4)= ” “3911 * StringUtil.center(“ab”, -1) = “ab”3912 * StringUtil.center(“ab”, 4) = ” ab “3913 * StringUtil.center(“abcd”, 2) = “abcd”3914 * StringUtil.center(“a”, 4) = ” a “3915 * </pre>3916 * str 要对齐的字符串 size 扩展字符串到指定宽度3919 * 扩展后的字符串,如果字符串为<code>null</code>,则返回<code>null</code>String center(String str, int size) {3923return center(str, size, ‘ ‘);3924 }* 扩展并居中字符串,用指定字符填充两边。3928 * <pre>3929 * StringUtil.center(null, *, *)= null3930 * StringUtil.center(“”, 4, ‘ ‘)= ” “3931 * StringUtil.center(“ab”, -1, ‘ ‘) = “ab”3932 * StringUtil.center(“ab”, 4, ‘ ‘) = ” ab “3933 * StringUtil.center(“abcd”, 2, ‘ ‘) = “abcd”3934 * StringUtil.center(“a”, 4, ‘ ‘) = ” a “3935 * StringUtil.center(“a”, 4, ‘y’) = “yayy”3936 * </pre>3937 * str 要对齐的字符串 size 扩展字符串到指定宽度 padChar 填充字符3941 * 扩展后的字符串,如果字符串为<code>null</code>,则返回<code>null</code>String center(String str, int size, char padChar) {3945if ((str == null) || (size <= 0)) {3946return str;3947 }strLen = str.length();3950int pads = size – strLen;(pads <= 0) {3953return str;3954 }3955 3956str = alignRight(str, strLen + (pads / 2), padChar);3957str = alignLeft(str, size, padChar);3958return str;3959 }* 扩展并居中字符串,用指定字符串填充两边。3963 * <pre>3964 * StringUtil.center(null, *, *)= null3965 * StringUtil.center(“”, 4, ” “)= ” “3966 * StringUtil.center(“ab”, -1, ” “) = “ab”3967 * StringUtil.center(“ab”, 4, ” “) = ” ab “3968 * StringUtil.center(“abcd”, 2, ” “) = “abcd”3969 * StringUtil.center(“a”, 4, ” “) = ” a “3970 * StringUtil.center(“a”, 4, “yz”) = “yayz”3971 * StringUtil.center(“abc”, 7, null) = ” abc “3972 * StringUtil.center(“abc”, 7, “”) = ” abc “3973 * </pre>3974 * str 要对齐的字符串 size 扩展字符串到指定宽度 padStr 填充字符串3978 * 扩展后的字符串,如果字符串为<code>null</code>,则返回<code>null</code>String center(String str, int size, String padStr) {3982if ((str == null) || (size <= 0)) {3983return str;3984 }((padStr == null) || (padStr.length() == 0)) {3987padStr = ” “;3988 }strLen = str.length();3991int pads = size – strLen;(pads <= 0) {3994return str;3995 }3996 3997str = alignRight(str, strLen + (pads / 2), padStr);3998str = alignLeft(str, size, padStr);3999return str;4000 }* 反转字符串中的字符顺序。4008 *4009 * <p>4010 * 如果字符串为<code>null</code>,则返回<code>null</code>。4011 * </p>4012 * <pre>4013 * StringUtil.reverse(null) = null4014 * StringUtil.reverse(“”) = “”4015 * StringUtil.reverse(“bat”) = “tab”4016 * </pre>4017 * str 要反转的字符串4019 * 反转后的字符串,如果原字符串为<code>null</code>,则返回<code>null</code> String reverse(String str) {4023if ((str == null) || (str.length() == 0)) {4024return str;4025 } StringBuffer(str).reverse().toString();4028 }* 反转指定分隔符分隔的各子串的顺序。4032 *4033 * <p>4034 * 如果字符串为<code>null</code>,则返回<code>null</code>。4035 * </p>4036 * <pre>4037 * StringUtil.reverseDelimited(null, *)= null4038 * StringUtil.reverseDelimited(“”, *)= “”4039 * StringUtil.reverseDelimited(“a.b.c”, ‘x’) = “a.b.c”4040 * StringUtil.reverseDelimited(“a.b.c”, ‘.’) = “c.b.a”4041 * </pre>4042 * str 要反转的字符串 separatorChar 分隔符4045 * 反转后的字符串,如果原字符串为<code>null</code>,则返回<code>null</code>String reverseDelimited(String str, char separatorChar) {4049if (str == null) {;4051 }4052 4053String[] strs = split(str, separatorChar);4054 4055 ArrayUtil.reverse(strs); join(strs, separatorChar);4058 }* 反转指定分隔符分隔的各子串的顺序。4062 *4063 * <p>4064 * 如果字符串为<code>null</code>,则返回<code>null</code>。4065 * </p>4066 * <pre>4067 * StringUtil.reverseDelimited(null, *, *)= null4068 * StringUtil.reverseDelimited(“”, *, *)= “”4069 * StringUtil.reverseDelimited(“a.b.c”, null, null) = “a.b.c”4070 * StringUtil.reverseDelimited(“a.b.c”, “”, null) = “a.b.c”4071 * StringUtil.reverseDelimited(“a.b.c”, “.”, “,”) = “c,b,a”4072 * StringUtil.reverseDelimited(“a.b.c”, “.”, null) = “c b a”4073 * </pre>4074 * str 要反转的字符串 separatorChars 分隔符,如果为<code>null</code>,则默认使用空白字符 separator 用来连接子串的分隔符,如果为<code>null</code>,默认使用空格4078 * 反转后的字符串,如果原字符串为<code>null</code>,则返回<code>null</code> String reverseDelimited(String str, String separatorChars, String separator) {4082if (str == null) {;4084 }4085 4086String[] strs = split(str, separatorChars);4087 4088 ArrayUtil.reverse(strs);(separator == null) {4091return join(strs, ‘ ‘);4092 } join(strs, separator);4095 }* 将字符串转换成指定长度的缩略,例如: 将”Now is the time for all good men”转换成”Now is the time for…”。4103 *4104 * <ul>4105 * <li>4106 * 如果<code>str</code>比<code>maxWidth</code>短,直接返回;4107 * </li>4108 * <li>4109 * 否则将它转换成缩略:<code>substring(str, 0, max-3) + “…”</code>;4110 * </li>4111 * <li>4112 * 如果<code>maxWidth</code>小于<code>4</code>抛出<code>IllegalArgumentException</code>;4113 * </li>4114 * <li>4115 * 返回的字符串不可能长于指定的<code>maxWidth</code>。4116 * </li>4117 * </ul>4118 *4119 * <pre>4120 * StringUtil.abbreviate(null, *)= null4121 * StringUtil.abbreviate(“”, 4)= “”4122 * StringUtil.abbreviate(“abcdefg”, 6) = “abc…”4123 * StringUtil.abbreviate(“abcdefg”, 7) = “abcdefg”4124 * StringUtil.abbreviate(“abcdefg”, 8) = “abcdefg”4125 * StringUtil.abbreviate(“abcdefg”, 4) = “a…”4126 * StringUtil.abbreviate(“abcdefg”, 3) = IllegalArgumentException4127 * </pre>4128 * str 要检查的字符串 maxWidth 最大长度,不小于<code>4</code>,如果小于<code>4</code>,则看作<code>4</code>4131 * 字符串缩略,如果原始字符串为<code>null</code>则返回<code>null</code>String abbreviate(String str, int maxWidth) {4135return abbreviate(str, 0, maxWidth);4136 }* 将字符串转换成指定长度的缩略,例如: 将”Now is the time for all good men”转换成”…is the time for…”。4140 *4141 * <p>4142 * 和<code>abbreviate(String, int)</code>类似,但是增加了一个“左边界”偏移量。4143 * 注意,美国空间,“左边界”处的字符未必出现在结果字符串的最左边,但一定出现在结果字符串中。4144 * </p>4145 *4146 * <p>4147 * 返回的字符串不可能长于指定的<code>maxWidth</code>。4148 * <pre>4149 * StringUtil.abbreviate(null, *, *)= null4150 * StringUtil.abbreviate(“”, 0, 4)= “”4151 * StringUtil.abbreviate(“abcdefghijklmno”, -1, 10) = “abcdefg…”4152 * StringUtil.abbreviate(“abcdefghijklmno”, 0, 10) = “abcdefg…”4153 * StringUtil.abbreviate(“abcdefghijklmno”, 1, 10) = “abcdefg…”4154 * StringUtil.abbreviate(“abcdefghijklmno”, 4, 10) = “abcdefg…”4155 * StringUtil.abbreviate(“abcdefghijklmno”, 5, 10) = “…fghi…”4156 * StringUtil.abbreviate(“abcdefghijklmno”, 6, 10) = “…ghij…”4157 * StringUtil.abbreviate(“abcdefghijklmno”, 8, 10) = “…ijklmno”4158 * StringUtil.abbreviate(“abcdefghijklmno”, 10, 10) = “…ijklmno”4159 * StringUtil.abbreviate(“abcdefghijklmno”, 12, 10) = “…ijklmno”4160 * StringUtil.abbreviate(“abcdefghij”, 0, 3)= IllegalArgumentException4161 * StringUtil.abbreviate(“abcdefghij”, 5, 6)= IllegalArgumentException4162 * </pre>4163 * </p>4164 * str 要检查的字符串 offset 左边界偏移量 maxWidth 最大长度,不小于<code>4</code>,如果小于<code>4</code>,则看作<code>4</code>4168 * 字符串缩略,如果原始字符串为<code>null</code>则返回<code>null</code>String abbreviate(String str, int offset, int maxWidth) {4172if (str == null) {;4174 }(maxWidth < 4) {4178maxWidth = 4;4179 }(str.length() <= maxWidth) {4182return str;4183 }(offset > str.length()) {4186offset = str.length();4187 }((str.length() – offset) < (maxWidth – 3)) {4190offset = str.length() – (maxWidth – 3);4191 }(offset <= 4) {4194return str.substring(0, maxWidth – 3) + “…”;4195 }(maxWidth < 7) {4199maxWidth = 7;4200 }((offset + (maxWidth – 3)) < str.length()) {4203return “…” + abbreviate(str.substring(offset), maxWidth – 3);4204 }”…” + str.substring(str.length() – (maxWidth – 3));4207 }* 比较两个字符串,取得第二个字符串中,和第一个字符串不同的部分。4217 * <pre>4218 * StringUtil.difference(“i am a machine”, “i am a robot”) = “robot”4219 * StringUtil.difference(null, null)= null4220 * StringUtil.difference(“”, “”)= “”4221 * StringUtil.difference(“”, null)= “”4222 * StringUtil.difference(“”, “abc”)= “abc”4223 * StringUtil.difference(“abc”, “”)= “”4224 * StringUtil.difference(“abc”, “abc”)= “”4225 * StringUtil.difference(“ab”, “abxyz”)= “xyz”4226 * StringUtil.difference(“abcde”, “abxyz”)= “xyz”4227 * StringUtil.difference(“abcde”, “xyz”)= “xyz”4228 * </pre>4229 * str1 字符串1 str2 字符串24232 * 第二个字符串中,和第一个字符串不同的部分。如果两个字符串相同,则返回空字符串<code>””</code> String difference(String str1, String str2) {4236if (str1 == null) {4237return str2;4238 }(str2 == null) {4241return str1;4242 }index = indexOfDifference(str1, str2);(index == -1) {4247return EMPTY_STRING;4248 } str2.substring(index);4251 }* 比较两个字符串,取得两字符串开始不同的索引值。4255 * <pre>4256 * StringUtil.indexOfDifference(“i am a machine”, “i am a robot”) = 74257 * StringUtil.indexOfDifference(null, null)= -14258 * StringUtil.indexOfDifference(“”, null)= -14259 * StringUtil.indexOfDifference(“”, “”)= -14260 * StringUtil.indexOfDifference(“”, “abc”)= 04261 * StringUtil.indexOfDifference(“abc”, “”)= 04262 * StringUtil.indexOfDifference(“abc”, “abc”)= -14263 * StringUtil.indexOfDifference(“ab”, “abxyz”)= 24264 * StringUtil.indexOfDifference(“abcde”, “abxyz”)= 24265 * StringUtil.indexOfDifference(“abcde”, “xyz”)= 04266 * </pre>4267 * str1 字符串1 str2 字符串24270 * 两字符串开始产生差异的索引值,如果两字符串相同,则返回<code>-1</code> indexOfDifference(String str1, String str2) {4274if ((str1 == str2) || (str1 == null) || (str2 == null)) {4275return -1;4276 } i;(i = 0; (i < str1.length()) && (i < str2.length()); ++i) {4281if (str1.charAt(i) != str2.charAt(i)) {4282break;4283 }4284 }((i < str2.length()) || (i < str1.length())) {4287return i;4288 }-1;4291 }* 取得两个字符串的相似度,<code>0</code>代表字符串相等,数字越大表示字符串越不像。4295 *4296 * <p></a>。4298 * 它计算的是从字符串1转变到字符串2所需要的删除、插入和替换的步骤数。4299 * </p>4300 * <pre>4301 * StringUtil.getLevenshteinDistance(null, *)= IllegalArgumentException4302 * StringUtil.getLevenshteinDistance(*, null)= IllegalArgumentException4303 * StringUtil.getLevenshteinDistance(“”,””)= 04304 * StringUtil.getLevenshteinDistance(“”,”a”)= 14305 * StringUtil.getLevenshteinDistance(“aaapppp”, “”)= 74306 * StringUtil.getLevenshteinDistance(“frog”, “fog”)= 14307 * StringUtil.getLevenshteinDistance(“fly”, “ant”)= 34308 * StringUtil.getLevenshteinDistance(“elephant”, “hippo”) = 74309 * StringUtil.getLevenshteinDistance(“hippo”, “elephant”) = 74310 * StringUtil.getLevenshteinDistance(“hippo”, “zzzzzzzz”) = 84311 * StringUtil.getLevenshteinDistance(“hello”, “hallo”) = 14312 * </pre>4313 * s 第一个字符串,如果是<code>null</code>,则看作空字符串 t 第二个字符串,如果是<code>null</code>,则看作空字符串4316 * 相似度值 getLevenshteinDistance(String s, String t) {4320s = defaultIfNull(s);4321t = defaultIfNull(t);[][] d; n; m; i; j; s_i; t_j; cost; // costn = s.length();4334m = t.length();(n == 0) {4337return m;4338 }(m == 0) {4341return n;4342 }[n + 1][m + 1];(i = 0; i <= n; i++) {4348d[i][0] = i;4349 }(j = 0; j <= m; j++) {4352d[0][j] = j;4353 }(i = 1; i <= n; i++) {4357s_i = s.charAt(i – 1);(j = 1; j <= m; j++) {4361t_j = t.charAt(j – 1);(s_i == t_j) {4365cost = 0;4366} else {4367cost = 1;4368 }d[i][j] = min(d[i – 1][j] + 1, d[i][j – 1] + 1, d[i – 1][j – 1] + cost);4372 }4373 } d[n][m];4377 }* 取得最小数。4381 * a 整数1 b 整数2 c 整数34385 * 三个数中的最小值min(int a, int b, int c) {4389if (b < a) {4390a = b;4391 }(c < a) {4394a = c;4395 } a;4398 }4399 }觉得自己做的到和不做的到,其实只在一念之间

分享一个String工具类,也许你的项目中会用得到

相关文章:

你感兴趣的文章:

标签云: