1 org.apache.commons.lang.StringUtils中方法的操作对象是java.lang.String类型的对象,是JDK提供的String类型操作方法的补充,并且是null安全的(即如果输入参数String为null则不会抛出NullPointerException,而是做了相应处理,例如,如果输入为null则返回也是null等,具体可以查看源代码)。 2 3 除了构造器,StringUtils中一共有130多个方法,并且都是static的, 4 5 所以我们可以这样调用StringUtils.xxx()。 6 7 下面分别对一些常用方法做简要介绍: 8 9 1. public static boolean isEmpty(String str) 10 11 判断某字符串是否为空,为空的标准是str == null 或 str.length() == 0 12 13 下面是示例: 14 15 StringUtils.isEmpty(null) = true 16 17 StringUtils.isEmpty("") = true 18 19 StringUtils.isEmpty(" ") = false 20 21 StringUtils.isEmpty(" ") = false 22 23 StringUtils.isEmpty("bob") = false 24 25 StringUtils.isEmpty(" bob ") = false 26 27 2. public static boolean isNotEmpty(String str) 28 29 判断某字符串是否非空,等于!isEmpty(String str) 30 31 下面是示例: 32 33 StringUtils.isNotEmpty(null) = false 34 35 StringUtils.isNotEmpty("") = false 36 37 StringUtils.isNotEmpty(" ") = true 38 39 StringUtils.isNotEmpty(" ") = true 40 41 StringUtils.isNotEmpty("bob") = true 42 43 StringUtils.isNotEmpty(" bob ") = true 44 45 3. public static boolean isBlank(String str) 46 47 判断某字符串是否为空或长度为0或由空白符(whitespace)构成 48 49 下面是示例: 50 51 StringUtils.isBlank(null) = true 52 53 StringUtils.isBlank("") = true 54 55 StringUtils.isBlank(" ") = true 56 57 StringUtils.isBlank(" ") = true 58 59 StringUtils.isBlank("\t \n \f \r") = true 60 61 StringUtils.isBlank("\b") = false 62 63 StringUtils.isBlank("bob") = false 64 65 StringUtils.isBlank(" bob ") = false 66 67 4. public static boolean isNotBlank(String str) 68 69 判断某字符串是否不为空且长度不为0且不由空白符(whitespace)构成, 70 71 等于!isBlank(String str) 72 73 下面是示例: 74 75 StringUtils.isNotBlank(null) = false 76 77 StringUtils.isNotBlank("") = false 78 79 StringUtils.isNotBlank(" ") = false 80 81 StringUtils.isNotBlank(" ") = false 82 83 StringUtils.isNotBlank("\t \n \f \r") = false 84 85 StringUtils.isNotBlank("\b") = true 86 87 StringUtils.isNotBlank("bob") = true 88 89 StringUtils.isNotBlank(" bob ") = true 90 91 5. public static String trim(String str) 92 93 去掉字符串两端的控制符(control characters, char <= 32) 94 95 如果输入为null则返回null 96 97 下面是示例: 98 99 StringUtils.trim(null) = null 100 101 StringUtils.trim("") = "" 102 103 StringUtils.trim(" ") = "" 104 105 StringUtils.trim(" \b \t \n \f \r ") = "" 106 107 StringUtils.trim(" \n\tss \b") = "ss" 108 109 StringUtils.trim(" d d dd ") = "d d dd" 110 111 StringUtils.trim("dd ") = "dd" 112 113 StringUtils.trim(" dd ") = "dd" 114 115 6.public static String trimToNull(String str) 116 117 去掉字符串两端的控制符(control characters, char <= 32) 118 119 如果变为null或"",则返回null 120 121 下面是示例: 122 123 StringUtils.trimToNull(null) = null 124 125 StringUtils.trimToNull("") = null 126 127 StringUtils.trimToNull(" ") = null 128 129 StringUtils.trimToNull(" \b \t \n \f \r ") = null 130 131 StringUtils.trimToNull(" \n\tss \b") = "ss" 132 133 StringUtils.trimToNull(" d d dd ") = "d d dd" 134 135 StringUtils.trimToNull("dd ") = "dd" 136 137 StringUtils.trimToNull(" dd ") = "dd" 138 139 7.public static String trimToEmpty(String str) 140 141 去掉字符串两端的控制符(control characters, char <= 32) 142 143 如果变为null或"",则返回"" 144 145 下面是示例: 146 147 StringUtils.trimToEmpty(null) = "" 148 149 StringUtils.trimToEmpty("") = "" 150 151 StringUtils.trimToEmpty(" ") = "" 152 153 StringUtils.trimToEmpty(" \b \t \n \f \r ") = "" 154 155 StringUtils.trimToEmpty(" \n\tss \b") = "ss" 156 157 StringUtils.trimToEmpty(" d d dd ") = "d d dd" 158 159 StringUtils.trimToEmpty("dd ") = "dd" 160 161 StringUtils.trimToEmpty(" dd ") = "dd" 162 163 8.public static String strip(String str) 164 165 去掉字符串两端的空白符(whitespace), 166 167 如果输入为null则返回null 168 169 下面是示例(注意和trim()的区别): 170 171 StringUtils.strip(null) = null 172 173 StringUtils.strip("") = "" 174 175 StringUtils.strip(" ") = "" 176 177 StringUtils.strip(" \b \t \n \f \r ") = "\b" 178 179 StringUtils.strip(" \n\tss \b") = "ss \b" 180 181 StringUtils.strip(" d d dd ") = "d d dd" 182 183 StringUtils.strip("dd ") = "dd" 184 185 StringUtils.strip(" dd ") = "dd" 186 187 9.public static String stripToNull(String str) 188 189 去掉字符串两端的空白符(whitespace), 190 191 如果变为null或"",则返回null 192 193 下面是示例(注意和trimToNull()的区别): 194 195 StringUtils.stripToNull(null) = null 196 197 StringUtils.stripToNull("") = null 198 199 StringUtils.stripToNull(" ") = null 200 201 StringUtils.stripToNull(" \b \t \n \f \r ") = "\b" 202 203 StringUtils.stripToNull(" \n\tss \b") = "ss \b" 204 205 StringUtils.stripToNull(" d d dd ") = "d d dd" 206 207 StringUtils.stripToNull("dd ") = "dd" 208 209 StringUtils.stripToNull(" dd ") = "dd" 210 211 10.public static String stripToEmpty(String str) 212 213 去掉字符串两端的空白符(whitespace), 214 215 如果变为null或"",则返回"" 216 217 下面是示例(注意和trimToEmpty()的区别): 218 219 StringUtils.stripToNull(null) = "" 220 221 StringUtils.stripToNull("") = "" 222 223 StringUtils.stripToNull(" ") = "" 224 225 StringUtils.stripToNull(" \b \t \n \f \r ") = "\b" 226 227 StringUtils.stripToNull(" \n\tss \b") = "ss \b" 228 229 StringUtils.stripToNull(" d d dd ") = "d d dd" 230 231 StringUtils.stripToNull("dd ") = "dd" 232 233 StringUtils.stripToNull(" dd ") = "dd" 234 235 236 237 11.public static String strip(String str, String stripChars) 238 239 去掉str两端的在stripChars中的字符。 240 241 如果str为null或等于"",则返回它本身; 242 243 如果stripChars为null或"",则返回strip(String str)。 244 245 12.public static String stripStart(String str, String stripChars) 246 247 和11相似,去掉str前端的在stripChars中的字符。 248 249 13.public static String stripEnd(String str, String stripChars) 250 251 和11相似,去掉str末端的在stripChars中的字符。 252 253 14.public static String[] stripAll(String[] strs) 254 255 对字符串数组中的每个字符串进行strip(String str),然后返回。 256 257 如果strs为null或strs长度为0,则返回strs本身 258 259 15.public static String[] stripAll(String[] strs, String stripChars) 260 261 对字符串数组中的每个字符串进行strip(String str, String stripChars),然后返回。 262 263 如果strs为null或strs长度为0,则返回strs本身 264 265 16.public static boolean equals(String str1, String str2) 266 267 比较两个字符串是否相等,如果两个均为空则也认为相等。 268 269 17.public static boolean equalsIgnoreCase(String str1, String str2) 270 271 比较两个字符串是否相等,不区分大小写,如果两个均为空则也认为相等。 272 273 18.public static int indexOf(String str, char searchChar) 274 275 返回字符searchChar在字符串str中第一次出现的位置。 276 277 如果searchChar没有在str中出现则返回-1, 278 279 如果str为null或"",则也返回-1 280 281 19.public static int indexOf(String str, char searchChar, int startPos) 282 283 返回字符searchChar从startPos开始在字符串str中第一次出现的位置。 284 285 如果从startPos开始searchChar没有在str中出现则返回-1, 286 287 如果str为null或"",则也返回-1 288 289 20.public static int indexOf(String str, String searchStr) 290 291 返回字符串searchStr在字符串str中第一次出现的位置。 292 293 如果str为null或searchStr为null则返回-1, 294 295 如果searchStr为"",且str为不为null,则返回0, 296 297 如果searchStr不在str中,则返回-1 298 299 21.public static int ordinalIndexOf(String str, String searchStr, int ordinal) 300 301 返回字符串searchStr在字符串str中第ordinal次出现的位置。 302 303 如果str=null或searchStr=null或ordinal<=0则返回-1 304 305 举例(*代表任意字符串): 306 307 StringUtils.ordinalIndexOf(null, *, *) = -1 308 309 StringUtils.ordinalIndexOf(*, null, *) = -1 310 311 StringUtils.ordinalIndexOf("", "", *) = 0 312 313 StringUtils.ordinalIndexOf("aabaabaa", "a", 1) = 0 314 315 StringUtils.ordinalIndexOf("aabaabaa", "a", 2) = 1 316 317 StringUtils.ordinalIndexOf("aabaabaa", "b", 1) = 2 318 319 StringUtils.ordinalIndexOf("aabaabaa", "b", 2) = 5 320 321 StringUtils.ordinalIndexOf("aabaabaa", "ab", 1) = 1 322 323 StringUtils.ordinalIndexOf("aabaabaa", "ab", 2) = 4 324 325 StringUtils.ordinalIndexOf("aabaabaa", "bc", 1) = -1 326 327 StringUtils.ordinalIndexOf("aabaabaa", "", 1) = 0 328 329 StringUtils.ordinalIndexOf("aabaabaa", "", 2) = 0 330 331 22. public static int indexOf(String str, String searchStr, int startPos) 332 333 返回字符串searchStr从startPos开始在字符串str中第一次出现的位置。 334 335 举例(*代表任意字符串): 336 337 StringUtils.indexOf(null, *, *) = -1 338 339 StringUtils.indexOf(*, null, *) = -1 340 341 StringUtils.indexOf("", "", 0) = 0 342 343 StringUtils.indexOf("aabaabaa", "a", 0) = 0 344 345 StringUtils.indexOf("aabaabaa", "b", 0) = 2 346 347 StringUtils.indexOf("aabaabaa", "ab", 0) = 1 348 349 StringUtils.indexOf("aabaabaa", "b", 3) = 5 350 351 StringUtils.indexOf("aabaabaa", "b", 9) = -1 352 353 StringUtils.indexOf("aabaabaa", "b", -1) = 2 354 355 StringUtils.indexOf("aabaabaa", "", 2) = 2 356 357 StringUtils.indexOf("abc", "", 9) = 3 358 359 23.public static int lastIndexOf(String str, char searchChar) 360 361 基本原理同18。 362 363 24.public static int lastIndexOf(String str, char searchChar, int startPos) 364 365 基本原理同19。 366 367 25.public static int lastIndexOf(String str, String searchStr) 368 369 基本原理同20。 370 371 26.public static int lastIndexOf(String str, String searchStr, int startPos) 372 373 基本原理同22。 374 375 376 377 27.public static boolean contains(String str, char searchChar) 378 379 判断字符串str中是否包含字符searchChar。 380 381 如果str为null或"",返回false; 382 383 如果searchChar不在str中,返回false。 384 385 28.public static boolean contains(String str, String searchStr) 386 387 判断字符串str是否包含字符串searchStr。 388 389 如果str为null或searchStr为null,返回false; 390 391 如果str为"",并且searchStr为"",返回true 392 393 举例: 394 395 StringUtils.contains("", "") = true 396 397 StringUtils.contains("dfg", "") = true 398 399 StringUtils.contains("dfg", "d") = true 400 401 StringUtils.contains("dfg", "gz") = false 402 403 29.public static boolean containsIgnoreCase(String str, String searchStr) 404 405 判断字符串str是否包含字符串searchStr,不区分大小写。 406 407 和28类似。 408 409 30.public static int indexOfAny(String str, char[] searchChars) 410 411 找出字符数组searchChars中的字符第一次出现在字符串str中的位置。 412 413 如果字符数组中的字符都不在字符串中,则返回-1 414 415 如果字符串为null或"",则返回-1 416 417 举例(*表示任意): 418 419 StringUtils.indexOfAny(null, *) = -1 420 421 StringUtils.indexOfAny("", *) = -1 422 423 StringUtils.indexOfAny(*, []) = -1 424 425 StringUtils.indexOfAny("asdf", ['a','f',' ']) = 0 426 427 StringUtils.indexOfAny("bs df", ['a','f',' ']) = 2 428 429 StringUtils.indexOfAny("bsdf", ['a','f',' ']) = 3 430 431 StringUtils.indexOfAny("bbeegg", ['a','f',' ']) = -1 432 433 31.public static int indexOfAny(String str, String searchChars) 434 435 找出字符串searchChars中的字符第一次出现在字符串str中的位置。 436 437 如果字符串searchChars中的字符都不在字符串str中,则返回-1 438 439 如果searchChars或str为null或为"",则返回-1 440 441 举例(*表示任意): 442 443 StringUtils.indexOfAny(null, *) = -1 444 445 StringUtils.indexOfAny("", *) = -1 446 447 StringUtils.indexOfAny(*, null) = -1 448 449 StringUtils.indexOfAny(*, "") = -1 450 451 StringUtils.indexOfAny("asdf", "af ") = 0 452 453 StringUtils.indexOfAny("bs df", "af ") = 2 454 455 StringUtils.indexOfAny("bsdf", "af ") = 3 456 457 StringUtils.indexOfAny("bbeegg", "af ") = -1 458 459 32.public static int indexOfAnyBut(String str, char[] searchChars) 460 461 找出字符串str中不在字符数组searchChars中的第一个字符的位置。 462 463 如果字符串中的所有字符都在字符数组中,则返回-1 464 465 如果字符串为null或"",则返回-1 466 467 举例(*表示任意): 468 469 StringUtils.indexOfAnyBut(null, *) = -1 470 471 StringUtils.indexOfAnyBut("", *) = -1 472 473 StringUtils.indexOfAnyBut(*, []) = -1 474 475 StringUtils.indexOfAnyBut("asdf", ['a','f',' ']) = 1 476 477 StringUtils.indexOfAnyBut("bs df", ['a','f',' ']) = 0 478 479 StringUtils.indexOfAnyBut(" aaf", ['a','f',' ']) = -1 480 481 StringUtils.indexOfAnyBut("bbeegg", ['a','f',' ']) = 0 482 483 33.public static int indexOfAnyBut(String str, String searchChars) 484 485 找出字符串str中不在字符串searchChars中的第一个字符的位置。 486 487 如果字符串str中的所有字符都在字符串searchChars中,则返回-1 488 489 如果字符串str或searchChars为null或"",则返回-1 490 491 举例(*表示任意): 492 493 StringUtils.indexOfAnyBut(null, *) = -1 494 495 StringUtils.indexOfAnyBut("", *) = -1 496 497 StringUtils.indexOfAnyBut(*, null) = -1 498 499 StringUtils.indexOfAnyBut(*, "") = -1 500 501 StringUtils.indexOfAnyBut("asdf", "af ") = 1 502 503 StringUtils.indexOfAnyBut("bs df", "af ") = 0 504 505 StringUtils.indexOfAnyBut(" aaf", "af ") = -1 506 507 StringUtils.indexOfAnyBut("bbeegg", "af ") = 0 508 509 34.public static boolean containsOnly(String str, char[] valid) 510 511 判断是否字符串str仅包含字符数组valid中的字符,即字符串中的字符是否都在字符数组中。 512 513 如果str为null,则返回false;如果str为"",则返回true 514 515 举例(*表示任意): 516 517 StringUtils.containsOnly(null, *)) = false 518 519 StringUtils.containsOnly("", *)) = true 520 521 StringUtils.containsOnly("afaf", ['a','f',' ']))= true 522 523 StringUtils.containsOnly("af a", ['a','f',' ']))= true 524 525 StringUtils.containsOnly("a", ['a','f',' '])) = true 526 527 StringUtils.containsOnly("afg", ['a','f',' '])) = false 528 529 StringUtils.containsOnly("bbeegg", [])) = false 530 531 35.public static boolean containsOnly(String str, String validChars) 532 533 判断是否字符串str仅包含字符串validChars中的字符, 534 535 即字符串str中的字符是否都在字符串validChars中。 536 537 和34类似,举例(*表示任意): 538 539 StringUtils.containsOnly(null, *) = false 540 541 StringUtils.containsOnly(*, null) = false 542 543 StringUtils.containsOnly("", "") = true 544 545 StringUtils.containsOnly("", "a") = true 546 547 StringUtils.containsOnly("afaf", "af ") = true 548 549 StringUtils.containsOnly("af a", "af ") = true 550 551 StringUtils.containsOnly("afg", "af ") = false 552 553 StringUtils.containsOnly("afg", "") = false 554 555 36.public static boolean containsNone(String str, char[] invalidChars) 556 557 判断是否字符串str不包含字符数组invalidChars中的字符,如果含有则返回false。 558 559 举例(*表示任意): 560 561 StringUtils.containsNone(null, *) = true 562 563 StringUtils.containsNone(*, []) = true 564 565 StringUtils.containsNone("", *) = true 566 567 StringUtils.containsNone("ab", []) = true 568 569 StringUtils.containsNone("b", ['a','f',' ']) = true 570 571 StringUtils.containsNone("bcd", ['a','f',' ']) = true 572 573 StringUtils.containsNone("abc", ['a','f',' ']) = false 574 575 StringUtils.containsNone(" ", ['a','f',' ']) = false 576 577 37.public static boolean containsNone(String str, String invalidChars) 578 579 判断是否字符串str不包含字符串invalidChars中的字符,如果含有则返回false。 580 581 举例(*表示任意): 582 583 StringUtils.containsNone(null, *) = true 584 585 StringUtils.containsNone(*, null) = true 586 587 StringUtils.containsNone("", *) = true 588 589 StringUtils.containsNone("ab", "") = true 590 591 StringUtils.containsNone("b", "af ") = true 592 593 StringUtils.containsNone("bcd", "af ") = true 594 595 StringUtils.containsNone("abc", "af ") = false 596 597 StringUtils.containsNone(" ", "af ") = false 598 599 600 601 38.public static int indexOfAny(String str, String[] searchStrs) 602 603 找出字符串数组searchStrs中的字符串第一次出现在字符串str中的位置。 604 605 如果数组中没有字符串在str中,则返回-1 606 607 如果数组为null或长度为0,则返回-1 608 609 举例(*表示任意): 610 611 StringUtils.indexOfAny(null, *) = -1 612 613 StringUtils.indexOfAny(*, null) = -1 614 615 StringUtils.indexOfAny(*, []) = -1 616 617 StringUtils.indexOfAny("", [""]) = 0 618 619 StringUtils.indexOfAny("bbeegg", ["as","df","yy"]) = -1 620 621 StringUtils.indexOfAny("asdfgh", ["as","df","yy"]) = 0 622 623 StringUtils.indexOfAny("dfasgh", ["as","df","yy"]) = 0 624 625 StringUtils.indexOfAny("ghasdf", ["as","df","yy"]) = 2 626 627 39.public static int lastIndexOfAny(String str, String[] searchStrs) 628 629 找出字符串数组searchStrs中的字符串最后一次出现在字符串str中的位置。 630 631 如果数组中没有字符串在str中,则返回-1 632 633 如果数组为null或长度为0,则返回-1 634 635 举例(*表示任意): 636 637 StringUtils.lastIndexOfAny(null, *) = -1 638 639 StringUtils.lastIndexOfAny(*, null) = -1 640 641 StringUtils.lastIndexOfAny(*, []) = -1 642 643 StringUtils.lastIndexOfAny("", [""]) = 0 644 645 StringUtils.lastIndexOfAny("bbeegg", ["as","df","yy"]) = -1 646 647 StringUtils.lastIndexOfAny("asdfgh", ["as","df","yy"]) = 2 648 649 StringUtils.lastIndexOfAny("dfghjk", ["as","df","yy"]) = 0 650 651 StringUtils.lastIndexOfAny("ghasdf", ["as","df","yy"]) = 4 652 653 StringUtils.lastIndexOfAny("ghasdf", ["as","df",""]) = 6 654 655 40.public static String substring(String str, int start) 656 657 得到字符串str的子串。 658 659 如果start小于0,位置是从后往前数的第|start|个 660 661 如果str为null或"",则返回它本身 662 663 举例(*表示任意): 664 665 StringUtils.substring(null, *) = null 666 667 StringUtils.substring("", *) = "" 668 669 StringUtils.substring("asdf", 0)) = "asdf" 670 671 StringUtils.substring("asdf", 1)) = "sdf" 672 673 StringUtils.substring("asdf", 3)) = "f" 674 675 StringUtils.substring("asdf",) = "" 676 677 StringUtils.substring("asdf", -1)) = "f" 678 679 StringUtils.substring("asdf", -3)) = "sdf" 680 681 StringUtils.substring("asdf", -8)) = "asdf" 682 683 41.public static String substring(String str, int start, int end) 684 685 得到字符串str的子串。 686 687 如果start小于0,位置是从后往前数的第|start|个, 688 689 如果end小于0,位置是从后往前数的第|end|个, 690 691 如果str为null或"",则返回它本身 692 693 举例(*表示任意): 694 695 StringUtils.substring(null, *, *) = null 696 697 StringUtils.substring("", * , *) = ""; 698 699 StringUtils.substring("asdf", 0, 2) = "as" 700 701 StringUtils.substring("asdf", 0, -1) = "asd" 702 703 StringUtils.substring("asdf", 2, -1) = "d" 704 705 StringUtils.substring("asdf", 2, -2) = "" 706 707 StringUtils.substring("asdf", 3, 2) = "" 708 709 StringUtils.substring("asdf", 1, = "sdf" 710 711 StringUtils.substring("asdf", -1, -3) = "" 712 713 StringUtils.substring("asdf", -3, -1) = "sd" 714 715 StringUtils.substring("asdf", -8, 5) = "asdf" 716 717 42.public static String left(String str, int len) 718 719 得到字符串str从左边数len长度的子串。 720 721 如果str为null或为"",则返回它本身 722 723 如果len小于0,则返回"" 724 725 举例(*表示任意): 726 727 StringUtils.left(null, *) = null 728 729 StringUtils.left(*, -ve) = "" 730 731 StringUtils.left("", *) = "" 732 733 StringUtils.left("asdf", 0) = "" 734 735 StringUtils.left("asdf", 2) = "as" 736 737 StringUtils.left("asdf", = "asdf" 738 739 43.public static String right(String str, int len) 740 741 得到字符串str从右边数len长度的子串。 742 743 如果str为null或为"",则返回它本身 744 745 如果len小于0,则返回"" 746 747 举例(*表示任意): 748 749 StringUtils.right(null, *) = null 750 751 StringUtils.right(*, -ve) = "" 752 753 StringUtils.right("", *) = "" 754 755 StringUtils.right("asdf", 0) = "" 756 757 StringUtils.right("asdf", 2) = "df" 758 759 StringUtils.right("asdf", = "asdf" 760 761 44.public static String mid(String str, int pos, int len) 762 763 得到字符串str从pos开始len长度的子串。 764 765 如果str为null或为"",则返回它本身 766 767 如果len小于0或pos大于srt的长度,则返回"" 768 769 如果pos小于0,则pos设为0 770 771 举例(*表示任意): 772 773 StringUtils.mid(null, *, *) = null 774 775 StringUtils.mid("", *, *) = "" 776 777 StringUtils.mid(*, *, -ve) = "" 778 779 StringUtils.mid("asdf", 0, 4)) = "asdf" 780 781 StringUtils.mid("asdf", 2, 2)) = "df" 782 783 StringUtils.mid("asdf", 2, 5)) = "df" 784 785 StringUtils.mid("asdf", -2, 1)) = "a" 786 787 StringUtils.mid("asdf", 0, -1)) = "" 788 789 45.public static String substringBefore(String str, String separator) 790 791 得到字符串str的在字符串separator出现前的字串,且separator不包括在内。 792 793 如果str为null或为"",则返回它本身 794 795 如果separator为null,则返回str本身 796 797 举例(*表示任意): 798 799 StringUtils.substringBefore(null, *) = null 800 801 StringUtils.substringBefore("", *) = "" 802 803 StringUtils.substringBefore("asdfg", null)) = "asdfg" 804 805 StringUtils.substringBefore("asdfg", "a")) = "" 806 807 StringUtils.substringBefore("asdfg", "sd")) = "a" 808 809 StringUtils.substringBefore("asdfsag", "sa")) = "asdf" 810 811 StringUtils.substringBefore("asdfg", "h")) = "asdfg" 812 813 StringUtils.substringBefore("asdfg", "")) = "" 814 815 StringUtils.substringBefore("asdfg", "dfgh")) = "asdfg" 816 817 StringUtils.substringBefore("asdfg", "dfg")) = "as" 818 819 StringUtils.substringBefore("abbbabbba", "bb")) = "a" 820 821 46.public static String substringAfter(String str, String separator) 822 823 得到字符串str的在字符串separator出现后的字串,且separator不包括在内。 824 825 如果str为null或为"",则返回它本身 826 827 如果separator为null,则返回"" 828 829 举例(*表示任意): 830 831 StringUtils.substringAfter(null, *) = null 832 833 StringUtils.substringAfter("", *) = "" 834 835 StringUtils.substringAfter(*, null) = "" 836 837 StringUtils.substringAfter("asdfg", "a")) = "sdfg" 838 839 StringUtils.substringAfter("asdfg", "sd")) = "fg" 840 841 StringUtils.substringAfter("asdfsag", "sa")) = "g" 842 843 StringUtils.substringAfter("asdfg", "h")) = "" 844 845 StringUtils.substringAfter("asdfg", "")) = "asdfg" 846 847 StringUtils.substringAfter("asdfg", "dfgh")) = "" 848 849 StringUtils.substringAfter("asdfg", "dfg")) = "" 850 851 StringUtils.substringAfter("abbbabbba", "bb"))= "babbba" 852 853 47.public static String substringBeforeLast(String str, String separator) 854 855 和45类似,得到字符串str的在字符串separator最后一次出现前的字串。 856 857 这里不再举例。 858 859 48.public static String substringAfterLast(String str, String separator) 860 861 和46类似,得到字符串str的在字符串separator最后一次出现后的字串。 862 863 这里不再举例。 864 865 49.public static String substringBetween(String str, String tag) 866 867 得到str中的在两个字符串tag中间的字符串,即str中的tag所夹的串。 868 869 如果str为null或tag为null,返回null 870 871 举例(*表示任意): 872 873 StringUtils.substringBetween(null, *) = null 874 875 StringUtils.substringBetween(*, null) = null 876 877 StringUtils.substringBetween("", "") = "" 878 879 StringUtils.substringBetween("", "a")) = null 880 881 StringUtils.substringBetween("asdfdf", "df")) = "" 882 883 StringUtils.substringBetween("asdfas", "as")) = "df" 884 885 StringUtils.substringBetween("dfasdfasdfas", "df")) = "as" 886 887 StringUtils.substringBetween("dfasdfasdfas", "as")) = "df" 888 889 StringUtils.substringBetween("dfasdfasgdf", "df")) = "as" 890 891 50.public static String substringBetween(String str, String open, String close) 892 893 得到str中的在两个字符串open和close中间的字符串,即open和close所夹的串。 894 895 如果str为null或open为null或close为null,返回null 896 897 举例(*表示任意): 898 899 StringUtils.substringBetween(null, *, *) = null 900 901 StringUtils.substringBetween(*, null, *) = null 902 903 StringUtils.substringBetween(*, *, null) = null 904 905 StringUtils.substringBetween("", "", "") = "" 906 907 StringUtils.substringBetween("", "", "]") = null 908 909 StringUtils.substringBetween("", "[", "]") = null 910 911 StringUtils.substringBetween("[]", "[","]")) = "" 912 913 StringUtils.substringBetween("a[sd]f", "[","]")) = "sd" 914 915 StringUtils.substringBetween("a[sd]f[gh]", "[","]")) = "sd" 916 917 StringUtils.substringBetween("a[sd]f", "]","[")) = null 918 919 StringUtils.substringBetween("a[sd]f", "","")) = "" 920 921 51.public static String[] substringsBetween(String str, String open, String close) 922 923 得到str中的在两个字符串open和close中间的字符串,即open和close所夹的串, 924 925 把所有符合的结果放在数组中返回。 926 927 和50类似,但是返回了所有的结果(50只返回了第一个匹配的结果)。 928 929 这里不再举例。 930 931 932 933 52.public static String[] split(String str) 934 935 把字符串拆分成一个字符串数组,用空白符(whitespace)作为分隔符。 936 937 Whitespace是这样定义的 {@link Character#isWhitespace(char)} 938 939 如果字符串为null,返回null 940 941 如果字符串为"",返回空数组{} 942 943 举例(*表示任意): 944 945 StringUtils.split(null) = null 946 947 StringUtils.split("") = {} 948 949 StringUtils.split("as df yy")) = {"as","df","yy"} 950 951 StringUtils.split(" as df yy ")) = {"as","df","yy"} 952 953 StringUtils.split("as\ndf\ryy")) = {"as","df","yy"} 954 955 StringUtils.split("as\tdf\fyy")) = {"as","df","yy"} 956 957 StringUtils.split("as df \fyy")) = {"as","df","yy"} 958 959 StringUtils.split("as\t \r df \f \n yy")) = {"as","df","yy"} 960 961 StringUtils.split("as")) = {"as"} 962 963 StringUtils.split(" as ")) = {"as"} 964 965 53.public static String[] split(String str, char separatorChar) 966 967 把字符串拆分成一个字符串数组,用指定的字符separatorChar作为分隔符。 968 969 如果字符串为null,返回null 970 971 如果字符串为"",返回空数组{} 972 973 举例(*表示任意): 974 975 StringUtils.split(null, *) = null 976 977 StringUtils.split("", *) = {} 978 979 StringUtils.split("as df yy",' ')) = {"as","df","yy"} 980 981 StringUtils.split(" as df yy ",' ')) = {"as","df","yy"} 982 983 StringUtils.split("asodfoyy",'o')) = {"as","df","yy"} 984 985 StringUtils.split("as.df.yy",'.')) = {"as","df","yy"} 986 987 StringUtils.split("as\ndf\nyy",'\n'))= {"as","df","yy"} 988 989 StringUtils.split("as",' ')) = {"as"} 990 991 StringUtils.split(" as ",' ')) = {"as"} 992 993 54.public static String[] split(String str, String separatorChars) 994 995 把字符串拆分成一个字符串数组,用指定的字符串separatorChars作为分隔符。 996 997 如果字符串str为null,返回null 998 999 如果字符串str为"",返回空数组{} 1000 1001 如果separatorChars为null,则默认为空白符 1002 1003 和53类似。 1004 1005 举例(*表示任意): 1006 1007 StringUtils.split("as \rdf \t yy",null)) = {"as","df","yy"} 1008 1009 StringUtils.split("as\ndf\fyy",null)) = {"as","df","yy"} 1010 1011 StringUtils.split("as","")) = {"as"} 1012 1013 55.public static String[] split(String str, String separatorChars, int max) 1014 1015 把字符串拆分成一个字符串数组,用指定的字符串separatorChars作为分隔符, 1016 1017 数组的最大长度为max。 1018 1019 如果字符串str为null,返回null 1020 1021 如果字符串str为"",返回空数组{} 1022 1023 如果separatorChars为null,则默认为空白符 1024 1025 如果max小于等于0,认为是没有限制 1026 1027 举例(*表示任意): 1028 1029 StringUtils.split(null, *, *) = null 1030 1031 StringUtils.split("", *, *) = {} 1032 1033 StringUtils.split("as df yy",null,0)) = {"as","df","yy"} 1034 1035 StringUtils.split("as df yy"," ",0)) = {"as","df","yy"} 1036 1037 StringUtils.split("as.df.yy",".",-1)) = {"as","df","yy"} 1038 1039 StringUtils.split("as.df.yy",".",4)) = {"as","df","yy"} 1040 1041 StringUtils.split("as-!-df-!-yy","-!-",0)) = {"as","df","yy"} 1042 1043 StringUtils.split("as.df.yy",".",2)) = {"as","df.yy"} 1044 1045 StringUtils.split("as","",0)) = {"as"} 1046 1047 StringUtils.split("as","",2)) = {"as"} 1048 1049 56.public static String[] splitByWholeSeparator(String str, String separator) 1050 1051 个人认为和54功能一样。区别有待发掘。 1052 1053 57.public static String[] splitByWholeSeparator( String str, String separator, int max ) 1054 1055 个人认为和55功能一样。区别有待发掘。 1056 1057 58.public static String[] splitPreserveAllTokens(String str) 1058 1059 把字符串str拆分成一个数组,用空白符(whitespace)作为分隔符,保留所有的标识, 1060 1061 包括相邻分隔符产生的空的标识。它可作为StringTokenizer的一个替代。 1062 1063 Whitespace是这样定义的{@link Character#isWhitespace(char)}。 1064 1065 举例(*表示任意): 1066 1067 StringUtils.splitPreserveAllTokens(null)) = null 1068 1069 StringUtils.splitPreserveAllTokens("")) = {} 1070 1071 StringUtils.splitPreserveAllTokens("as df gh jk")) = {"as","df","gh","jk"} 1072 1073 StringUtils.splitPreserveAllTokens("as\ndf\rgh\fjk")) = {"as","df","gh","jk"} 1074 1075 StringUtils.splitPreserveAllTokens("as\tdf gh jk")) = {"as","df","gh","jk"} 1076 1077 StringUtils.splitPreserveAllTokens("as df gh")) = {"as","","df","gh"} 1078 1079 StringUtils.splitPreserveAllTokens(" as df ")) = {"","as","","","df","",""} 1080 1081 59.public static String[] splitPreserveAllTokens(String str, char separatorChar) 1082 1083 和58类似,只是分隔符为字符separatorChar。 1084 1085 举例(*表示任意): 1086 1087 StringUtils.splitPreserveAllTokens(null, *) = null 1088 1089 StringUtils.splitPreserveAllTokens("", *) = {} 1090 1091 StringUtils.splitPreserveAllTokens("as df gh jk",' ')) = {"as","df","gh","jk"} 1092 1093 StringUtils.splitPreserveAllTokens("as.df.gh.jk",'.')) = {"as","df","gh","jk"} 1094 1095 StringUtils.splitPreserveAllTokens("as..df.gh",'.')) = {"as","","df","gh"} 1096 1097 StringUtils.splitPreserveAllTokens(",as,,,df,,",',')) = {"","as","","","df","",""} 1098 1099 StringUtils.splitPreserveAllTokens("as.df.gh",',')) = {"as.df.gh"} 1100 1101 60.public static String[] splitPreserveAllTokens(String str, String separatorChars) 1102 1103 和59类似,只是分隔符为字符串separatorChars。 1104 1105 举例(*表示任意): 1106 1107 StringUtils.splitPreserveAllTokens(null, *) = null 1108 1109 StringUtils.splitPreserveAllTokens("", *) = {} 1110 1111 StringUtils.splitPreserveAllTokens("as df gh jk",null)) = {"as","df","gh","jk"} 1112 1113 StringUtils.splitPreserveAllTokens("as\ndf\rgh\fjk",null))= {"as","df","gh","jk"} 1114 1115 StringUtils.splitPreserveAllTokens("as df gh jk"," ")) = {"as","df","gh","jk"} 1116 1117 StringUtils.splitPreserveAllTokens("as.df.gh.jk",".")) = {"as","df","gh","jk"} 1118 1119 StringUtils.splitPreserveAllTokens("as..df.gh",".")) = {"as","","df","gh"} 1120 1121 StringUtils.splitPreserveAllTokens(",as,,,df,,",",")) = {"","as","","","df","",""} 1122 1123 StringUtils.splitPreserveAllTokens("as.df.gh",",")) = {"as.df.gh"} 1124 1125 StringUtils.splitPreserveAllTokens("as.df.gh","")) = {"as.df.gh"} 1126 1127 61.public static String[] splitPreserveAllTokens(String str, String separatorChars, int max) 1128 1129 和上面几个类似,只是指定了数组的最大长度。 1130 1131 如果max为0或负数,则认为没有限制。 1132 1133 这里不再举例。 1134 1135 62.public static String join(Object[] array) 1136 1137 把数组中的元素连接成一个字符串返回。 1138 1139 举例(*表示任意): 1140 1141 StringUtils.join(null) = null 1142 1143 StringUtils.join({}) = "" 1144 1145 StringUtils.join({"as","df","gh","jk"})) = "asdfghjk" 1146 1147 StringUtils.join({"as","","df","gh"})) = "asdfgh" 1148 1149 StringUtils.join({"","as","","","df","",""})) = "asdf" 1150 1151 63.public static String join(Object[] array, char separator) 1152 1153 把数组中的元素连接成一个字符串返回,把分隔符separator也加上。 1154 1155 举例(*表示任意): 1156 1157 StringUtils.join(null, *) = null 1158 1159 StringUtils.join({}, *) = "" 1160 1161 StringUtils.join({null}, *) = "" 1162 1163 StringUtils.join({"as","df","gh","jk"},' ')) = "as df gh jk" 1164 1165 StringUtils.join({"as","df","gh","jk"},'.')) = "as.df.gh.jk" 1166 1167 StringUtils.join({"as","","df","gh"},'.')) = "as..df.gh" 1168 1169 StringUtils.join({"","as","","","df","",""},','))= ",as,,,df,," 1170 1171 StringUtils.join({"","as","","","df","",""},' '))= " as df " 1172 1173 StringUtils.join({"as.df.gh"},'.')) = "as.df.gh" 1174 1175 StringUtils.join({"as.df.gh"},' ')) = "as.df.gh" 1176 1177 64.public static String join(Object[] array, char separator, int startIndex, int endIndex) 1178 1179 把数组中的元素连接成一个字符串返回,把分隔符separator也加上。 1180 1181 连接的开始位置为startIndex,结束位置为endIndex。 1182 1183 这里不再举例。 1184 1185 65.public static String join(Object[] array, String separator) 1186 1187 与63类似,这里不再举例。 1188 1189 66.public static String join(Object[] array, String separator, int startIndex, int endIndex) 1190 1191 与64类似,这里不再举例。 1192 1193 1194 1195 67.public static String deleteWhitespace(String str) 1196 1197 删除字符串中的所有空白符(whitespace),空白符是这样定义的{@link Character#isWhitespace(char)}。 1198 1199 举例(*表示任意): 1200 1201 StringUtils.deleteWhitespace(null) = null 1202 1203 StringUtils.deleteWhitespace("") = "" 1204 1205 StringUtils.deleteWhitespace("asd")) = "asd", 1206 1207 StringUtils.deleteWhitespace("as df")) = "asdf" 1208 1209 StringUtils.deleteWhitespace("as\n\r\f\tdf")) = "asdf" 1210 1211 StringUtils.deleteWhitespace("as\bdf")) = "as\bdf" 1212 1213 StringUtils.deleteWhitespace(" as df ")) = "asdf" 1214 1215 68.public static String removeStart(String str, String remove) 1216 1217 如果字符串str是以字符串remove开始,则去掉这个开始,然后返回,否则返回原来的串。 1218 1219 举例(*表示任意): 1220 1221 StringUtils.removeStart(null, *) = null 1222 1223 StringUtils.removeStart("", *) = "" 1224 1225 StringUtils.removeStart(*, null) = * 1226 1227 StringUtils.removeStart("asdf","")) = "asdf" 1228 1229 StringUtils.removeStart("asdf","as")) = "df" 1230 1231 StringUtils.removeStart("asdf","df")) = "asdf" 1232 1233 StringUtils.removeStart("asdf","gh")) = "asdf" 1234 1235 69.public static String removeEnd(String str, String remove) 1236 1237 如果字符串str是以字符串remove结尾,则去掉这个结尾,然后返回,否则返回原来的串。 1238 1239 这里不再举例。 1240 1241 70.public static String remove(String str, String remove) 1242 1243 去掉字符串str中所有包含remove的部分,然后返回。 1244 1245 这里不再举例。 1246 1247 71.public static String remove(String str, char remove) 1248 1249 去掉字符串str中所有包含remove的部分,然后返回。 1250 1251 这里不再举例。 1252 1253 72.public static String replaceOnce(String text, String repl, String with) 1254 1255 在字符串text中用with代替repl,仅一次。 1256 1257 这里不再举例。 1258 1259 73.public static String replace(String text, String repl, String with) 1260 1261 在字符串text中用with代替repl,替换所有。 1262 1263 这里不再举例。 1264 1265 74.public static String replace(String text, String repl, String with, int max) 1266 1267 在字符串text中用with代替repl,max为最大替换次数。 1268 1269 如果max小于0,则替换所有。 1270 1271 这里不再举例。 1272 1273 75. public static String replaceChars(String str, char searchChar, char replaceChar) 1274 1275 在字符串str中用字符replaceChar代替所有字符searchChar, 1276 1277 如果字符串为null或"",则返回它本身。 1278 1279 这里不再举例。 1280 1281 76.public static String replaceChars(String str, String searchChars, String replaceChars) 1282 1283 用replaceChars代替str中的searchChars。 1284 1285 replaceChars的长度应该和searchChars的长度相等, 1286 1287 如果replaceChars的长度大于searchChars的长度,超过长度的字符将被忽略, 1288 1289 如果replaceChars的长度小于searchChars的长度,超过长度的字符将被删除。 1290 1291 举例(*表示任意): 1292 1293 StringUtils.replaceChars(null, *, *) = null 1294 1295 StringUtils.replaceChars("", *, *) = "" 1296 1297 StringUtils.replaceChars("asdf", null, *) = "asdf" 1298 1299 StringUtils.replaceChars("asdf", "", *) = "asdf" 1300 1301 StringUtils.replaceChars("asdf","s",null)) = "adf" 1302 1303 StringUtils.replaceChars("asdf","s","")) = "adf" 1304 1305 StringUtils.replaceChars("asdsfsg","s","y")) = "aydyfyg" 1306 1307 StringUtils.replaceChars("asdf","sd","yy")) = "ayyf" 1308 1309 StringUtils.replaceChars("asdf","sd","yyy")) = "ayyf" 1310 1311 StringUtils.replaceChars("asssdf","s","yyy")) = "ayyydf" 1312 1313 StringUtils.replaceChars("asdf","sd","y")) = "ayf" 1314 1315 StringUtils.replaceChars("assssddddf","sd","y"))= "ayyyyf" 1316 1317 77.public static String overlay(String str, String overlay, int start, int end) 1318 1319 用字符串overlay覆盖字符串str从start到end之间的串。 1320 1321 如果str为null,则返回null 1322 1323 如果start或end小于0,则设为0 1324 1325 如果start大于end,则两者交换 1326 1327 如果start或end大于str的长度,则认为等于str的长度 1328 1329 举例(*表示任意): 1330 1331 StringUtils.overlay(null, *, *, *) = null 1332 1333 StringUtils.overlay("","as",0,0)) = "as" 1334 1335 StringUtils.overlay("asdfgh","qq",2,5)) = "asqqh" 1336 1337 StringUtils.overlay("asdfgh","qq",5,2)) = "asqqh" 1338 1339 StringUtils.overlay("asdfgh","qq",-1,3)) = "qqfgh" 1340 1341 StringUtils.overlay("asdfgh","qq",-1,-3)) = "qqasdfgh" 1342 1343 StringUtils.overlay("asdfgh","qq",7,10)) = "asdfghqq" 1344 1345 StringUtils.overlay("asdfgh","qq",0,8)) = "qq" 1346 1347 StringUtils.overlay("asdfgh","qq",2,8)) = "asqq" 1348 1349 StringUtils.overlay("asdfgh",null,2,5)) = "ash" 1350 1351 StringUtils.overlay("asdfgh","",2,5)) = "ash" 1352 1353 78.public static String chop(String str) 1354 1355 去掉字符串str的最后一个字符。 1356 1357 如果字符串以"\r\n"结尾,则去掉它们。 1358 1359 这里不再举例。 1360 1361 79.public static String repeat(String str, int repeat) 1362 1363 重复字符串repeat次,组合成一个新串返回。 1364 1365 如果字符串str为null或"",则返回它本身 1366 1367 如果repeat小于0,则返回"" 1368 1369 举例(*表示任意): 1370 1371 StringUtils.repeat(null, *) = null 1372 1373 StringUtils.repeat("", *) = "" 1374 1375 StringUtils.repeat("a", 3) = "aaa" 1376 1377 StringUtils.repeat("ab", 2) = "abab" 1378 1379 StringUtils.repeat("a", -2) = "" 1380 1381 80.public static String rightPad(String str, int size) 1382 1383 如果str为null,则返回null 1384 1385 如果字符串长度小于size,则在右边补空格使其长度等于size,然后返回 1386 1387 如果字符串长度大于等于size,则返回它本身 1388 1389 这里不再举例。 1390 1391 81.public static String rightPad(String str, int size, char padChar) 1392 1393 和80类似,只是补的字符为padChar。 1394 1395 这里不再举例。 1396 1397 82.public static String rightPad(String str, int size, String padStr) 1398 1399 和80类似,只是补的是字符串padStr。 1400 1401 举例(*表示任意): 1402 1403 StringUtils.rightPad(null, *, *) = null 1404 1405 StringUtils.rightPad("",0,"")) = "" 1406 1407 StringUtils.rightPad("",3,"")) = " " 1408 1409 StringUtils.rightPad("",3,"a")) = "aaa" 1410 1411 StringUtils.rightPad("",2,"as")) = "as" 1412 1413 StringUtils.rightPad("as",-1,"df")) = "as" 1414 1415 StringUtils.rightPad("as",0,"df")) = "as" 1416 1417 StringUtils.rightPad("as",3,"df")) = "asd" 1418 1419 StringUtils.rightPad("as",8,"df")) = "asdfdfdf" 1420 1421 StringUtils.rightPad("as",5,null)) = "as " 1422 1423 StringUtils.rightPad("as",5,"")) = "as " 1424 1425 83.public static String leftPad(String str, int size) 1426 1427 和80类似,只是补左边。 1428 1429 这里不再举例。 1430 1431 84.public static String leftPad(String str, int size, char padChar) 1432 1433 和81类似。 1434 1435 这里不再举例。 1436 1437 85.public static String leftPad(String str, int size, String padStr) 1438 1439 和82类似。 1440 1441 这里不再举例。 1442 1443 86.public static String center(String str, int size) 1444 1445 产生一个字符串返回,该字符串长度等于size,str位于新串的中心,其他位置补空格。 1446 1447 如果str为null,则返回null 1448 1449 如果size小于str的长度,则返回str本身 1450 1451 举例(*表示任意): 1452 1453 StringUtils.center(null, *) = null 1454 1455 StringUtils.center("",1)) = " " 1456 1457 StringUtils.center("",2)) = " " 1458 1459 StringUtils.center("as",-1)) = "as" 1460 1461 StringUtils.center("as",2)) = "as" 1462 1463 StringUtils.center("as",3)) = "as " 1464 1465 StringUtils.center("as",4)) = " as " 1466 1467 StringUtils.center("as",10)) = " as " 1468 1469 87.public static String center(String str, int size, char padChar) 1470 1471 和86类似,只是其他位置补padChar。 1472 1473 这里不再举例。 1474 1475 88.public static String center(String str, int size, String padStr) 1476 1477 和86类似,只是其他位置补padStr。 1478 1479 这里不再举例。 1480 1481 89.public static String swapCase(String str) 1482 1483 把字符串中的字符大写转换为小写,小写转换为大写。 1484 1485 举例: 1486 1487 StringUtils.swapCase(null) = null 1488 1489 StringUtils.swapCase("") = "" 1490 1491 StringUtils.swapCase("Hello Boys")) = "hELLO bOYS" 1492 1493 StringUtils.swapCase("I am 11")) = "i AM 11" 1494 1495 90.public static int countMatches(String str, String sub) 1496 1497 计算字符串sub在字符串str中出现的次数。 1498 1499 如果str为null或"",则返回0 1500 1501 举例(*表示任意): 1502 1503 StringUtils.countMatches(null, *) = 0 1504 1505 StringUtils.countMatches("", *) = 0 1506 1507 StringUtils.countMatches("asdf","as")) = 1 1508 1509 StringUtils.countMatches("asdfas","as")) = 2 1510 1511 StringUtils.countMatches("dfgh","as")) = 0 1512 1513 StringUtils.countMatches("as","")) = 0 1514 1515 StringUtils.countMatches("as",null)) = 0
StringUtils 使用教程
猜你喜欢
转载自www.cnblogs.com/ihong/p/12036631.html
今日推荐
周排行