1. /* ====================================================================
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowledgement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowledgement may appear in the software itself,
  24. * if and wherever such third-party acknowledgements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Commons", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Software Foundation.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.commons.lang;
  55. import java.util.ArrayList;
  56. import java.util.Iterator;
  57. import java.util.List;
  58. /**
  59. * <p>Operations on {@link java.lang.String} that are
  60. * <code>null</code> safe.</p>
  61. *
  62. * <ul>
  63. * <li><b>IsEmpty/IsBlank</b>
  64. * - checks if a String contains text</li>
  65. * <li><b>Trim/Strip</b>
  66. * - removes leading and trailing whitespace</li>
  67. * <li><b>Equals</b>
  68. * - compares two strings null-safe</li>
  69. * <li><b>IndexOf/LastIndexOf/Contains</b>
  70. * - null-safe index-of checks
  71. * <li><b>IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut</b>
  72. * - index-of any of a set of Strings</li>
  73. * <li><b>ContainsOnly/ContainsNone</b>
  74. * - does String contains only/none of these characters</li>
  75. * <li><b>Substring/Left/Right/Mid</b>
  76. * - null-safe substring extractions</li>
  77. * <li><b>SubstringBefore/SubstringAfter/SubstringBetween</b>
  78. * - substring extraction relative to other strings</li>
  79. * <li><b>Split/Join</b>
  80. * - splits a String into an array of substrings and vice versa</li>
  81. * <li><b>Replace/Delete/Overlay</b>
  82. * - Searches a String and replaces one String with another</li>
  83. * <li><b>Chomp/Chop</b>
  84. * - removes the last part of a String</li>
  85. * <li><b>LeftPad/RightPad/Center/Repeat</b>
  86. * - pads a String</li>
  87. * <li><b>UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize</b>
  88. * - changes the case of a String</li>
  89. * <li><b>CountMatches</b>
  90. * - counts the number of occurrances of one String in another</li>
  91. * <li><b>IsAlpha/IsNumeric/IsWhitespace</b>
  92. * - checks the characters in a String</li>
  93. * <li><b>DefaultString</b>
  94. * - protects against a null input String</li>
  95. * <li><b>Reverse/ReverseDelimited</b>
  96. * - reverses a String</li>
  97. * <li><b>Abbreviate</b>
  98. * - abbreviates a string using ellipsis</li>
  99. * <li><b>Difference</b>
  100. * - compares two Strings and reports on their differences</li>
  101. * <li><b>LevensteinDistance</b>
  102. * - the number of changes needed to change one String into another</li>
  103. * </ul>
  104. *
  105. * <p>The <code>StringUtils</code> class defines certain words related to
  106. * String handling.</p>
  107. *
  108. * <ul>
  109. * <li>null - <code>null</code></li>
  110. * <li>empty - a zero-length string (<code>""</code>)</li>
  111. * <li>space - the space character (<code>' '</code>, char 32)</li>
  112. * <li>whitespace - the characters defined by {@link Character#isWhitespace(char)}</li>
  113. * <li>trim - the characters <= 32 as in {@link String#trim()}</li>
  114. * </ul>
  115. *
  116. * <p><code>StringUtils</code> handles <code>null</code> input Strings quietly.
  117. * That is to say that a <code>null</code> input will return <code>null</code>.
  118. * Where a <code>boolean</code> or <code>int</code> is being returned
  119. * details vary by method.</p>
  120. *
  121. * <p>A side effect of the <code>null</code> handling is that a
  122. * <code>NullPointerException</code> should be considered a bug in
  123. * <code>StringUtils</code> (except for deprecated methods).</p>
  124. *
  125. * <p>Methods in this class give sample code to explain their operation.
  126. * The symbol <code>*</code> is used to indicate any input including <code>null</code>.</p>
  127. *
  128. * @see java.lang.String
  129. * @author <a href="http://jakarta.apache.org/turbine/">Apache Jakarta Turbine</a>
  130. * @author GenerationJavaCore
  131. * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
  132. * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
  133. * @author <a href="mailto:gcoladonato@yahoo.com">Greg Coladonato</a>
  134. * @author <a href="mailto:bayard@generationjava.com">Henri Yandell</a>
  135. * @author <a href="mailto:ed@apache.org">Ed Korthof</a>
  136. * @author <a href="mailto:rand_mcneely@yahoo.com">Rand McNeely</a>
  137. * @author Stephen Colebourne
  138. * @author <a href="mailto:fredrik@westermarck.com">Fredrik Westermarck</a>
  139. * @author Holger Krauth
  140. * @author <a href="mailto:alex@purpletech.com">Alexander Day Chaffee</a>
  141. * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
  142. * @author Arun Mammen Thomas
  143. * @author Gary Gregory
  144. * @author Phil Steitz
  145. * @since 1.0
  146. * @version $Id: StringUtils.java,v 1.107 2003/08/23 10:39:20 scolebourne Exp $
  147. */
  148. public class StringUtils {
  149. // Performance testing notes (JDK 1.4, Jul03, scolebourne)
  150. // Whitespace:
  151. // Character.isWhitespace() is faster than WHITESPACE.indexOf()
  152. // where WHITESPACE is a string of all whitespace characters
  153. //
  154. // Character access:
  155. // String.charAt(n) versus toCharArray(), then array[n]
  156. // String.charAt(n) is about 15% worse for a 10K string
  157. // They are about equal for a length 50 string
  158. // String.charAt(n) is about 4 times better for a length 3 string
  159. // String.charAt(n) is best bet overall
  160. //
  161. // Append:
  162. // String.concat about twice as fast as StringBuffer.append
  163. // (not sure who tested this)
  164. /**
  165. * The empty String <code>""</code>.
  166. * @since 2.0
  167. */
  168. public static final String EMPTY = "";
  169. /**
  170. * <p>The maximum size to which the padding constant(s) can expand.</p>
  171. */
  172. private static final int PAD_LIMIT = 8192;
  173. /**
  174. * <p>An array of <code>String</code>s used for padding.</p>
  175. *
  176. * <p>Used for efficient space padding. The length of each String expands as needed.</p>
  177. */
  178. private static final String[] PADDING = new String[Character.MAX_VALUE];
  179. static {
  180. // space padding is most common, start with 64 chars
  181. PADDING[32] = " ";
  182. }
  183. /**
  184. * <p><code>StringUtils</code> instances should NOT be constructed in
  185. * standard programming. Instead, the class should be used as
  186. * <code>StringUtils.trim(" foo ");</code>.</p>
  187. *
  188. * <p>This constructor is public to permit tools that require a JavaBean
  189. * instance to operate.</p>
  190. */
  191. public StringUtils() {
  192. }
  193. // Empty checks
  194. //-----------------------------------------------------------------------
  195. /**
  196. * <p>Checks if a String is empty ("") or null.</p>
  197. *
  198. * <pre>
  199. * StringUtils.isEmpty(null) = true
  200. * StringUtils.isEmpty("") = true
  201. * StringUtils.isEmpty(" ") = false
  202. * StringUtils.isEmpty("bob") = false
  203. * StringUtils.isEmpty(" bob ") = false
  204. * </pre>
  205. *
  206. * <p>NOTE: This method changed in Lang version 2.0.
  207. * It no longer trims the String.
  208. * That functionality is available in isBlank().</p>
  209. *
  210. * @param str the String to check, may be null
  211. * @return <code>true</code> if the String is empty or null
  212. */
  213. public static boolean isEmpty(String str) {
  214. return (str == null || str.length() == 0);
  215. }
  216. /**
  217. * <p>Checks if a String is not empty ("") and not null.</p>
  218. *
  219. * <pre>
  220. * StringUtils.isNotEmpty(null) = false
  221. * StringUtils.isNotEmpty("") = false
  222. * StringUtils.isNotEmpty(" ") = true
  223. * StringUtils.isNotEmpty("bob") = true
  224. * StringUtils.isNotEmpty(" bob ") = true
  225. * </pre>
  226. *
  227. * @param str the String to check, may be null
  228. * @return <code>true</code> if the String is not empty and not null
  229. */
  230. public static boolean isNotEmpty(String str) {
  231. return (str != null && str.length() > 0);
  232. }
  233. /**
  234. * <p>Checks if a String is whitespace, empty ("") or null.</p>
  235. *
  236. * <pre>
  237. * StringUtils.isBlank(null) = true
  238. * StringUtils.isBlank("") = true
  239. * StringUtils.isBlank(" ") = true
  240. * StringUtils.isBlank("bob") = false
  241. * StringUtils.isBlank(" bob ") = false
  242. * </pre>
  243. *
  244. * @param str the String to check, may be null
  245. * @return <code>true</code> if the String is null, empty or whitespace
  246. * @since 2.0
  247. */
  248. public static boolean isBlank(String str) {
  249. int strLen;
  250. if (str == null || (strLen = str.length()) == 0) {
  251. return true;
  252. }
  253. for (int i = 0; i < strLen; i++) {
  254. if ((Character.isWhitespace(str.charAt(i)) == false) ) {
  255. return false;
  256. }
  257. }
  258. return true;
  259. }
  260. /**
  261. * <p>Checks if a String is not empty (""), not null and not whitespace only.</p>
  262. *
  263. * <pre>
  264. * StringUtils.isNotBlank(null) = false
  265. * StringUtils.isNotBlank("") = false
  266. * StringUtils.isNotBlank(" ") = false
  267. * StringUtils.isNotBlank("bob") = true
  268. * StringUtils.isNotBlank(" bob ") = true
  269. * </pre>
  270. *
  271. * @param str the String to check, may be null
  272. * @return <code>true</code> if the String is
  273. * not empty and not null and not whitespace
  274. * @since 2.0
  275. */
  276. public static boolean isNotBlank(String str) {
  277. int strLen;
  278. if (str == null || (strLen = str.length()) == 0) {
  279. return false;
  280. }
  281. for (int i = 0; i < strLen; i++) {
  282. if ((Character.isWhitespace(str.charAt(i)) == false) ) {
  283. return true;
  284. }
  285. }
  286. return false;
  287. }
  288. // Trim
  289. //-----------------------------------------------------------------------
  290. /**
  291. * <p>Removes control characters (char <= 32) from both
  292. * ends of this String, handling <code>null</code> by returning
  293. * an empty String ("").</p>
  294. *
  295. * <pre>
  296. * StringUtils.clean(null) = ""
  297. * StringUtils.clean("") = ""
  298. * StringUtils.clean("abc") = "abc"
  299. * StringUtils.clean(" abc ") = "abc"
  300. * StringUtils.clean(" ") = ""
  301. * </pre>
  302. *
  303. * @see java.lang.String#trim()
  304. * @param str the String to clean, may be null
  305. * @return the trimmed text, never <code>null</code>
  306. * @deprecated Use the clearer named {@link #trimToEmpty(String)}.
  307. * Method will be removed in Commons Lang 3.0.
  308. */
  309. public static String clean(String str) {
  310. return (str == null ? EMPTY : str.trim());
  311. }
  312. /**
  313. * <p>Removes control characters (char <= 32) from both
  314. * ends of this String, handling <code>null</code> by returning
  315. * <code>null</code>.</p>
  316. *
  317. * <p>The String is trimmed using {@link String#trim()}.
  318. * Trim removes start and end characters <= 32.
  319. * To strip whitespace use {@link #strip(String)}.</p>
  320. *
  321. * <p>To trim your choice of characters, use the
  322. * {@link #strip(String, String)} methods.</p>
  323. *
  324. * <pre>
  325. * StringUtils.trim(null) = null
  326. * StringUtils.trim("") = ""
  327. * StringUtils.trim(" ") = ""
  328. * StringUtils.trim("abc") = "abc"
  329. * StringUtils.trim(" abc ") = "abc"
  330. * </pre>
  331. *
  332. * @param str the String to be trimmed, may be null
  333. * @return the trimmed string, <code>null</code> if null String input
  334. */
  335. public static String trim(String str) {
  336. return (str == null ? null : str.trim());
  337. }
  338. /**
  339. * <p>Removes control characters (char <= 32) from both
  340. * ends of this String returning <code>null</code> if the String is
  341. * empty ("") after the trim or if it is <code>null</code>.
  342. *
  343. * <p>The String is trimmed using {@link String#trim()}.
  344. * Trim removes start and end characters <= 32.
  345. * To strip whitespace use {@link #stripToNull(String)}.</p>
  346. *
  347. * <pre>
  348. * StringUtils.trimToNull(null) = null
  349. * StringUtils.trimToNull("") = null
  350. * StringUtils.trimToNull(" ") = null
  351. * StringUtils.trimToNull("abc") = "abc"
  352. * StringUtils.trimToNull(" abc ") = "abc"
  353. * </pre>
  354. *
  355. * @param str the String to be trimmed, may be null
  356. * @return the trimmed String,
  357. * <code>null</code> if only chars <= 32, empty or null String input
  358. * @since 2.0
  359. */
  360. public static String trimToNull(String str) {
  361. String ts = trim(str);
  362. return (ts == null || ts.length() == 0 ? null : ts);
  363. }
  364. /**
  365. * <p>Removes control characters (char <= 32) from both
  366. * ends of this String returning an empty String ("") if the String
  367. * is empty ("") after the trim or if it is <code>null</code>.
  368. *
  369. * <p>The String is trimmed using {@link String#trim()}.
  370. * Trim removes start and end characters <= 32.
  371. * To strip whitespace use {@link #stripToEmpty(String)}.</p>
  372. *
  373. * <pre>
  374. * StringUtils.trimToEmpty(null) = ""
  375. * StringUtils.trimToEmpty("") = ""
  376. * StringUtils.trimToEmpty(" ") = ""
  377. * StringUtils.trimToEmpty("abc") = "abc"
  378. * StringUtils.trimToEmpty(" abc ") = "abc"
  379. * </pre>
  380. *
  381. * @param str the String to be trimmed, may be null
  382. * @return the trimmed String, or an empty String if <code>null</code> input
  383. * @since 2.0
  384. */
  385. public static String trimToEmpty(String str) {
  386. return (str == null ? EMPTY : str.trim());
  387. }
  388. // Stripping
  389. //-----------------------------------------------------------------------
  390. /**
  391. * <p>Strips whitespace from the start and end of a String.</p>
  392. *
  393. * <p>This is similar to {@link #trim(String)} but removes whitespace.
  394. * Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
  395. *
  396. * <p>A <code>null</code> input String returns <code>null</code>.</p>
  397. *
  398. * <pre>
  399. * StringUtils.strip(null) = null
  400. * StringUtils.strip("") = ""
  401. * StringUtils.strip(" ") = ""
  402. * StringUtils.strip("abc") = "abc"
  403. * StringUtils.strip(" abc") = "abc"
  404. * StringUtils.strip("abc ") = "abc"
  405. * StringUtils.strip(" abc ") = "abc"
  406. * StringUtils.strip(" ab c ") = "ab c"
  407. * </pre>
  408. *
  409. * @param str the String to remove whitespace from, may be null
  410. * @return the stripped String, <code>null</code> if null String input
  411. */
  412. public static String strip(String str) {
  413. return strip(str, null);
  414. }
  415. /**
  416. * <p>Strips whitespace from the start and end of a String returning
  417. * <code>null</code> if the String is empty ("") after the strip.</p>
  418. *
  419. * <p>This is similar to {@link #trimToNull(String)} but removes whitespace.
  420. * Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
  421. *
  422. * <pre>
  423. * StringUtils.strip(null) = null
  424. * StringUtils.strip("") = null
  425. * StringUtils.strip(" ") = null
  426. * StringUtils.strip("abc") = "abc"
  427. * StringUtils.strip(" abc") = "abc"
  428. * StringUtils.strip("abc ") = "abc"
  429. * StringUtils.strip(" abc ") = "abc"
  430. * StringUtils.strip(" ab c ") = "ab c"
  431. * </pre>
  432. *
  433. * @param str the String to be stripped, may be null
  434. * @return the stripped String,
  435. * <code>null</code> if whitespace, empty or null String input
  436. * @since 2.0
  437. */
  438. public static String stripToNull(String str) {
  439. if (str == null) {
  440. return null;
  441. }
  442. str = strip(str, null);
  443. return (str.length() == 0 ? null : str);
  444. }
  445. /**
  446. * <p>Strips whitespace from the start and end of a String returning
  447. * an empty String if <code>null</code> input.</p>
  448. *
  449. * <p>This is similar to {@link #trimToEmpty(String)} but removes whitespace.
  450. * Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
  451. *
  452. * <pre>
  453. * StringUtils.strip(null) = ""
  454. * StringUtils.strip("") = ""
  455. * StringUtils.strip(" ") = ""
  456. * StringUtils.strip("abc") = "abc"
  457. * StringUtils.strip(" abc") = "abc"
  458. * StringUtils.strip("abc ") = "abc"
  459. * StringUtils.strip(" abc ") = "abc"
  460. * StringUtils.strip(" ab c ") = "ab c"
  461. * </pre>
  462. *
  463. * @param str the String to be stripped, may be null
  464. * @return the trimmed String, or an empty String if <code>null</code> input
  465. * @since 2.0
  466. */
  467. public static String stripToEmpty(String str) {
  468. return (str == null ? EMPTY : strip(str, null));
  469. }
  470. /**
  471. * <p>Strips any of a set of characters from the start and end of a String.
  472. * This is similar to {@link String#trim()} but allows the characters
  473. * to be stripped to be controlled.</p>
  474. *
  475. * <p>A <code>null</code> input String returns <code>null</code>.
  476. * An empty string ("") input returns the empty string.</p>
  477. *
  478. * <p>If the stripChars String is <code>null</code>, whitespace is
  479. * stripped as defined by {@link Character#isWhitespace(char)}.
  480. * Alternatively use {@link #strip(String)}.</p>
  481. *
  482. * <pre>
  483. * StringUtils.strip(null, *) = null
  484. * StringUtils.strip("", *) = ""
  485. * StringUtils.strip("abc", null) = "abc"
  486. * StringUtils.strip(" abc", null) = "abc"
  487. * StringUtils.strip("abc ", null) = "abc"
  488. * StringUtils.strip(" abc ", null) = "abc"
  489. * StringUtils.strip(" abcyx", "xyz") = " abc"
  490. * </pre>
  491. *
  492. * @param str the String to remove characters from, may be null
  493. * @param stripChars the characters to remove, null treated as whitespace
  494. * @return the stripped String, <code>null</code> if null String input
  495. */
  496. public static String strip(String str, String stripChars) {
  497. if (str == null || str.length() == 0) {
  498. return str;
  499. }
  500. str = stripStart(str, stripChars);
  501. return stripEnd(str, stripChars);
  502. }
  503. /**
  504. * <p>Strips any of a set of characters from the start of a String.</p>
  505. *
  506. * <p>A <code>null</code> input String returns <code>null</code>.
  507. * An empty string ("") input returns the empty string.</p>
  508. *
  509. * <p>If the stripChars String is <code>null</code>, whitespace is
  510. * stripped as defined by {@link Character#isWhitespace(char)}.</p>
  511. *
  512. * <pre>
  513. * StringUtils.stripStart(null, *) = null
  514. * StringUtils.stripStart("", *) = ""
  515. * StringUtils.stripStart("abc", "") = "abc"
  516. * StringUtils.stripStart("abc", null) = "abc"
  517. * StringUtils.stripStart(" abc", null) = "abc"
  518. * StringUtils.stripStart("abc ", null) = "abc "
  519. * StringUtils.stripStart(" abc ", null) = "abc "
  520. * StringUtils.stripStart("yxabc ", "xyz") = "abc "
  521. * </pre>
  522. *
  523. * @param str the String to remove characters from, may be null
  524. * @param stripChars the characters to remove, null treated as whitespace
  525. * @return the stripped String, <code>null</code> if null String input
  526. */
  527. public static String stripStart(String str, String stripChars) {
  528. int strLen;
  529. if (str == null || (strLen = str.length()) == 0) {
  530. return str;
  531. }
  532. int start = 0;
  533. if (stripChars == null) {
  534. while ((start != strLen) && Character.isWhitespace(str.charAt(start))) {
  535. start++;
  536. }
  537. } else if (stripChars.length() == 0) {
  538. return str;
  539. } else {
  540. while ((start != strLen) && (stripChars.indexOf(str.charAt(start)) != -1)) {
  541. start++;
  542. }
  543. }
  544. return str.substring(start);
  545. }
  546. /**
  547. * <p>Strips any of a set of characters from the end of a String.</p>
  548. *
  549. * <p>A <code>null</code> input String returns <code>null</code>.
  550. * An empty string ("") input returns the empty string.</p>
  551. *
  552. * <p>If the stripChars String is <code>null</code>, whitespace is
  553. * stripped as defined by {@link Character#isWhitespace(char)}.</p>
  554. *
  555. * <pre>
  556. * StringUtils.stripEnd(null, *) = null
  557. * StringUtils.stripEnd("", *) = ""
  558. * StringUtils.stripEnd("abc", "") = "abc"
  559. * StringUtils.stripEnd("abc", null) = "abc"
  560. * StringUtils.stripEnd(" abc", null) = " abc"
  561. * StringUtils.stripEnd("abc ", null) = "abc"
  562. * StringUtils.stripEnd(" abc ", null) = " abc"
  563. * StringUtils.stripEnd(" abcyx", "xyz") = " abc"
  564. * </pre>
  565. *
  566. * @param str the String to remove characters from, may be null
  567. * @param stripChars the characters to remove, null treated as whitespace
  568. * @return the stripped String, <code>null</code> if null String input
  569. */
  570. public static String stripEnd(String str, String stripChars) {
  571. int end;
  572. if (str == null || (end = str.length()) == 0) {
  573. return str;
  574. }
  575. if (stripChars == null) {
  576. while ((end != 0) && Character.isWhitespace(str.charAt(end - 1))) {
  577. end--;
  578. }
  579. } else if (stripChars.length() == 0) {
  580. return str;
  581. } else {
  582. while ((end != 0) && (stripChars.indexOf(str.charAt(end - 1)) != -1)) {
  583. end--;
  584. }
  585. }
  586. return str.substring(0, end);
  587. }
  588. // StripAll
  589. //-----------------------------------------------------------------------
  590. /**
  591. * <p>Strips whitespace from the start and end of every String in an array.
  592. * Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
  593. *
  594. * <p>A new array is returned each time, except for length zero.
  595. * A <code>null</code> array will return <code>null</code>.
  596. * An empty array will return itself.
  597. * A <code>null</code> array entry will be ignored.</p>
  598. *
  599. * <pre>
  600. * StringUtils.stripAll(null) = null
  601. * StringUtils.stripAll([]) = []
  602. * StringUtils.stripAll(["abc", " abc"]) = ["abc", "abc"]
  603. * StringUtils.stripAll(["abc ", null]) = ["abc", null]
  604. * </pre>
  605. *
  606. * @param strs the array to remove whitespace from, may be null
  607. * @return the stripped Strings, <code>null</code> if null array input
  608. */
  609. public static String[] stripAll(String[] strs) {
  610. return stripAll(strs, null);
  611. }
  612. /**
  613. * <p>Strips any of a set of characters from the start and end of every
  614. * String in an array.</p>
  615. * Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
  616. *
  617. * <p>A new array is returned each time, except for length zero.
  618. * A <code>null</code> array will return <code>null</code>.
  619. * An empty array will return itself.
  620. * A <code>null</code> array entry will be ignored.
  621. * A <code>null</code> stripChars will strip whitespace as defined by
  622. * {@link Character#isWhitespace(char)}.</p>
  623. *
  624. * <pre>
  625. * StringUtils.stripAll(null, *) = null
  626. * StringUtils.stripAll([], *) = []
  627. * StringUtils.stripAll(["abc", " abc"], null) = ["abc", "abc"]
  628. * StringUtils.stripAll(["abc ", null], null) = ["abc", null]
  629. * StringUtils.stripAll(["abc ", null], "yz") = ["abc ", null]
  630. * StringUtils.stripAll(["yabcz", null], "yz") = ["abc", null]
  631. * </pre>
  632. *
  633. * @param strs the array to remove characters from, may be null
  634. * @param stripChars the characters to remove, null treated as whitespace
  635. * @return the stripped Strings, <code>null</code> if null array input
  636. */
  637. public static String[] stripAll(String[] strs, String stripChars) {
  638. int strsLen;
  639. if (strs == null || (strsLen = strs.length) == 0) {
  640. return strs;
  641. }
  642. String[] newArr = new String[strsLen];
  643. for (int i = 0; i < strsLen; i++) {
  644. newArr[i] = strip(strs[i], stripChars);
  645. }
  646. return newArr;
  647. }
  648. // Equals
  649. //-----------------------------------------------------------------------
  650. /**
  651. * <p>Compares two Strings, returning <code>true</code> if they are equal.</p>
  652. *
  653. * <p><code>null</code>s are handled without exceptions. Two <code>null</code>
  654. * references are considered to be equal. The comparison is case sensitive.</p>
  655. *
  656. * <pre>
  657. * StringUtils.equals(null, null) = true
  658. * StringUtils.equals(null, "abc") = false
  659. * StringUtils.equals("abc", null) = false
  660. * StringUtils.equals("abc", "abc") = true
  661. * StringUtils.equals("abc", "ABC") = false
  662. * </pre>
  663. *
  664. * @see java.lang.String#equals(Object)
  665. * @param str1 the first String, may be null
  666. * @param str2 the second String, may be null
  667. * @return <code>true</code> if the Strings are equal, case sensitive, or
  668. * both <code>null</code>
  669. */
  670. public static boolean equals(String str1, String str2) {
  671. return (str1 == null ? str2 == null : str1.equals(str2));
  672. }
  673. /**
  674. * <p>Compares two Strings, returning <code>true</code> if they are equal ignoring
  675. * the case.</p>
  676. *
  677. * <p><code>null</code>s are handled without exceptions. Two <code>null</code>
  678. * references are considered equal. Comparison is case insensitive.</p>
  679. *
  680. * <pre>
  681. * StringUtils.equalsIgnoreCase(null, null) = true
  682. * StringUtils.equalsIgnoreCase(null, "abc") = false
  683. * StringUtils.equalsIgnoreCase("abc", null) = false
  684. * StringUtils.equalsIgnoreCase("abc", "abc") = true
  685. * StringUtils.equalsIgnoreCase("abc", "ABC") = true
  686. * </pre>
  687. *
  688. * @see java.lang.String#equalsIgnoreCase(String)
  689. * @param str1 the first String, may be null
  690. * @param str2 the second String, may be null
  691. * @return <code>true</code> if the Strings are equal, case insensitive, or
  692. * both <code>null</code>
  693. */
  694. public static boolean equalsIgnoreCase(String str1, String str2) {
  695. return (str1 == null ? str2 == null : str1.equalsIgnoreCase(str2));
  696. }
  697. // IndexOf
  698. //-----------------------------------------------------------------------
  699. /**
  700. * <p>Finds the first index within a String, handling <code>null</code>.
  701. * This method uses {@link String#indexOf(int)}.</p>
  702. *
  703. * <p>A <code>null</code> or empty ("") String will return <code>-1</code>.</p>
  704. *
  705. * <pre>
  706. * StringUtils.indexOf(null, *) = -1
  707. * StringUtils.indexOf("", *) = -1
  708. * StringUtils.indexOf("aabaabaa", 'a') = 0
  709. * StringUtils.indexOf("aabaabaa", 'b') = 2
  710. * </pre>
  711. *
  712. * @param str the String to check, may be null
  713. * @param searchChar the character to find
  714. * @return the first index of the search character,
  715. * -1 if no match or <code>null</code> string input
  716. * @since 2.0
  717. */
  718. public static int indexOf(String str, char searchChar) {
  719. if (str == null || str.length() == 0) {
  720. return -1;
  721. }
  722. return str.indexOf(searchChar);
  723. }
  724. /**
  725. * <p>Finds the first index within a String from a start position,
  726. * handling <code>null</code>.
  727. * This method uses {@link String#indexOf(int, int)}.</p>
  728. *
  729. * <p>A <code>null</code> or empty ("") String will return <code>-1</code>.
  730. * A negative start position is treated as zero.
  731. * A start position greater than the string length returns <code>-1</code>.</p>
  732. *
  733. * <pre>
  734. * StringUtils.indexOf(null, *, *) = -1
  735. * StringUtils.indexOf("", *, *) = -1
  736. * StringUtils.indexOf("aabaabaa", 'b', 0) = 2
  737. * StringUtils.indexOf("aabaabaa", 'b', 3) = 5
  738. * StringUtils.indexOf("aabaabaa", 'b', 9) = -1
  739. * StringUtils.indexOf("aabaabaa", 'b', -1) = 2
  740. * </pre>
  741. *
  742. * @param str the String to check, may be null
  743. * @param searchChar the character to find
  744. * @param startPos the start position, negative treated as zero
  745. * @return the first index of the search character,
  746. * -1 if no match or <code>null</code> string input
  747. * @since 2.0
  748. */
  749. public static int indexOf(String str, char searchChar, int startPos) {
  750. if (str == null || str.length() == 0) {
  751. return -1;
  752. }
  753. return str.indexOf(searchChar, startPos);
  754. }
  755. /**
  756. * <p>Finds the first index within a String, handling <code>null</code>.
  757. * This method uses {@link String#indexOf(String)}.</p>
  758. *
  759. * <p>A <code>null</code> String will return <code>-1</code>.</p>
  760. *
  761. * <pre>
  762. * StringUtils.indexOf(null, *) = -1
  763. * StringUtils.indexOf(*, null) = -1
  764. * StringUtils.indexOf("", "") = 0
  765. * StringUtils.indexOf("aabaabaa", "a") = 0
  766. * StringUtils.indexOf("aabaabaa", "b") = 2
  767. * StringUtils.indexOf("aabaabaa", "ab") = 1
  768. * StringUtils.indexOf("aabaabaa", "") = 0
  769. * </pre>
  770. *
  771. * @param str the String to check, may be null
  772. * @param searchStr the String to find, may be null
  773. * @return the first index of the search String,
  774. * -1 if no match or <code>null</code> string input
  775. * @since 2.0
  776. */
  777. public static int indexOf(String str, String searchStr) {
  778. if (str == null || searchStr == null) {
  779. return -1;
  780. }
  781. return str.indexOf(searchStr);
  782. }
  783. /**
  784. * <p>Finds the first index within a String, handling <code>null</code>.
  785. * This method uses {@link String#indexOf(String, int)}.</p>
  786. *
  787. * <p>A <code>null</code> String will return <code>-1</code>.
  788. * A negative start position is treated as zero.
  789. * An empty ("") search String always matches.
  790. * A start position greater than the string length only matches
  791. * an empty search String.</p>
  792. *
  793. * <pre>
  794. * StringUtils.indexOf(null, *, *) = -1
  795. * StringUtils.indexOf(*, null, *) = -1
  796. * StringUtils.indexOf("", "", 0) = 0
  797. * StringUtils.indexOf("aabaabaa", "a", 0) = 0
  798. * StringUtils.indexOf("aabaabaa", "b", 0) = 2
  799. * StringUtils.indexOf("aabaabaa", "ab", 0) = 1
  800. * StringUtils.indexOf("aabaabaa", "b", 3) = 5
  801. * StringUtils.indexOf("aabaabaa", "b", 9) = -1
  802. * StringUtils.indexOf("aabaabaa", "b", -1) = 2
  803. * StringUtils.indexOf("aabaabaa", "", 2) = 2
  804. * StringUtils.indexOf("abc", "", 9) = 3
  805. * </pre>
  806. *
  807. * @param str the String to check, may be null
  808. * @param searchStr the String to find, may be null
  809. * @param startPos the start position, negative treated as zero
  810. * @return the first index of the search String,
  811. * -1 if no match or <code>null</code> string input
  812. * @since 2.0
  813. */
  814. public static int indexOf(String str, String searchStr, int startPos) {
  815. if (str == null || searchStr == null) {
  816. return -1;
  817. }
  818. // JDK1.2/JDK1.3 have a bug, when startPos > str.length for "", hence
  819. if (searchStr.length() == 0 && startPos >= str.length()) {
  820. return str.length();
  821. }
  822. return str.indexOf(searchStr, startPos);
  823. }
  824. // LastIndexOf
  825. //-----------------------------------------------------------------------
  826. /**
  827. * <p>Finds the last index within a String, handling <code>null</code>.
  828. * This method uses {@link String#lastIndexOf(int)}.</p>
  829. *
  830. * <p>A <code>null</code> or empty ("") String will return <code>-1</code>.</p>
  831. *
  832. * <pre>
  833. * StringUtils.lastIndexOf(null, *) = -1
  834. * StringUtils.lastIndexOf("", *) = -1
  835. * StringUtils.lastIndexOf("aabaabaa", 'a') = 7
  836. * StringUtils.lastIndexOf("aabaabaa", 'b') = 5
  837. * </pre>
  838. *
  839. * @param str the String to check, may be null
  840. * @param searchChar the character to find
  841. * @return the last index of the search character,
  842. * -1 if no match or <code>null</code> string input
  843. * @since 2.0
  844. */
  845. public static int lastIndexOf(String str, char searchChar) {
  846. if (str == null || str.length() == 0) {
  847. return -1;
  848. }
  849. return str.lastIndexOf(searchChar);
  850. }
  851. /**
  852. * <p>Finds the last index within a String from a start position,
  853. * handling <code>null</code>.
  854. * This method uses {@link String#lastIndexOf(int, int)}.</p>
  855. *
  856. * <p>A <code>null</code> or empty ("") String will return <code>-1</code>.
  857. * A negative start position returns <code>-1</code>.
  858. * A start position greater than the string length searches the whole string.</p>
  859. *
  860. * <pre>
  861. * StringUtils.lastIndexOf(null, *, *) = -1
  862. * StringUtils.lastIndexOf("", *, *) = -1
  863. * StringUtils.lastIndexOf("aabaabaa", 'b', 8) = 5
  864. * StringUtils.lastIndexOf("aabaabaa", 'b', 4) = 2
  865. * StringUtils.lastIndexOf("aabaabaa", 'b', 0) = -1
  866. * StringUtils.lastIndexOf("aabaabaa", 'b', 9) = 5
  867. * StringUtils.lastIndexOf("aabaabaa", 'b', -1) = -1
  868. * StringUtils.lastIndexOf("aabaabaa", 'a', 0) = 0
  869. * </pre>
  870. *
  871. * @param str the String to check, may be null
  872. * @param searchChar the character to find
  873. * @param startPos the start position
  874. * @return the last index of the search character,
  875. * -1 if no match or <code>null</code> string input
  876. * @since 2.0
  877. */
  878. public static int lastIndexOf(String str, char searchChar, int startPos) {
  879. if (str == null || str.length() == 0) {
  880. return -1;
  881. }
  882. return str.lastIndexOf(searchChar, startPos);
  883. }
  884. /**
  885. * <p>Finds the last index within a String, handling <code>null</code>.
  886. * This method uses {@link String#lastIndexOf(String)}.</p>
  887. *
  888. * <p>A <code>null</code> String will return <code>-1</code>.</p>
  889. *
  890. * <pre>
  891. * StringUtils.lastIndexOf(null, *) = -1
  892. * StringUtils.lastIndexOf(*, null) = -1
  893. * StringUtils.lastIndexOf("", "") = 0
  894. * StringUtils.lastIndexOf("aabaabaa", "a") = 0
  895. * StringUtils.lastIndexOf("aabaabaa", "b") = 2
  896. * StringUtils.lastIndexOf("aabaabaa", "ab") = 1
  897. * StringUtils.lastIndexOf("aabaabaa", "") = 8
  898. * </pre>
  899. *
  900. * @param str the String to check, may be null
  901. * @param searchStr the String to find, may be null
  902. * @return the last index of the search String,
  903. * -1 if no match or <code>null</code> string input
  904. * @since 2.0
  905. */
  906. public static int lastIndexOf(String str, String searchStr) {
  907. if (str == null || searchStr == null) {
  908. return -1;
  909. }
  910. return str.lastIndexOf(searchStr);
  911. }
  912. /**
  913. * <p>Finds the first index within a String, handling <code>null</code>.
  914. * This method uses {@link String#lastIndexOf(String, int)}.</p>
  915. *
  916. * <p>A <code>null</code> String will return <code>-1</code>.
  917. * A negative start position returns <code>-1</code>.
  918. * An empty ("") search String always matches unless the start position is negative.
  919. * A start position greater than the string length searches the whole string.</p>
  920. *
  921. * <pre>
  922. * StringUtils.lastIndexOf(null, *, *) = -1
  923. * StringUtils.lastIndexOf(*, null, *) = -1
  924. * StringUtils.lastIndexOf("aabaabaa", "a", 8) = 7
  925. * StringUtils.lastIndexOf("aabaabaa", "b", 8) = 5
  926. * StringUtils.lastIndexOf("aabaabaa", "ab", 8) = 4
  927. * StringUtils.lastIndexOf("aabaabaa", "b", 9) = 5
  928. * StringUtils.lastIndexOf("aabaabaa", "b", -1) = -1
  929. * StringUtils.lastIndexOf("aabaabaa", "a", 0) = 0
  930. * StringUtils.lastIndexOf("aabaabaa", "b", 0) = -1
  931. * </pre>
  932. *
  933. * @param str the String to check, may be null
  934. * @param searchStr the String to find, may be null
  935. * @param startPos the start position, negative treated as zero
  936. * @return the first index of the search String,
  937. * -1 if no match or <code>null</code> string input
  938. * @since 2.0
  939. */
  940. public static int lastIndexOf(String str, String searchStr, int startPos) {
  941. if (str == null || searchStr == null) {
  942. return -1;
  943. }
  944. return str.lastIndexOf(searchStr, startPos);
  945. }
  946. // Contains
  947. //-----------------------------------------------------------------------
  948. /**
  949. * <p>Checks if String contains a search character, handling <code>null</code>.
  950. * This method uses {@link String#indexOf(int)}.</p>
  951. *
  952. * <p>A <code>null</code> or empty ("") String will return <code>false</code>.</p>
  953. *
  954. * <pre>
  955. * StringUtils.contains(null, *) = false
  956. * StringUtils.contains("", *) = false
  957. * StringUtils.contains("abc", 'a') = true
  958. * StringUtils.contains("abc", 'z') = false
  959. * </pre>
  960. *
  961. * @param str the String to check, may be null
  962. * @param searchChar the character to find
  963. * @return true if the String contains the search character,
  964. * false if not or <code>null</code> string input
  965. * @since 2.0
  966. */
  967. public static boolean contains(String str, char searchChar) {
  968. if (str == null || str.length() == 0) {
  969. return false;
  970. }
  971. return (str.indexOf(searchChar) >= 0);
  972. }
  973. /**
  974. * <p>Find the first index within a String, handling <code>null</code>.
  975. * This method uses {@link String#indexOf(int)}.</p>
  976. *
  977. * <p>A <code>null</code> String will return <code>false</code>.</p>
  978. *
  979. * <pre>
  980. * StringUtils.contains(null, *) = false
  981. * StringUtils.contains(*, null) = false
  982. * StringUtils.contains("", "") = true
  983. * StringUtils.contains("abc", "") = true
  984. * StringUtils.contains("abc", "a") = true
  985. * StringUtils.contains("abc", "z") = false
  986. * </pre>
  987. *
  988. * @param str the String to check, may be null
  989. * @param searchStr the String to find, may be null
  990. * @return true if the String contains the search character,
  991. * false if not or <code>null</code> string input
  992. * @since 2.0
  993. */
  994. public static boolean contains(String str, String searchStr) {
  995. if (str == null || searchStr == null) {
  996. return false;
  997. }
  998. return (str.indexOf(searchStr) >= 0);
  999. }
  1000. // IndexOfAny chars
  1001. //-----------------------------------------------------------------------
  1002. /**
  1003. * <p>Search a String to find the first index of any
  1004. * character in the given set of characters.</p>
  1005. *
  1006. * <p>A <code>null</code> String will return <code>-1</code>.
  1007. * A <code>null</code> or zero length search array will return <code>-1</code>.</p>
  1008. *
  1009. * <pre>
  1010. * StringUtils.indexOfAny(null, *) = -1
  1011. * StringUtils.indexOfAny("", *) = -1
  1012. * StringUtils.indexOfAny(*, null) = -1
  1013. * StringUtils.indexOfAny(*, []) = -1
  1014. * StringUtils.indexOfAny("zzabyycdxx",['z','a']) = 0
  1015. * StringUtils.indexOfAny("zzabyycdxx",['b','y']) = 3
  1016. * StringUtils.indexOfAny("aba", ['z']) = -1
  1017. * </pre>
  1018. *
  1019. * @param str the String to check, may be null
  1020. * @param searchChars the chars to search for, may be null
  1021. * @return the index of any of the chars, -1 if no match or null input
  1022. * @since 2.0
  1023. */
  1024. public static int indexOfAny(String str, char[] searchChars) {
  1025. if (str == null || str.length() == 0 || searchChars == null || searchChars.length == 0) {
  1026. return -1;
  1027. }
  1028. for (int i = 0; i < str.length(); i ++) {
  1029. char ch = str.charAt(i);
  1030. for (int j = 0; j < searchChars.length; j++) {
  1031. if (searchChars[j] == ch) {
  1032. return i;
  1033. }
  1034. }
  1035. }
  1036. return -1;
  1037. }
  1038. /**
  1039. * <p>Search a String to find the first index of any
  1040. * character in the given set of characters.</p>
  1041. *
  1042. * <p>A <code>null</code> String will return <code>-1</code>.
  1043. * A <code>null</code> search string will return <code>-1</code>.</p>
  1044. *
  1045. * <pre>
  1046. * StringUtils.indexOfAny(null, *) = -1
  1047. * StringUtils.indexOfAny("", *) = -1
  1048. * StringUtils.indexOfAny(*, null) = -1
  1049. * StringUtils.indexOfAny(*, "") = -1
  1050. * StringUtils.indexOfAny("zzabyycdxx", "za") = 0
  1051. * StringUtils.indexOfAny("zzabyycdxx", "by") = 3
  1052. * StringUtils.indexOfAny("aba","z") = -1
  1053. * </pre>
  1054. *
  1055. * @param str the String to check, may be null
  1056. * @param searchChars the chars to search for, may be null
  1057. * @return the index of any of the chars, -1 if no match or null input
  1058. * @since 2.0
  1059. */
  1060. public static int indexOfAny(String str, String searchChars) {
  1061. if (str == null || str.length() == 0 || searchChars == null || searchChars.length() == 0) {
  1062. return -1;
  1063. }
  1064. return indexOfAny(str, searchChars.toCharArray());
  1065. }
  1066. // IndexOfAnyBut chars
  1067. //-----------------------------------------------------------------------
  1068. /**
  1069. * <p>Search a String to find the first index of any
  1070. * character not in the given set of characters.</p>
  1071. *
  1072. * <p>A <code>null</code> String will return <code>-1</code>.
  1073. * A <code>null</code> or zero length search array will return <code>-1</code>.</p>
  1074. *
  1075. * <pre>
  1076. * StringUtils.indexOfAnyBut(null, *) = -1
  1077. * StringUtils.indexOfAnyBut("", *) = -1
  1078. * StringUtils.indexOfAnyBut(*, null) = -1
  1079. * StringUtils.indexOfAnyBut(*, []) = -1
  1080. * StringUtils.indexOfAnyBut("zzabyycdxx",'za') = 3
  1081. * StringUtils.indexOfAnyBut("zzabyycdxx", '') = 0
  1082. * StringUtils.indexOfAnyBut("aba", 'ab') = -1
  1083. * </pre>
  1084. *
  1085. * @param str the String to check, may be null
  1086. * @param searchChars the chars to search for, may be null
  1087. * @return the index of any of the chars, -1 if no match or null input
  1088. * @since 2.0
  1089. */
  1090. public static int indexOfAnyBut(String str, char[] searchChars) {
  1091. if (str == null || str.length() == 0 || searchChars == null || searchChars.length == 0) {
  1092. return -1;
  1093. }
  1094. outer: for (int i = 0; i < str.length(); i ++) {
  1095. char ch = str.charAt(i);
  1096. for (int j = 0; j < searchChars.length; j++) {
  1097. if (searchChars[j] == ch) {
  1098. continue outer;
  1099. }
  1100. }
  1101. return i;
  1102. }
  1103. return -1;
  1104. }
  1105. /**
  1106. * <p>Search a String to find the first index of any
  1107. * character not in the given set of characters.</p>
  1108. *
  1109. * <p>A <code>null</code> String will return <code>-1</code>.
  1110. * A <code>null</code> search string will return <code>-1</code>.</p>
  1111. *
  1112. * <pre>
  1113. * StringUtils.indexOfAnyBut(null, *) = -1
  1114. * StringUtils.indexOfAnyBut("", *) = -1
  1115. * StringUtils.indexOfAnyBut(*, null) = -1
  1116. * StringUtils.indexOfAnyBut(*, "") = -1
  1117. * StringUtils.indexOfAnyBut("zzabyycdxx", "za") = 3
  1118. * StringUtils.indexOfAnyBut("zzabyycdxx", "") = 0
  1119. * StringUtils.indexOfAnyBut("aba","ab") = -1
  1120. * </pre>
  1121. *
  1122. * @param str the String to check, may be null
  1123. * @param searchChars the chars to search for, may be null
  1124. * @return the index of any of the chars, -1 if no match or null input
  1125. * @since 2.0
  1126. */
  1127. public static int indexOfAnyBut(String str, String searchChars) {
  1128. if (str == null || str.length() == 0 || searchChars == null || searchChars.length() == 0) {
  1129. return -1;
  1130. }
  1131. for (int i = 0; i < str.length(); i++) {
  1132. if (searchChars.indexOf(str.charAt(i)) < 0) {
  1133. return i;
  1134. }
  1135. }
  1136. return -1;
  1137. }
  1138. // ContainsOnly
  1139. //-----------------------------------------------------------------------
  1140. /**
  1141. * <p>Checks if the String contains only certain characters.</p>
  1142. *
  1143. * <p>A <code>null</code> String will return <code>false</code>.
  1144. * A <code>null</code> valid character array will return <code>false</code>.
  1145. * An empty String ("") always returns <code>true</code>.</p>
  1146. *
  1147. * <pre>
  1148. * StringUtils.containsOnly(null, *) = false
  1149. * StringUtils.containsOnly(*, null) = false
  1150. * StringUtils.containsOnly("", *) = true
  1151. * StringUtils.containsOnly("ab", '') = false
  1152. * StringUtils.containsOnly("abab", 'abc') = true
  1153. * StringUtils.containsOnly("ab1", 'abc') = false
  1154. * StringUtils.containsOnly("abz", 'abc') = false
  1155. * </pre>
  1156. *
  1157. * @param str the String to check, may be null
  1158. * @param valid an array of valid chars, may be null
  1159. * @return true if it only contains valid chars and is non-null
  1160. */
  1161. public static boolean containsOnly(String str, char[] valid) {
  1162. // All these pre-checks are to maintain API with an older version
  1163. if ( (valid == null) || (str == null) ) {
  1164. return false;
  1165. }
  1166. if (str.length() == 0) {
  1167. return true;
  1168. }
  1169. if (valid.length == 0) {
  1170. return false;
  1171. }
  1172. return indexOfAnyBut(str, valid) == -1;
  1173. }
  1174. /**
  1175. * <p>Checks if the String contains only certain characters.</p>
  1176. *
  1177. * <p>A <code>null</code> String will return <code>false</code>.
  1178. * A <code>null</code> valid character String will return <code>false</code>.
  1179. * An empty String ("") always returns <code>true</code>.</p>
  1180. *
  1181. * <pre>
  1182. * StringUtils.containsOnly(null, *) = false
  1183. * StringUtils.containsOnly(*, null) = false
  1184. * StringUtils.containsOnly("", *) = true
  1185. * StringUtils.containsOnly("ab", "") = false
  1186. * StringUtils.containsOnly("abab", "abc") = true
  1187. * StringUtils.containsOnly("ab1", "abc") = false
  1188. * StringUtils.containsOnly("abz", "abc") = false
  1189. * </pre>
  1190. *
  1191. * @param str the String to check, may be null
  1192. * @param validChars a String of valid chars, may be null
  1193. * @return true if it only contains valid chars and is non-null
  1194. * @since 2.0
  1195. */
  1196. public static boolean containsOnly(String str, String validChars) {
  1197. if (str == null || validChars == null) {
  1198. return false;
  1199. }
  1200. return containsOnly(str, validChars.toCharArray());
  1201. }
  1202. // ContainsNone
  1203. //-----------------------------------------------------------------------
  1204. /**
  1205. * <p>Checks that the String does not contain certain characters.</p>
  1206. *
  1207. * <p>A <code>null</code> String will return <code>true</code>.
  1208. * A <code>null</code> invalid character array will return <code>true</code>.
  1209. * An empty String ("") always returns true.</p>
  1210. *
  1211. * <pre>
  1212. * StringUtils.containsNone(null, *) = true
  1213. * StringUtils.containsNone(*, null) = true
  1214. * StringUtils.containsNone("", *) = true
  1215. * StringUtils.containsNone("ab", '') = true
  1216. * StringUtils.containsNone("abab", 'xyz') = true
  1217. * StringUtils.containsNone("ab1", 'xyz') = true
  1218. * StringUtils.containsNone("abz", 'xyz') = false
  1219. * </pre>
  1220. *
  1221. * @param str the String to check, may be null
  1222. * @param invalidChars an array of invalid chars, may be null
  1223. * @return true if it contains none of the invalid chars, or is null
  1224. * @since 2.0
  1225. */
  1226. public static boolean containsNone(String str, char[] invalidChars) {
  1227. if (str == null || invalidChars == null) {
  1228. return true;
  1229. }
  1230. int strSize = str.length();
  1231. int validSize = invalidChars.length;
  1232. for (int i = 0; i < strSize; i++) {
  1233. char ch = str.charAt(i);
  1234. for (int j = 0; j < validSize; j++) {
  1235. if (invalidChars[j] == ch) {
  1236. return false;
  1237. }
  1238. }
  1239. }
  1240. return true;
  1241. }
  1242. /**
  1243. * <p>Checks that the String does not contain certain characters.</p>
  1244. *
  1245. * <p>A <code>null</code> String will return <code>true</code>.
  1246. * A <code>null</code> invalid character array will return <code>true</code>.
  1247. * An empty String ("") always returns true.</p>
  1248. *
  1249. * <pre>
  1250. * StringUtils.containsNone(null, *) = true
  1251. * StringUtils.containsNone(*, null) = true
  1252. * StringUtils.containsNone("", *) = true
  1253. * StringUtils.containsNone("ab", "") = true
  1254. * StringUtils.containsNone("abab", "xyz") = true
  1255. * StringUtils.containsNone("ab1", "xyz") = true
  1256. * StringUtils.containsNone("abz", "xyz") = false
  1257. * </pre>
  1258. *
  1259. * @param str the String to check, may be null
  1260. * @param invalidChars a String of invalid chars, may be null
  1261. * @return true if it contains none of the invalid chars, or is null
  1262. * @since 2.0
  1263. */
  1264. public static boolean containsNone(String str, String invalidChars) {
  1265. if (str == null || invalidChars == null) {
  1266. return true;
  1267. }
  1268. return containsNone(str, invalidChars.toCharArray());
  1269. }
  1270. // IndexOfAny strings
  1271. //-----------------------------------------------------------------------
  1272. /**
  1273. * <p>Find the first index of any of a set of potential substrings.</p>
  1274. *
  1275. * <p>A <code>null</code> String will return <code>-1</code>.
  1276. * A <code>null</code> or zero length search array will return <code>-1</code>.
  1277. * A <code>null</code> search array entry will be ignored, but a search
  1278. * array containing "" will return <code>0</code> if <code>str</code> is not
  1279. * null. This method uses {@link String#indexOf(String)}.</p>
  1280. *
  1281. * <pre>
  1282. * StringUtils.indexOfAny(null, *) = -1
  1283. * StringUtils.indexOfAny(*, null) = -1
  1284. * StringUtils.indexOfAny(*, []) = -1
  1285. * StringUtils.indexOfAny("zzabyycdxx", ["ab","cd"]) = 2
  1286. * StringUtils.indexOfAny("zzabyycdxx", ["cd","ab"]) = 2
  1287. * StringUtils.indexOfAny("zzabyycdxx", ["mn","op"]) = -1
  1288. * StringUtils.indexOfAny("zzabyycdxx", ["zab","aby"]) = 1
  1289. * StringUtils.indexOfAny("zzabyycdxx", [""]) = 0
  1290. * StringUtils.indexOfAny("", [""]) = 0
  1291. * StringUtils.indexOfAny("", ["a"]) = -1
  1292. * </pre>
  1293. *
  1294. * @param str the String to check, may be null
  1295. * @param searchStrs the Strings to search for, may be null
  1296. * @return the first index of any of the searchStrs in str, -1 if no match
  1297. */
  1298. public static int indexOfAny(String str, String[] searchStrs) {
  1299. if ((str == null) || (searchStrs == null)) {
  1300. return -1;
  1301. }
  1302. int sz = searchStrs.length;
  1303. // String's can't have a MAX_VALUEth index.
  1304. int ret = Integer.MAX_VALUE;
  1305. int tmp = 0;
  1306. for (int i = 0; i < sz; i++) {
  1307. String search = searchStrs[i];
  1308. if (search == null) {
  1309. continue;
  1310. }
  1311. tmp = str.indexOf(search);
  1312. if (tmp == -1) {
  1313. continue;
  1314. }
  1315. if (tmp < ret) {
  1316. ret = tmp;
  1317. }
  1318. }
  1319. return (ret == Integer.MAX_VALUE) ? -1 : ret;
  1320. }
  1321. /**
  1322. * <p>Find the latest index of any of a set of potential substrings.</p>
  1323. *
  1324. * <p>A <code>null</code> String will return <code>-1</code>.
  1325. * A <code>null</code> search array will return <code>-1</code>.
  1326. * A <code>null</code> or zero length search array entry will be ignored,
  1327. * but a search array containing "" will return the length of <code>str</code>
  1328. * if <code>str</code> is not null. This method uses {@link String#indexOf(String)}</p>
  1329. *
  1330. * <pre>
  1331. * StringUtils.lastIndexOfAny(null, *) = -1
  1332. * StringUtils.lastIndexOfAny(*, null) = -1
  1333. * StringUtils.lastIndexOfAny(*, []) = -1
  1334. * StringUtils.lastIndexOfAny(*, [null]) = -1
  1335. * StringUtils.lastIndexOfAny("zzabyycdxx", ["ab","cd"]) = 6
  1336. * StringUtils.lastIndexOfAny("zzabyycdxx", ["cd","ab"]) = 6
  1337. * StringUtils.lastIndexOfAny("zzabyycdxx", ["mn","op"]) = -1
  1338. * StringUtils.lastIndexOfAny("zzabyycdxx", ["mn","op"]) = -1
  1339. * StringUtils.lastIndexOfAny("zzabyycdxx", ["mn",""]) = 10
  1340. * </pre>
  1341. *
  1342. * @param str the String to check, may be null
  1343. * @param searchStrs the Strings to search for, may be null
  1344. * @return the last index of any of the Strings, -1 if no match
  1345. */
  1346. public static int lastIndexOfAny(String str, String[] searchStrs) {
  1347. if ((str == null) || (searchStrs == null)) {
  1348. return -1;
  1349. }
  1350. int sz = searchStrs.length;
  1351. int ret = -1;
  1352. int tmp = 0;
  1353. for (int i = 0; i < sz; i++) {
  1354. String search = searchStrs[i];
  1355. if (search == null) {
  1356. continue;
  1357. }
  1358. tmp = str.lastIndexOf(search);
  1359. if (tmp > ret) {
  1360. ret = tmp;
  1361. }
  1362. }
  1363. return ret;
  1364. }
  1365. // Substring
  1366. //-----------------------------------------------------------------------
  1367. /**
  1368. * <p>Gets a substring from the specified String avoiding exceptions.</p>
  1369. *
  1370. * <p>A negative start position can be used to start <code>n</code>
  1371. * characters from the end of the String.</p>
  1372. *
  1373. * <p>A <code>null</code> String will return <code>null</code>.
  1374. * An empty ("") String will return "".</p>
  1375. *
  1376. * <pre>
  1377. * StringUtils.substring(null, *) = null
  1378. * StringUtils.substring("", *) = ""
  1379. * StringUtils.substring("abc", 0) = "abc"
  1380. * StringUtils.substring("abc", 2) = "c"
  1381. * StringUtils.substring("abc", 4) = ""
  1382. * StringUtils.substring("abc", -2) = "bc"
  1383. * StringUtils.substring("abc", -4) = "abc"
  1384. * </pre>
  1385. *
  1386. * @param str the String to get the substring from, may be null
  1387. * @param start the position to start from, negative means
  1388. * count back from the end of the String by this many characters
  1389. * @return substring from start position, <code>null</code> if null String input
  1390. */
  1391. public