java pair工具类_java 常用工具类的使用<二>

1 packagecom.mkyong.common;2

3 importjava.util.ArrayList;4 importjava.util.List;5

6 /**

7 *8 * String工具类.
9 *10 *@author宋立君11 * @date 2014年06月24日12 */

13 public classStringUtil {14

15 private static final int INDEX_NOT_FOUND = -1;16 private static final String EMPTY = "";17 /**

18 *

19 * The maximum size to which the padding constant(s) can expand.20 *

21 */

22 private static final int PAD_LIMIT = 8192;23

24 /**

25 * 功能:将半角的符号转换成全角符号.(即英文字符转中文字符)26 *27 *@author宋立君28 *@paramstr29 * 源字符串30 *@returnString31 * @date 2014年06月24日32 */

33 public staticString changeToFull(String str) {34 String source = "1234567890!@#$%^&*()abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_=+\\|[];:'\",<.>/?";35 String[] decode = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0",36 "!", "@", "#", "$", "%", "︿", "&", "*", "(", ")", "a", "b",37 "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",38 "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",39 "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",40 "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X",41 "Y", "Z", "-", "_", "=", "+", "\", "|", "【", "】", ";", ":",42 "'", "\"", ",", "〈", "。", "〉", "/", "?"};43 String result = "";44 for (int i = 0; i < str.length(); i++) {45 int pos =source.indexOf(str.charAt(i));46 if (pos != -1) {47 result +=decode[pos];48 } else{49 result +=str.charAt(i);50 }51 }52 returnresult;53 }54

55 /**

56 * 功能:cs串中是否一个都不包含字符数组searchChars中的字符。57 *58 *@author宋立君59 *@paramcs60 * 字符串61 *@paramsearchChars62 * 字符数组63 *@returnboolean 都不包含返回true,否则返回false。64 * @date 2014年06月24日65 */

66 public static boolean containsNone(CharSequence cs, char... searchChars) {67 if (cs == null || searchChars == null) {68 return true;69 }70 int csLen =cs.length();71 int csLast = csLen - 1;72 int searchLen =searchChars.length;73 int searchLast = searchLen - 1;74 for (int i = 0; i < csLen; i++) {75 char ch =cs.charAt(i);76 for (int j = 0; j < searchLen; j++) {77 if (searchChars[j] ==ch) {78 if(Character.isHighSurrogate(ch)) {79 if (j ==searchLast) {80 //missing low surrogate, fine, like81 //String.indexOf(String)

82 return false;83 }84 if (i

90 return false;91 }92 }93 }94 }95 return true;96 }97

98 /**

99 *

100 * 编码为Unicode,格式 '\u0020'.101 *

102 *103 *@author宋立君104 *105 *
106 *   CharUtils.unicodeEscaped(' ') = "\u0020"107 *   CharUtils.unicodeEscaped('A') = "\u0041"108 * 
109 *110 *@paramch111 * 源字符串112 *@return转码后的字符串113 * @date 2014年06月24日114 */

115 public static String unicodeEscaped(charch) {116 if (ch < 0x10) {117 return "\\u000" +Integer.toHexString(ch);118 } else if (ch < 0x100) {119 return "\\u00" +Integer.toHexString(ch);120 } else if (ch < 0x1000) {121 return "\\u0" +Integer.toHexString(ch);122 }123 return "\\u" +Integer.toHexString(ch);124 }125

126 /**

127 *

128 * 进行tostring操作,如果传入的是null,返回空字符串。129 *

130 *131 *
132 * ObjectUtils.toString(null)         = ""133 * ObjectUtils.toString("")           = ""134 * ObjectUtils.toString("bat")        = "bat"135 * ObjectUtils.toString(Boolean.TRUE) = "true"136 * 
137 *138 *@paramobj139 * 源140 *@returnString141 */

142 public staticString toString(Object obj) {143 return obj == null ? "": obj.toString();144 }145

146 /**

147 *

148 * 进行tostring操作,如果传入的是null,返回指定的默认值。149 *

150 *151 *
152 * ObjectUtils.toString(null, null)           = null153 * ObjectUtils.toString(null, "null")         = "null"154 * ObjectUtils.toString("", "null")           = ""155 * ObjectUtils.toString("bat", "null")        = "bat"156 * ObjectUtils.toString(Boolean.TRUE, "null") = "true"157 * 
158 *159 *@paramobj160 * 源161 *@paramnullStr162 * 如果obj为null时返回这个指定值163 *@returnString164 */

165 public staticString toString(Object obj, String nullStr) {166 return obj == null ?nullStr : obj.toString();167 }168

169 /**

170 *

171 * 只从源字符串中移除指定开头子字符串.172 *

173 *174 *
175 * StringUtil.removeStart(null, *)      = null176 * StringUtil.removeStart("", *)        = ""177 * StringUtil.removeStart(*, null)      = *178 * StringUtil.removeStart("www.domain.com", "www.")   = "domain.com"179 * StringUtil.removeStart("domain.com", "www.")       = "domain.com"180 * StringUtil.removeStart("www.domain.com", "domain") = "www.domain.com"181 * StringUtil.removeStart("abc", "")    = "abc"182 * 
183 *184 *@paramstr185 * 源字符串186 *@paramremove187 * 将要被移除的子字符串188 *@returnString189 */

190 public staticString removeStart(String str, String remove) {191 if (isEmpty(str) ||isEmpty(remove)) {192 returnstr;193 }194 if(str.startsWith(remove)) {195 returnstr.substring(remove.length());196 }197 returnstr;198 }199

200 /**

201 *

202 * 只从源字符串中移除指定结尾的子字符串.203 *

204 *205 *
206 * StringUtil.removeEnd(null, *)      = null207 * StringUtil.removeEnd("", *)        = ""208 * StringUtil.removeEnd(*, null)      = *209 * StringUtil.removeEnd("www.domain.com", ".com.")  = "www.domain.com"210 * StringUtil.removeEnd("www.domain.com", ".com")   = "www.domain"211 * StringUtil.removeEnd("www.domain.com", "domain") = "www.domain.com"212 * StringUtil.removeEnd("abc", "")    = "abc"213 * 
214 *215 *@paramstr216 * 源字符串217 *@paramremove218 * 将要被移除的子字符串219 *@returnString220 */

221 public staticString removeEnd(String str, String remove) {222 if (isEmpty(str) ||isEmpty(remove)) {223 returnstr;224 }225 if(str.endsWith(remove)) {226 return str.substring(0, str.length() -remove.length());227 }228 returnstr;229 }230

231 /**

232 *

233 * 将一个字符串重复N次234 *

235 *236 *
237 * StringUtil.repeat(null, 2) = null238 * StringUtil.repeat("", 0)   = ""239 * StringUtil.repeat("", 2)   = ""240 * StringUtil.repeat("a", 3)  = "aaa"241 * StringUtil.repeat("ab", 2) = "abab"242 * StringUtil.repeat("a", -2) = ""243 * 
244 *245 *@paramstr246 * 源字符串247 *@paramrepeat248 * 重复的次数249 *@returnString250 */

251 public static String repeat(String str, intrepeat) {252 //Performance tuned for 2.0 (JDK1.4)

253

254 if (str == null) {255 return null;256 }257 if (repeat <= 0) {258 returnEMPTY;259 }260 int inputLength =str.length();261 if (repeat == 1 || inputLength == 0) {262 returnstr;263 }264 if (inputLength == 1 && repeat <=PAD_LIMIT) {265 return repeat(str.charAt(0), repeat);266 }267

268 int outputLength = inputLength *repeat;269 switch(inputLength) {270 case 1:271 return repeat(str.charAt(0), repeat);272 case 2:273 char ch0 = str.charAt(0);274 char ch1 = str.charAt(1);275 char[] output2 = new char[outputLength];276 for (int i = repeat * 2 - 2; i >= 0; i--, i--) {277 output2[i] =ch0;278 output2[i + 1] =ch1;279 }280 return newString(output2);281 default:282 StringBuilder buf = newStringBuilder(outputLength);283 for (int i = 0; i < repeat; i++) {284 buf.append(str);285 }286 returnbuf.toString();287 }288 }289

290 /**

291 *

292 * 将一个字符串重复N次,并且中间加上指定的分隔符293 *

294 *295 *
296 * StringUtil.repeat(null, null, 2) = null297 * StringUtil.repeat(null, "x", 2)  = null298 * StringUtil.repeat("", null, 0)   = ""299 * StringUtil.repeat("", "", 2)     = ""300 * StringUtil.repeat("", "x", 3)    = "xxx"301 * StringUtil.repeat("?", ", ", 3)  = "?, ?, ?"302 * 
303 *304 *@paramstr305 * 源字符串306 *@paramseparator307 * 分隔符308 *@paramrepeat309 * 重复次数310 *@returnString311 */

312 public static String repeat(String str, String separator, intrepeat) {313 if (str == null || separator == null) {314 returnrepeat(str, repeat);315 } else{316 //given that repeat(String, int) is quite optimized, better to rely317 //on it than try and splice this into it

318 String result = repeat(str +separator, repeat);319 returnremoveEnd(result, separator);320 }321 }322

323 /**

324 *

325 * 将某个字符重复N次.326 *

327 *328 *@paramch329 * 某个字符330 *@paramrepeat331 * 重复次数332 *@returnString333 */

334 public static String repeat(char ch, intrepeat) {335 char[] buf = new char[repeat];336 for (int i = repeat - 1; i >= 0; i--) {337 buf[i] =ch;338 }339 return newString(buf);340 }341

342 /**

343 *

344 * 字符串长度达不到指定长度时,在字符串右边补指定的字符.345 *

346 *347 *
348 * StringUtil.rightPad(null, *, *)     = null349 * StringUtil.rightPad("", 3, 'z')     = "zzz"350 * StringUtil.rightPad("bat", 3, 'z')  = "bat"351 * StringUtil.rightPad("bat", 5, 'z')  = "batzz"352 * StringUtil.rightPad("bat", 1, 'z')  = "bat"353 * StringUtil.rightPad("bat", -1, 'z') = "bat"354 * 
355 *356 *@paramstr357 * 源字符串358 *@paramsize359 * 指定的长度360 *@parampadChar361 * 进行补充的字符362 *@returnString363 */

364 public static String rightPad(String str, int size, charpadChar) {365 if (str == null) {366 return null;367 }368 int pads = size -str.length();369 if (pads <= 0) {370 return str; //returns original String when possible

371 }372 if (pads >PAD_LIMIT) {373 returnrightPad(str, size, String.valueOf(padChar));374 }375 returnstr.concat(repeat(padChar, pads));376 }377

378 /**

379 *

380 * 扩大字符串长度,从左边补充指定字符381 *

382 *383 *
384 * StringUtil.rightPad(null, *, *)      = null385 * StringUtil.rightPad("", 3, "z")      = "zzz"386 * StringUtil.rightPad("bat", 3, "yz")  = "bat"387 * StringUtil.rightPad("bat", 5, "yz")  = "batyz"388 * StringUtil.rightPad("bat", 8, "yz")  = "batyzyzy"389 * StringUtil.rightPad("bat", 1, "yz")  = "bat"390 * StringUtil.rightPad("bat", -1, "yz") = "bat"391 * StringUtil.rightPad("bat", 5, null)  = "bat  "392 * StringUtil.rightPad("bat", 5, "")    = "bat  "393 * 
394 *395 *@paramstr396 * 源字符串397 *@paramsize398 * 扩大后的长度399 *@parampadStr400 * 在右边补充的字符串401 *@returnString402 */

403 public static String rightPad(String str, intsize, String padStr) {404 if (str == null) {405 return null;406 }407 if(isEmpty(padStr)) {408 padStr = " ";409 }410 int padLen =padStr.length();411 int strLen =str.length();412 int pads = size -strLen;413 if (pads <= 0) {414 return str; //returns original String when possible

415 }416 if (padLen == 1 && pads <=PAD_LIMIT) {417 return rightPad(str, size, padStr.charAt(0));418 }419

420 if (pads ==padLen) {421 returnstr.concat(padStr);422 } else if (pads

434 /**

435 *

436 * 扩大字符串长度,从左边补充空格437 *

438 *439 *
440 * StringUtil.leftPad(null, *)   = null441 * StringUtil.leftPad("", 3)     = "   "442 * StringUtil.leftPad("bat", 3)  = "bat"443 * StringUtil.leftPad("bat", 5)  = "  bat"444 * StringUtil.leftPad("bat", 1)  = "bat"445 * StringUtil.leftPad("bat", -1) = "bat"446 * 
447 *448 *@paramstr449 * 源字符串450 *@paramsize451 * 扩大后的长度452 *@returnString453 */

454 public static String leftPad(String str, intsize) {455 return leftPad(str, size, ' ');456 }457

458 /**

459 *

460 * 扩大字符串长度,从左边补充指定的字符461 *

462 *463 *
464 * StringUtil.leftPad(null, *, *)     = null465 * StringUtil.leftPad("", 3, 'z')     = "zzz"466 * StringUtil.leftPad("bat", 3, 'z')  = "bat"467 * StringUtil.leftPad("bat", 5, 'z')  = "zzbat"468 * StringUtil.leftPad("bat", 1, 'z')  = "bat"469 * StringUtil.leftPad("bat", -1, 'z') = "bat"470 * 
471 *472 *@paramstr473 * 源字符串474 *@paramsize475 * 扩大后的长度476 *@parampadStr477 * 补充的字符478 *@returnString479 */

480 public static String leftPad(String str, int size, charpadChar) {481 if (str == null) {482 return null;483 }484 int pads = size -str.length();485 if (pads <= 0) {486 return str; //returns original String when possible

487 }488 if (pads >PAD_LIMIT) {489 returnleftPad(str, size, String.valueOf(padChar));490 }491 returnrepeat(padChar, pads).concat(str);492 }493

494 /**

495 *

496 * 扩大字符串长度,从左边补充指定的字符497 *

498 *499 *
500 * StringUtil.leftPad(null, *, *)      = null501 * StringUtil.leftPad("", 3, "z")      = "zzz"502 * StringUtil.leftPad("bat", 3, "yz")  = "bat"503 * StringUtil.leftPad("bat", 5, "yz")  = "yzbat"504 * StringUtil.leftPad("bat", 8, "yz")  = "yzyzybat"505 * StringUtil.leftPad("bat", 1, "yz")  = "bat"506 * StringUtil.leftPad("bat", -1, "yz") = "bat"507 * StringUtil.leftPad("bat", 5, null)  = "  bat"508 * StringUtil.leftPad("bat", 5, "")    = "  bat"509 * 
510 *511 *@paramstr512 * 源字符串513 *@paramsize514 * 扩大后的长度515 *@parampadStr516 * 补充的字符串517 *@returnString518 */

519 public static String leftPad(String str, intsize, String padStr) {520 if (str == null) {521 return null;522 }523 if(isEmpty(padStr)) {524 padStr = " ";525 }526 int padLen =padStr.length();527 int strLen =str.length();528 int pads = size -strLen;529 if (pads <= 0) {530 return str; //returns original String when possible

531 }532 if (padLen == 1 && pads <=PAD_LIMIT) {533 return leftPad(str, size, padStr.charAt(0));534 }535

536 if (pads ==padLen) {537 returnpadStr.concat(str);538 } else if (pads

550 /**

551 *

552 * 扩大字符串长度并将现在的字符串居中,被扩大部分用空格填充。553 *

554 *555 *

556 * StringUtil.center(null, *)   = null557 * StringUtil.center("", 4)     = "    "558 * StringUtil.center("ab", -1)  = "ab"559 * StringUtil.center("ab", 4)   = " ab "560 * StringUtil.center("abcd", 2) = "abcd"561 * StringUtil.center("a", 4)    = " a  "562 * 
563 *564 *@paramstr565 * 源字符串566 *@paramsize567 * 扩大后的长度568 *@returnString569 */

570 public static String center(String str, intsize) {571 return center(str, size, ' ');572 }573

574 /**

575 *

576 * 将字符串长度修改为指定长度,并进行居中显示。577 *

578 *579 *
580 * StringUtil.center(null, *, *)     = null581 * StringUtil.center("", 4, ' ')     = "    "582 * StringUtil.center("ab", -1, ' ')  = "ab"583 * StringUtil.center("ab", 4, ' ')   = " ab"584 * StringUtil.center("abcd", 2, ' ') = "abcd"585 * StringUtil.center("a", 4, ' ')    = " a  "586 * StringUtil.center("a", 4, 'y')    = "yayy"587 * 
588 *589 *@paramstr590 * 源字符串591 *@paramsize592 * 指定的长度593 *@parampadStr594 * 长度不够时补充的字符串595 *@returnString596 *@throwsIllegalArgumentException597 * 如果被补充字符串为 null或者 empty598 */

599 public static String center(String str, int size, charpadChar) {600 if (str == null || size <= 0) {601 returnstr;602 }603 int strLen =str.length();604 int pads = size -strLen;605 if (pads <= 0) {606 returnstr;607 }608 str = leftPad(str, strLen + pads / 2, padChar);609 str =rightPad(str, size, padChar);610 returnstr;611 }612

613 /**

614 *

615 * 将字符串长度修改为指定长度,并进行居中显示。616 *

617 *618 *
619 * StringUtil.center(null, *, *)     = null620 * StringUtil.center("", 4, " ")     = "    "621 * StringUtil.center("ab", -1, " ")  = "ab"622 * StringUtil.center("ab", 4, " ")   = " ab"623 * StringUtil.center("abcd", 2, " ") = "abcd"624 * StringUtil.center("a", 4, " ")    = " a  "625 * StringUtil.center("a", 4, "yz")   = "yayz"626 * StringUtil.center("abc", 7, null) = "  abc  "627 * StringUtil.center("abc", 7, "")   = "  abc  "628 * 
629 *630 *@paramstr631 * 源字符串632 *@paramsize633 * 指定的长度634 *@parampadStr635 * 长度不够时补充的字符串636 *@returnString637 *@throwsIllegalArgumentException638 * 如果被补充字符串为 null或者 empty639 */

640 public static String center(String str, intsize, String padStr) {641 if (str == null || size <= 0) {642 returnstr;643 }644 if(isEmpty(padStr)) {645 padStr = " ";646 }647 int strLen =str.length();648 int pads = size -strLen;649 if (pads <= 0) {650 returnstr;651 }652 str = leftPad(str, strLen + pads / 2, padStr);653 str =rightPad(str, size, padStr);654 returnstr;655 }656

657 /**

658 *

659 * 检查字符串是否全部为小写.660 *

661 *662 *
663 * StringUtil.isAllLowerCase(null)   = false664 * StringUtil.isAllLowerCase("")     = false665 * StringUtil.isAllLowerCase("  ")   = false666 * StringUtil.isAllLowerCase("abc")  = true667 * StringUtil.isAllLowerCase("abC") = false668 * 
669 *670 *@paramcs671 * 源字符串672 *@returnString673 */

674 public static booleanisAllLowerCase(String cs) {675 if (cs == null ||isEmpty(cs)) {676 return false;677 }678 int sz =cs.length();679 for (int i = 0; i < sz; i++) {680 if (Character.isLowerCase(cs.charAt(i)) == false) {681 return false;682 }683 }684 return true;685 }686

687 /**

688 *

689 * 检查是否都是大写.690 *

691 *692 *
693 * StringUtil.isAllUpperCase(null)   = false694 * StringUtil.isAllUpperCase("")     = false695 * StringUtil.isAllUpperCase("  ")   = false696 * StringUtil.isAllUpperCase("ABC")  = true697 * StringUtil.isAllUpperCase("aBC") = false698 * 
699 *700 *@paramcs701 * 源字符串702 *@returnString703 */

704 public static booleanisAllUpperCase(String cs) {705 if (cs == null ||StringUtil.isEmpty(cs)) {706 return false;707 }708 int sz =cs.length();709 for (int i = 0; i < sz; i++) {710 if (Character.isUpperCase(cs.charAt(i)) == false) {711 return false;712 }713 }714 return true;715 }716

717 /**

718 *

719 * 反转字符串.720 *

721 *722 *
723 * StringUtil.reverse(null)  = null724 * StringUtil.reverse("")    = ""725 * StringUtil.reverse("bat") = "tab"726 * 
727 *728 *@paramstr729 * 源字符串730 *@returnString731 */

732 public staticString reverse(String str) {733 if (str == null) {734 return null;735 }736 return newStringBuilder(str).reverse().toString();737 }738

739 /**

740 *

741 * 字符串达不到一定长度时在右边补空白.742 *

743 *744 *
745 * StringUtil.rightPad(null, *)   = null746 * StringUtil.rightPad("", 3)     = "   "747 * StringUtil.rightPad("bat", 3)  = "bat"748 * StringUtil.rightPad("bat", 5)  = "bat  "749 * StringUtil.rightPad("bat", 1)  = "bat"750 * StringUtil.rightPad("bat", -1) = "bat"751 * 
752 *753 *@paramstr754 * 源字符串755 *@paramsize756 * 指定的长度757 *@returnString758 */

759 public static String rightPad(String str, intsize) {760 return rightPad(str, size, ' ');761 }762

763 /**

764 * 从右边截取字符串.

765 *766 *
767 * StringUtil.right(null, *)    = null768 * StringUtil.right(*, -ve)     = ""769 * StringUtil.right("", *)      = ""770 * StringUtil.right("abc", 0)   = ""771 * StringUtil.right("abc", 2)   = "bc"772 * StringUtil.right("abc", 4)   = "abc"773 * 
774 *775 *@paramstr776 * 源字符串777 *@paramlen778 * 长度779 *@returnString780 */

781 public static String right(String str, intlen) {782 if (str == null) {783 return null;784 }785 if (len < 0) {786 returnEMPTY;787 }788 if (str.length() <=len) {789 returnstr;790 }791 return str.substring(str.length() -len);792 }793

794 /**

795 *

796 * 截取一个字符串的前几个.797 *

798 *799 *
800 * StringUtil.left(null, *)    = null801 * StringUtil.left(*, -ve)     = ""802 * StringUtil.left("", *)      = ""803 * StringUtil.left("abc", 0)   = ""804 * StringUtil.left("abc", 2)   = "ab"805 * StringUtil.left("abc", 4)   = "abc"806 * 
807 *808 *@paramstr809 * 源字符串810 *@paramlen811 * 截取的长度812 *@returnthe String813 */

814 public static String left(String str, intlen) {815 if (str == null) {816 return null;817 }818 if (len < 0) {819 returnEMPTY;820 }821 if (str.length() <=len) {822 returnstr;823 }824 return str.substring(0, len);825 }826

827 /**

828 *

829 * 得到tag字符串中间的子字符串,只返回第一个匹配项。830 *

831 *832 *
833 * StringUtil.substringBetween(null, *)            = null834 * StringUtil.substringBetween("", "")             = ""835 * StringUtil.substringBetween("", "tag")          = null836 * StringUtil.substringBetween("tagabctag", null)  = null837 * StringUtil.substringBetween("tagabctag", "")    = ""838 * StringUtil.substringBetween("tagabctag", "tag") = "abc"839 * 
840 *841 *@paramstr842 * 源字符串。843 *@paramtag844 * 标识字符串。845 *@returnString 子字符串, 如果没有符合要求的,返回{@codenull}。846 */

847 public staticString substringBetween(String str, String tag) {848 returnsubstringBetween(str, tag, tag);849 }850

851 /**

852 *

853 * 得到两个字符串中间的子字符串,只返回第一个匹配项。854 *

855 *856 *
857 * StringUtil.substringBetween("wx[b]yz", "[", "]") = "b"858 * StringUtil.substringBetween(null, *, *)          = null859 * StringUtil.substringBetween(*, null, *)          = null860 * StringUtil.substringBetween(*, *, null)          = null861 * StringUtil.substringBetween("", "", "")          = ""862 * StringUtil.substringBetween("", "", "]")         = null863 * StringUtil.substringBetween("", "[", "]")        = null864 * StringUtil.substringBetween("yabcz", "", "")     = ""865 * StringUtil.substringBetween("yabcz", "y", "z")   = "abc"866 * StringUtil.substringBetween("yabczyabcz", "y", "z")   = "abc"867 * 
868 *869 *@paramstr870 * 源字符串871 *@paramopen872 * 起字符串。873 *@paramclose874 * 末字符串。875 *@returnString 子字符串, 如果没有符合要求的,返回{@codenull}。876 */

877 public staticString substringBetween(String str, String open, String close) {878 if (str == null || open == null || close == null) {879 return null;880 }881 int start =str.indexOf(open);882 if (start !=INDEX_NOT_FOUND) {883 int end = str.indexOf(close, start +open.length());884 if (end !=INDEX_NOT_FOUND) {885 return str.substring(start +open.length(), end);886 }887 }888 return null;889 }890

891 /**

892 *

893 * 得到两个字符串中间的子字符串,所有匹配项组合为数组并返回。894 *

895 *896 *
897 * StringUtil.substringsBetween("[a][b][c]", "[", "]") = ["a","b","c"]898 * StringUtil.substringsBetween(null, *, *)            = null899 * StringUtil.substringsBetween(*, null, *)            = null900 * StringUtil.substringsBetween(*, *, null)            = null901 * StringUtil.substringsBetween("", "[", "]")          = []902 * 
903 *904 *@paramstr905 * 源字符串906 *@paramopen907 * 起字符串。908 *@paramclose909 * 末字符串。910 *@returnString 子字符串数组, 如果没有符合要求的,返回{@codenull}。911 */

912 public staticString[] substringsBetween(String str, String open,913 String close) {914 if (str == null || isEmpty(open) ||isEmpty(close)) {915 return null;916 }917 int strLen =str.length();918 if (strLen == 0) {919 return new String[0];920 }921 int closeLen =close.length();922 int openLen =open.length();923 List list = new ArrayList();924 int pos = 0;925 while (pos < strLen -closeLen) {926 int start =str.indexOf(open, pos);927 if (start < 0) {928 break;929 }930 start +=openLen;931 int end =str.indexOf(close, start);932 if (end < 0) {933 break;934 }935 list.add(str.substring(start, end));936 pos = end +closeLen;937 }938 if(list.isEmpty()) {939 return null;940 }941 return list.toArray(newString[list.size()]);942 }943

944 /**

945 * 功能:切换字符串中的所有字母大小写。
946 *947 *

948 * StringUtil.swapCase(null)                 = null949 * StringUtil.swapCase("")                   = ""950 * StringUtil.swapCase("The dog has a BONE") = "tHE DOG HAS A bone"951 * 
952 *953 *954 *@paramstr955 * 源字符串956 *@returnString957 */

958 public staticString swapCase(String str) {959 if(StringUtil.isEmpty(str)) {960 returnstr;961 }962 char[] buffer =str.toCharArray();963

964 boolean whitespace = true;965

966 for (int i = 0; i < buffer.length; i++) {967 char ch =buffer[i];968 if(Character.isUpperCase(ch)) {969 buffer[i] =Character.toLowerCase(ch);970 whitespace = false;971 } else if(Character.isTitleCase(ch)) {972 buffer[i] =Character.toLowerCase(ch);973 whitespace = false;974 } else if(Character.isLowerCase(ch)) {975 if(whitespace) {976 buffer[i] =Character.toTitleCase(ch);977 whitespace = false;978 } else{979 buffer[i] =Character.toUpperCase(ch);980 }981 } else{982 whitespace =Character.isWhitespace(ch);983 }984 }985 return newString(buffer);986 }987

988 /**

989 * 功能:截取出最后一个标志位之后的字符串.
990 * 如果sourceStr为empty或者expr为null,直接返回源字符串。
991 * 如果expr长度为0,直接返回sourceStr。
992 * 如果expr在sourceStr中不存在,直接返回sourceStr。
993 *994 *@author宋立君995 * @date 2014年06月24日996 *@paramsourceStr997 * 被截取的字符串998 *@paramexpr999 * 分隔符1000 *@returnString1001 */

1002 public staticString substringAfterLast(String sourceStr, String expr) {1003 if (isEmpty(sourceStr) || expr == null) {1004 returnsourceStr;1005 }1006 if (expr.length() == 0) {1007 returnsourceStr;1008 }1009

1010 int pos =sourceStr.lastIndexOf(expr);1011 if (pos == -1) {1012 returnsourceStr;1013 }1014 return sourceStr.substring(pos +expr.length());1015 }1016

1017 /**

1018 * 功能:截取出最后一个标志位之前的字符串.
1019 * 如果sourceStr为empty或者expr为null,直接返回源字符串。
1020 * 如果expr长度为0,直接返回sourceStr。
1021 * 如果expr在sourceStr中不存在,直接返回sourceStr。
1022 *1023 *@author宋立君1024 * @date 2014年06月24日1025 *@paramsourceStr1026 * 被截取的字符串1027 *@paramexpr1028 * 分隔符1029 *@returnString1030 */

1031 public staticString substringBeforeLast(String sourceStr, String expr) {1032 if (isEmpty(sourceStr) || expr == null) {1033 returnsourceStr;1034 }1035 if (expr.length() == 0) {1036 returnsourceStr;1037 }1038 int pos =sourceStr.lastIndexOf(expr);1039 if (pos == -1) {1040 returnsourceStr;1041 }1042 return sourceStr.substring(0, pos);1043 }1044

1045 /**

1046 * 功能:截取出第一个标志位之后的字符串.
1047 * 如果sourceStr为empty或者expr为null,直接返回源字符串。
1048 * 如果expr长度为0,直接返回sourceStr。
1049 * 如果expr在sourceStr中不存在,直接返回sourceStr。
1050 *1051 *@author宋立君1052 * @date 2014年06月24日1053 *@paramsourceStr1054 * 被截取的字符串1055 *@paramexpr1056 * 分隔符1057 *@returnString1058 */

1059 public staticString substringAfter(String sourceStr, String expr) {1060 if (isEmpty(sourceStr) || expr == null) {1061 returnsourceStr;1062 }1063 if (expr.length() == 0) {1064 returnsourceStr;1065 }1066

1067 int pos =sourceStr.indexOf(expr);1068 if (pos == -1) {1069 returnsourceStr;1070 }1071 return sourceStr.substring(pos +expr.length());1072 }1073

1074 /**

1075 * 功能:截取出第一个标志位之前的字符串.
1076 * 如果sourceStr为empty或者expr为null,直接返回源字符串。
1077 * 如果expr长度为0,直接返回sourceStr。
1078 * 如果expr在sourceStr中不存在,直接返回sourceStr。
1079 * 如果expr在sourceStr中存在不止一个,以第一个位置为准。1080 *1081 *@author宋立君1082 * @date 2014年06月24日1083 *@paramsourceStr1084 * 被截取的字符串1085 *@paramexpr1086 * 分隔符1087 *@returnString1088 */

1089 public staticString substringBefore(String sourceStr, String expr) {1090 if (isEmpty(sourceStr) || expr == null) {1091 returnsourceStr;1092 }1093 if (expr.length() == 0) {1094 returnsourceStr;1095 }1096 int pos =sourceStr.indexOf(expr);1097 if (pos == -1) {1098 returnsourceStr;1099 }1100 return sourceStr.substring(0, pos);1101 }1102

1103 /**

1104 * 功能:检查这个字符串是不是空字符串。
1105 * 如果这个字符串为null或者trim后为空字符串则返回true,否则返回false。1106 *1107 *@author宋立君1108 * @date 2014年06月24日1109 *@paramchkStr1110 * 被检查的字符串1111 *@returnboolean1112 */

1113 public static booleanisEmpty(String chkStr) {1114 if (chkStr == null) {1115 return true;1116 } else{1117 return "".equals(chkStr.trim()) ? true : false;1118 }1119 }1120

1121 /**

1122 * 如果字符串没有超过最长显示长度返回原字符串,否则从开头截取指定长度并加...返回。1123 *1124 *@paramstr1125 * 原字符串1126 *@paramlength1127 * 字符串最长显示的长度1128 *@return转换后的字符串1129 */

1130 public static String trimString(String str, intlength) {1131 if (str == null) {1132 return "";1133 } else if (str.length() >length) {1134 return str.substring(0, length - 3) + "...";1135 } else{1136 returnstr;1137 }1138 }1139

1140 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值