Coverage Report - org.apache.commons.lang3.StringUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
StringUtils
98%
1313/1333
97%
1173/1209
5.783
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.lang3;
 18  
 
 19  
 import java.io.UnsupportedEncodingException;
 20  
 import java.text.Normalizer;
 21  
 import java.util.ArrayList;
 22  
 import java.util.Arrays;
 23  
 import java.util.Iterator;
 24  
 import java.util.List;
 25  
 import java.util.Locale;
 26  
 import java.util.regex.Pattern;
 27  
 
 28  
 /**
 29  
  * <p>Operations on {@link java.lang.String} that are
 30  
  * {@code null} safe.</p>
 31  
  *
 32  
  * <ul>
 33  
  *  <li><b>IsEmpty/IsBlank</b>
 34  
  *      - checks if a String contains text</li>
 35  
  *  <li><b>Trim/Strip</b>
 36  
  *      - removes leading and trailing whitespace</li>
 37  
  *  <li><b>Equals</b>
 38  
  *      - compares two strings null-safe</li>
 39  
  *  <li><b>startsWith</b>
 40  
  *      - check if a String starts with a prefix null-safe</li>
 41  
  *  <li><b>endsWith</b>
 42  
  *      - check if a String ends with a suffix null-safe</li>
 43  
  *  <li><b>IndexOf/LastIndexOf/Contains</b>
 44  
  *      - null-safe index-of checks
 45  
  *  <li><b>IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut</b>
 46  
  *      - index-of any of a set of Strings</li>
 47  
  *  <li><b>ContainsOnly/ContainsNone/ContainsAny</b>
 48  
  *      - does String contains only/none/any of these characters</li>
 49  
  *  <li><b>Substring/Left/Right/Mid</b>
 50  
  *      - null-safe substring extractions</li>
 51  
  *  <li><b>SubstringBefore/SubstringAfter/SubstringBetween</b>
 52  
  *      - substring extraction relative to other strings</li>
 53  
  *  <li><b>Split/Join</b>
 54  
  *      - splits a String into an array of substrings and vice versa</li>
 55  
  *  <li><b>Remove/Delete</b>
 56  
  *      - removes part of a String</li>
 57  
  *  <li><b>Replace/Overlay</b>
 58  
  *      - Searches a String and replaces one String with another</li>
 59  
  *  <li><b>Chomp/Chop</b>
 60  
  *      - removes the last part of a String</li>
 61  
  *  <li><b>LeftPad/RightPad/Center/Repeat</b>
 62  
  *      - pads a String</li>
 63  
  *  <li><b>UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize</b>
 64  
  *      - changes the case of a String</li>
 65  
  *  <li><b>CountMatches</b>
 66  
  *      - counts the number of occurrences of one String in another</li>
 67  
  *  <li><b>IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable</b>
 68  
  *      - checks the characters in a String</li>
 69  
  *  <li><b>DefaultString</b>
 70  
  *      - protects against a null input String</li>
 71  
  *  <li><b>Reverse/ReverseDelimited</b>
 72  
  *      - reverses a String</li>
 73  
  *  <li><b>Abbreviate</b>
 74  
  *      - abbreviates a string using ellipsis</li>
 75  
  *  <li><b>Difference</b>
 76  
  *      - compares Strings and reports on their differences</li>
 77  
  *  <li><b>LevenshteinDistance</b>
 78  
  *      - the number of changes needed to change one String into another</li>
 79  
  * </ul>
 80  
  *
 81  
  * <p>The {@code StringUtils} class defines certain words related to
 82  
  * String handling.</p>
 83  
  *
 84  
  * <ul>
 85  
  *  <li>null - {@code null}</li>
 86  
  *  <li>empty - a zero-length string ({@code ""})</li>
 87  
  *  <li>space - the space character ({@code ' '}, char 32)</li>
 88  
  *  <li>whitespace - the characters defined by {@link Character#isWhitespace(char)}</li>
 89  
  *  <li>trim - the characters &lt;= 32 as in {@link String#trim()}</li>
 90  
  * </ul>
 91  
  *
 92  
  * <p>{@code StringUtils} handles {@code null} input Strings quietly.
 93  
  * That is to say that a {@code null} input will return {@code null}.
 94  
  * Where a {@code boolean} or {@code int} is being returned
 95  
  * details vary by method.</p>
 96  
  *
 97  
  * <p>A side effect of the {@code null} handling is that a
 98  
  * {@code NullPointerException} should be considered a bug in
 99  
  * {@code StringUtils}.</p>
 100  
  *
 101  
  * <p>Methods in this class give sample code to explain their operation.
 102  
  * The symbol {@code *} is used to indicate any input including {@code null}.</p>
 103  
  *
 104  
  * <p>#ThreadSafe#</p>
 105  
  * @see java.lang.String
 106  
  * @since 1.0
 107  
  * @version $Id: StringUtils.java 1437065 2013-01-22 17:29:33Z ggregory $
 108  
  */
 109  
 //@Immutable
 110  
 public class StringUtils {
 111  
     // Performance testing notes (JDK 1.4, Jul03, scolebourne)
 112  
     // Whitespace:
 113  
     // Character.isWhitespace() is faster than WHITESPACE.indexOf()
 114  
     // where WHITESPACE is a string of all whitespace characters
 115  
     //
 116  
     // Character access:
 117  
     // String.charAt(n) versus toCharArray(), then array[n]
 118  
     // String.charAt(n) is about 15% worse for a 10K string
 119  
     // They are about equal for a length 50 string
 120  
     // String.charAt(n) is about 4 times better for a length 3 string
 121  
     // String.charAt(n) is best bet overall
 122  
     //
 123  
     // Append:
 124  
     // String.concat about twice as fast as StringBuffer.append
 125  
     // (not sure who tested this)
 126  
 
 127  
     /**
 128  
      * A String for a space character.
 129  
      * 
 130  
      * @since 3.2
 131  
      */
 132  
     public static final String SPACE = " ";
 133  
 
 134  
     /**
 135  
      * The empty String {@code ""}.
 136  
      * @since 2.0
 137  
      */
 138  
     public static final String EMPTY = "";
 139  
 
 140  
     /**
 141  
      * Represents a failed index search.
 142  
      * @since 2.1
 143  
      */
 144  
     public static final int INDEX_NOT_FOUND = -1;
 145  
 
 146  
     /**
 147  
      * <p>The maximum size to which the padding constant(s) can expand.</p>
 148  
      */
 149  
     private static final int PAD_LIMIT = 8192;
 150  
 
 151  
     /**
 152  
      * A regex pattern for recognizing blocks of whitespace characters.
 153  
      * The apparent convolutedness of the pattern serves the purpose of
 154  
      * ignoring "blocks" consisting of only a single space:  the pattern
 155  
      * is used only to normalize whitespace, condensing "blocks" down to a
 156  
      * single space, thus matching the same would likely cause a great
 157  
      * many noop replacements.
 158  
      */
 159  1
     private static final Pattern WHITESPACE_PATTERN = Pattern.compile("(?: \\s|[\\s&&[^ ]])\\s*");
 160  
 
 161  
     /**
 162  
      * <p>{@code StringUtils} instances should NOT be constructed in
 163  
      * standard programming. Instead, the class should be used as
 164  
      * {@code StringUtils.trim(" foo ");}.</p>
 165  
      *
 166  
      * <p>This constructor is public to permit tools that require a JavaBean
 167  
      * instance to operate.</p>
 168  
      */
 169  
     public StringUtils() {
 170  1
         super();
 171  1
     }
 172  
 
 173  
     // Empty checks
 174  
     //-----------------------------------------------------------------------
 175  
     /**
 176  
      * <p>Checks if a CharSequence is empty ("") or null.</p>
 177  
      *
 178  
      * <pre>
 179  
      * StringUtils.isEmpty(null)      = true
 180  
      * StringUtils.isEmpty("")        = true
 181  
      * StringUtils.isEmpty(" ")       = false
 182  
      * StringUtils.isEmpty("bob")     = false
 183  
      * StringUtils.isEmpty("  bob  ") = false
 184  
      * </pre>
 185  
      *
 186  
      * <p>NOTE: This method changed in Lang version 2.0.
 187  
      * It no longer trims the CharSequence.
 188  
      * That functionality is available in isBlank().</p>
 189  
      *
 190  
      * @param cs  the CharSequence to check, may be null
 191  
      * @return {@code true} if the CharSequence is empty or null
 192  
      * @since 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence)
 193  
      */
 194  
     public static boolean isEmpty(final CharSequence cs) {
 195  2434
         return cs == null || cs.length() == 0;
 196  
     }
 197  
 
 198  
     /**
 199  
      * <p>Checks if a CharSequence is not empty ("") and not null.</p>
 200  
      *
 201  
      * <pre>
 202  
      * StringUtils.isNotEmpty(null)      = false
 203  
      * StringUtils.isNotEmpty("")        = false
 204  
      * StringUtils.isNotEmpty(" ")       = true
 205  
      * StringUtils.isNotEmpty("bob")     = true
 206  
      * StringUtils.isNotEmpty("  bob  ") = true
 207  
      * </pre>
 208  
      *
 209  
      * @param cs  the CharSequence to check, may be null
 210  
      * @return {@code true} if the CharSequence is not empty and not null
 211  
      * @since 3.0 Changed signature from isNotEmpty(String) to isNotEmpty(CharSequence)
 212  
      */
 213  
     public static boolean isNotEmpty(final CharSequence cs) {
 214  56
         return !StringUtils.isEmpty(cs);
 215  
     }
 216  
 
 217  
     /**
 218  
      * <p>Checks if a CharSequence is whitespace, empty ("") or null.</p>
 219  
      *
 220  
      * <pre>
 221  
      * StringUtils.isBlank(null)      = true
 222  
      * StringUtils.isBlank("")        = true
 223  
      * StringUtils.isBlank(" ")       = true
 224  
      * StringUtils.isBlank("bob")     = false
 225  
      * StringUtils.isBlank("  bob  ") = false
 226  
      * </pre>
 227  
      *
 228  
      * @param cs  the CharSequence to check, may be null
 229  
      * @return {@code true} if the CharSequence is null, empty or whitespace
 230  
      * @since 2.0
 231  
      * @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence)
 232  
      */
 233  
     public static boolean isBlank(final CharSequence cs) {
 234  
         int strLen;
 235  156
         if (cs == null || (strLen = cs.length()) == 0) {
 236  17
             return true;
 237  
         }
 238  239
         for (int i = 0; i < strLen; i++) {
 239  228
             if (Character.isWhitespace(cs.charAt(i)) == false) {
 240  128
                 return false;
 241  
             }
 242  
         }
 243  11
         return true;
 244  
     }
 245  
 
 246  
     /**
 247  
      * <p>Checks if a CharSequence is not empty (""), not null and not whitespace only.</p>
 248  
      *
 249  
      * <pre>
 250  
      * StringUtils.isNotBlank(null)      = false
 251  
      * StringUtils.isNotBlank("")        = false
 252  
      * StringUtils.isNotBlank(" ")       = false
 253  
      * StringUtils.isNotBlank("bob")     = true
 254  
      * StringUtils.isNotBlank("  bob  ") = true
 255  
      * </pre>
 256  
      *
 257  
      * @param cs  the CharSequence to check, may be null
 258  
      * @return {@code true} if the CharSequence is
 259  
      *  not empty and not null and not whitespace
 260  
      * @since 2.0
 261  
      * @since 3.0 Changed signature from isNotBlank(String) to isNotBlank(CharSequence)
 262  
      */
 263  
     public static boolean isNotBlank(final CharSequence cs) {
 264  5
         return !StringUtils.isBlank(cs);
 265  
     }
 266  
 
 267  
     // Trim
 268  
     //-----------------------------------------------------------------------
 269  
     /**
 270  
      * <p>Removes control characters (char &lt;= 32) from both
 271  
      * ends of this String, handling {@code null} by returning
 272  
      * {@code null}.</p>
 273  
      *
 274  
      * <p>The String is trimmed using {@link String#trim()}.
 275  
      * Trim removes start and end characters &lt;= 32.
 276  
      * To strip whitespace use {@link #strip(String)}.</p>
 277  
      *
 278  
      * <p>To trim your choice of characters, use the
 279  
      * {@link #strip(String, String)} methods.</p>
 280  
      *
 281  
      * <pre>
 282  
      * StringUtils.trim(null)          = null
 283  
      * StringUtils.trim("")            = ""
 284  
      * StringUtils.trim("     ")       = ""
 285  
      * StringUtils.trim("abc")         = "abc"
 286  
      * StringUtils.trim("    abc    ") = "abc"
 287  
      * </pre>
 288  
      *
 289  
      * @param str  the String to be trimmed, may be null
 290  
      * @return the trimmed string, {@code null} if null String input
 291  
      */
 292  
     public static String trim(final String str) {
 293  34
         return str == null ? null : str.trim();
 294  
     }
 295  
 
 296  
     /**
 297  
      * <p>Removes control characters (char &lt;= 32) from both
 298  
      * ends of this String returning {@code null} if the String is
 299  
      * empty ("") after the trim or if it is {@code null}.
 300  
      *
 301  
      * <p>The String is trimmed using {@link String#trim()}.
 302  
      * Trim removes start and end characters &lt;= 32.
 303  
      * To strip whitespace use {@link #stripToNull(String)}.</p>
 304  
      *
 305  
      * <pre>
 306  
      * StringUtils.trimToNull(null)          = null
 307  
      * StringUtils.trimToNull("")            = null
 308  
      * StringUtils.trimToNull("     ")       = null
 309  
      * StringUtils.trimToNull("abc")         = "abc"
 310  
      * StringUtils.trimToNull("    abc    ") = "abc"
 311  
      * </pre>
 312  
      *
 313  
      * @param str  the String to be trimmed, may be null
 314  
      * @return the trimmed String,
 315  
      *  {@code null} if only chars &lt;= 32, empty or null String input
 316  
      * @since 2.0
 317  
      */
 318  
     public static String trimToNull(final String str) {
 319  9
         final String ts = trim(str);
 320  9
         return isEmpty(ts) ? null : ts;
 321  
     }
 322  
 
 323  
     /**
 324  
      * <p>Removes control characters (char &lt;= 32) from both
 325  
      * ends of this String returning an empty String ("") if the String
 326  
      * is empty ("") after the trim or if it is {@code null}.
 327  
      *
 328  
      * <p>The String is trimmed using {@link String#trim()}.
 329  
      * Trim removes start and end characters &lt;= 32.
 330  
      * To strip whitespace use {@link #stripToEmpty(String)}.</p>
 331  
      *
 332  
      * <pre>
 333  
      * StringUtils.trimToEmpty(null)          = ""
 334  
      * StringUtils.trimToEmpty("")            = ""
 335  
      * StringUtils.trimToEmpty("     ")       = ""
 336  
      * StringUtils.trimToEmpty("abc")         = "abc"
 337  
      * StringUtils.trimToEmpty("    abc    ") = "abc"
 338  
      * </pre>
 339  
      *
 340  
      * @param str  the String to be trimmed, may be null
 341  
      * @return the trimmed String, or an empty String if {@code null} input
 342  
      * @since 2.0
 343  
      */
 344  
     public static String trimToEmpty(final String str) {
 345  9
         return str == null ? EMPTY : str.trim();
 346  
     }
 347  
 
 348  
     // Stripping
 349  
     //-----------------------------------------------------------------------
 350  
     /**
 351  
      * <p>Strips whitespace from the start and end of a String.</p>
 352  
      *
 353  
      * <p>This is similar to {@link #trim(String)} but removes whitespace.
 354  
      * Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
 355  
      *
 356  
      * <p>A {@code null} input String returns {@code null}.</p>
 357  
      *
 358  
      * <pre>
 359  
      * StringUtils.strip(null)     = null
 360  
      * StringUtils.strip("")       = ""
 361  
      * StringUtils.strip("   ")    = ""
 362  
      * StringUtils.strip("abc")    = "abc"
 363  
      * StringUtils.strip("  abc")  = "abc"
 364  
      * StringUtils.strip("abc  ")  = "abc"
 365  
      * StringUtils.strip(" abc ")  = "abc"
 366  
      * StringUtils.strip(" ab c ") = "ab c"
 367  
      * </pre>
 368  
      *
 369  
      * @param str  the String to remove whitespace from, may be null
 370  
      * @return the stripped String, {@code null} if null String input
 371  
      */
 372  
     public static String strip(final String str) {
 373  5
         return strip(str, null);
 374  
     }
 375  
 
 376  
     /**
 377  
      * <p>Strips whitespace from the start and end of a String  returning
 378  
      * {@code null} if the String is empty ("") after the strip.</p>
 379  
      *
 380  
      * <p>This is similar to {@link #trimToNull(String)} but removes whitespace.
 381  
      * Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
 382  
      *
 383  
      * <pre>
 384  
      * StringUtils.stripToNull(null)     = null
 385  
      * StringUtils.stripToNull("")       = null
 386  
      * StringUtils.stripToNull("   ")    = null
 387  
      * StringUtils.stripToNull("abc")    = "abc"
 388  
      * StringUtils.stripToNull("  abc")  = "abc"
 389  
      * StringUtils.stripToNull("abc  ")  = "abc"
 390  
      * StringUtils.stripToNull(" abc ")  = "abc"
 391  
      * StringUtils.stripToNull(" ab c ") = "ab c"
 392  
      * </pre>
 393  
      *
 394  
      * @param str  the String to be stripped, may be null
 395  
      * @return the stripped String,
 396  
      *  {@code null} if whitespace, empty or null String input
 397  
      * @since 2.0
 398  
      */
 399  
     public static String stripToNull(String str) {
 400  6
         if (str == null) {
 401  1
             return null;
 402  
         }
 403  5
         str = strip(str, null);
 404  5
         return str.length() == 0 ? null : str;
 405  
     }
 406  
 
 407  
     /**
 408  
      * <p>Strips whitespace from the start and end of a String  returning
 409  
      * an empty String if {@code null} input.</p>
 410  
      *
 411  
      * <p>This is similar to {@link #trimToEmpty(String)} but removes whitespace.
 412  
      * Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
 413  
      *
 414  
      * <pre>
 415  
      * StringUtils.stripToEmpty(null)     = ""
 416  
      * StringUtils.stripToEmpty("")       = ""
 417  
      * StringUtils.stripToEmpty("   ")    = ""
 418  
      * StringUtils.stripToEmpty("abc")    = "abc"
 419  
      * StringUtils.stripToEmpty("  abc")  = "abc"
 420  
      * StringUtils.stripToEmpty("abc  ")  = "abc"
 421  
      * StringUtils.stripToEmpty(" abc ")  = "abc"
 422  
      * StringUtils.stripToEmpty(" ab c ") = "ab c"
 423  
      * </pre>
 424  
      *
 425  
      * @param str  the String to be stripped, may be null
 426  
      * @return the trimmed String, or an empty String if {@code null} input
 427  
      * @since 2.0
 428  
      */
 429  
     public static String stripToEmpty(final String str) {
 430  6
         return str == null ? EMPTY : strip(str, null);
 431  
     }
 432  
 
 433  
     /**
 434  
      * <p>Strips any of a set of characters from the start and end of a String.
 435  
      * This is similar to {@link String#trim()} but allows the characters
 436  
      * to be stripped to be controlled.</p>
 437  
      *
 438  
      * <p>A {@code null} input String returns {@code null}.
 439  
      * An empty string ("") input returns the empty string.</p>
 440  
      *
 441  
      * <p>If the stripChars String is {@code null}, whitespace is
 442  
      * stripped as defined by {@link Character#isWhitespace(char)}.
 443  
      * Alternatively use {@link #strip(String)}.</p>
 444  
      *
 445  
      * <pre>
 446  
      * StringUtils.strip(null, *)          = null
 447  
      * StringUtils.strip("", *)            = ""
 448  
      * StringUtils.strip("abc", null)      = "abc"
 449  
      * StringUtils.strip("  abc", null)    = "abc"
 450  
      * StringUtils.strip("abc  ", null)    = "abc"
 451  
      * StringUtils.strip(" abc ", null)    = "abc"
 452  
      * StringUtils.strip("  abcyx", "xyz") = "  abc"
 453  
      * </pre>
 454  
      *
 455  
      * @param str  the String to remove characters from, may be null
 456  
      * @param stripChars  the characters to remove, null treated as whitespace
 457  
      * @return the stripped String, {@code null} if null String input
 458  
      */
 459  
     public static String strip(String str, final String stripChars) {
 460  45
         if (isEmpty(str)) {
 461  13
             return str;
 462  
         }
 463  32
         str = stripStart(str, stripChars);
 464  32
         return stripEnd(str, stripChars);
 465  
     }
 466  
 
 467  
     /**
 468  
      * <p>Strips any of a set of characters from the start of a String.</p>
 469  
      *
 470  
      * <p>A {@code null} input String returns {@code null}.
 471  
      * An empty string ("") input returns the empty string.</p>
 472  
      *
 473  
      * <p>If the stripChars String is {@code null}, whitespace is
 474  
      * stripped as defined by {@link Character#isWhitespace(char)}.</p>
 475  
      *
 476  
      * <pre>
 477  
      * StringUtils.stripStart(null, *)          = null
 478  
      * StringUtils.stripStart("", *)            = ""
 479  
      * StringUtils.stripStart("abc", "")        = "abc"
 480  
      * StringUtils.stripStart("abc", null)      = "abc"
 481  
      * StringUtils.stripStart("  abc", null)    = "abc"
 482  
      * StringUtils.stripStart("abc  ", null)    = "abc  "
 483  
      * StringUtils.stripStart(" abc ", null)    = "abc "
 484  
      * StringUtils.stripStart("yxabc  ", "xyz") = "abc  "
 485  
      * </pre>
 486  
      *
 487  
      * @param str  the String to remove characters from, may be null
 488  
      * @param stripChars  the characters to remove, null treated as whitespace
 489  
      * @return the stripped String, {@code null} if null String input
 490  
      */
 491  
     public static String stripStart(final String str, final String stripChars) {
 492  
         int strLen;
 493  52
         if (str == null || (strLen = str.length()) == 0) {
 494  8
             return str;
 495  
         }
 496  44
         int start = 0;
 497  44
         if (stripChars == null) {
 498  263
             while (start != strLen && Character.isWhitespace(str.charAt(start))) {
 499  240
                 start++;
 500  
             }
 501  21
         } else if (stripChars.length() == 0) {
 502  8
             return str;
 503  
         } else {
 504  41
             while (start != strLen && stripChars.indexOf(str.charAt(start)) != INDEX_NOT_FOUND) {
 505  28
                 start++;
 506  
             }
 507  
         }
 508  36
         return str.substring(start);
 509  
     }
 510  
 
 511  
     /**
 512  
      * <p>Strips any of a set of characters from the end of a String.</p>
 513  
      *
 514  
      * <p>A {@code null} input String returns {@code null}.
 515  
      * An empty string ("") input returns the empty string.</p>
 516  
      *
 517  
      * <p>If the stripChars String is {@code null}, whitespace is
 518  
      * stripped as defined by {@link Character#isWhitespace(char)}.</p>
 519  
      *
 520  
      * <pre>
 521  
      * StringUtils.stripEnd(null, *)          = null
 522  
      * StringUtils.stripEnd("", *)            = ""
 523  
      * StringUtils.stripEnd("abc", "")        = "abc"
 524  
      * StringUtils.stripEnd("abc", null)      = "abc"
 525  
      * StringUtils.stripEnd("  abc", null)    = "  abc"
 526  
      * StringUtils.stripEnd("abc  ", null)    = "abc"
 527  
      * StringUtils.stripEnd(" abc ", null)    = " abc"
 528  
      * StringUtils.stripEnd("  abcyx", "xyz") = "  abc"
 529  
      * StringUtils.stripEnd("120.00", ".0")   = "12"
 530  
      * </pre>
 531  
      *
 532  
      * @param str  the String to remove characters from, may be null
 533  
      * @param stripChars  the set of characters to remove, null treated as whitespace
 534  
      * @return the stripped String, {@code null} if null String input
 535  
      */
 536  
     public static String stripEnd(final String str, final String stripChars) {
 537  
         int end;
 538  54
         if (str == null || (end = str.length()) == 0) {
 539  15
             return str;
 540  
         }
 541  
 
 542  39
         if (stripChars == null) {
 543  173
             while (end != 0 && Character.isWhitespace(str.charAt(end - 1))) {
 544  156
                 end--;
 545  
             }
 546  22
         } else if (stripChars.length() == 0) {
 547  8
             return str;
 548  
         } else {
 549  45
             while (end != 0 && stripChars.indexOf(str.charAt(end - 1)) != INDEX_NOT_FOUND) {
 550  31
                 end--;
 551  
             }
 552  
         }
 553  31
         return str.substring(0, end);
 554  
     }
 555  
 
 556  
     // StripAll
 557  
     //-----------------------------------------------------------------------
 558  
     /**
 559  
      * <p>Strips whitespace from the start and end of every String in an array.
 560  
      * Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
 561  
      *
 562  
      * <p>A new array is returned each time, except for length zero.
 563  
      * A {@code null} array will return {@code null}.
 564  
      * An empty array will return itself.
 565  
      * A {@code null} array entry will be ignored.</p>
 566  
      *
 567  
      * <pre>
 568  
      * StringUtils.stripAll(null)             = null
 569  
      * StringUtils.stripAll([])               = []
 570  
      * StringUtils.stripAll(["abc", "  abc"]) = ["abc", "abc"]
 571  
      * StringUtils.stripAll(["abc  ", null])  = ["abc", null]
 572  
      * </pre>
 573  
      *
 574  
      * @param strs  the array to remove whitespace from, may be null
 575  
      * @return the stripped Strings, {@code null} if null array input
 576  
      */
 577  
     public static String[] stripAll(final String... strs) {
 578  5
         return stripAll(strs, null);
 579  
     }
 580  
 
 581  
     /**
 582  
      * <p>Strips any of a set of characters from the start and end of every
 583  
      * String in an array.</p>
 584  
      * Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
 585  
      *
 586  
      * <p>A new array is returned each time, except for length zero.
 587  
      * A {@code null} array will return {@code null}.
 588  
      * An empty array will return itself.
 589  
      * A {@code null} array entry will be ignored.
 590  
      * A {@code null} stripChars will strip whitespace as defined by
 591  
      * {@link Character#isWhitespace(char)}.</p>
 592  
      *
 593  
      * <pre>
 594  
      * StringUtils.stripAll(null, *)                = null
 595  
      * StringUtils.stripAll([], *)                  = []
 596  
      * StringUtils.stripAll(["abc", "  abc"], null) = ["abc", "abc"]
 597  
      * StringUtils.stripAll(["abc  ", null], null)  = ["abc", null]
 598  
      * StringUtils.stripAll(["abc  ", null], "yz")  = ["abc  ", null]
 599  
      * StringUtils.stripAll(["yabcz", null], "yz")  = ["abc", null]
 600  
      * </pre>
 601  
      *
 602  
      * @param strs  the array to remove characters from, may be null
 603  
      * @param stripChars  the characters to remove, null treated as whitespace
 604  
      * @return the stripped Strings, {@code null} if null array input
 605  
      */
 606  
     public static String[] stripAll(final String[] strs, final String stripChars) {
 607  
         int strsLen;
 608  8
         if (strs == null || (strsLen = strs.length) == 0) {
 609  4
             return strs;
 610  
         }
 611  4
         final String[] newArr = new String[strsLen];
 612  14
         for (int i = 0; i < strsLen; i++) {
 613  10
             newArr[i] = strip(strs[i], stripChars);
 614  
         }
 615  4
         return newArr;
 616  
     }
 617  
 
 618  
     /**
 619  
      * <p>Removes diacritics (~= accents) from a string. The case will not be altered.</p>
 620  
      * <p>For instance, '&agrave;' will be replaced by 'a'.</p>
 621  
      * <p>Note that ligatures will be left as is.</p>
 622  
      *
 623  
      * <pre>
 624  
      * StringUtils.stripAccents(null)                = null
 625  
      * StringUtils.stripAccents("")                  = ""
 626  
      * StringUtils.stripAccents("control")           = "control"
 627  
      * StringUtils.stripAccents("&eacute;clair")     = "eclair"
 628  
      * </pre>
 629  
      *
 630  
      * @param input String to be stripped
 631  
      * @return input text with diacritics removed
 632  
      *
 633  
      * @since 3.0
 634  
      */
 635  
     // See also Lucene's ASCIIFoldingFilter (Lucene 2.9) that replaces accented characters by their unaccented equivalent (and uncommitted bug fix: https://issues.apache.org/jira/browse/LUCENE-1343?focusedCommentId=12858907&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#action_12858907).
 636  
     public static String stripAccents(final String input) {
 637  6
         if(input == null) {
 638  1
             return null;
 639  
         }
 640  5
         final Pattern pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");//$NON-NLS-1$
 641  5
         final String decomposed = Normalizer.normalize(input, Normalizer.Form.NFD);
 642  
         // Note that this doesn't correctly remove ligatures...
 643  5
         return pattern.matcher(decomposed).replaceAll("");//$NON-NLS-1$
 644  
     }
 645  
 
 646  
     // Equals
 647  
     //-----------------------------------------------------------------------
 648  
     /**
 649  
      * <p>Compares two CharSequences, returning {@code true} if they represent
 650  
      * equal sequences of characters.</p>
 651  
      *
 652  
      * <p>{@code null}s are handled without exceptions. Two {@code null}
 653  
      * references are considered to be equal. The comparison is case sensitive.</p>
 654  
      *
 655  
      * <pre>
 656  
      * StringUtils.equals(null, null)   = true
 657  
      * StringUtils.equals(null, "abc")  = false
 658  
      * StringUtils.equals("abc", null)  = false
 659  
      * StringUtils.equals("abc", "abc") = true
 660  
      * StringUtils.equals("abc", "ABC") = false
 661  
      * </pre>
 662  
      *
 663  
      * @see Object#equals(Object)
 664  
      * @param cs1  the first CharSequence, may be {@code null}
 665  
      * @param cs2  the second CharSequence, may be {@code null}
 666  
      * @return {@code true} if the CharSequences are equal (case-sensitive), or both {@code null}
 667  
      * @since 3.0 Changed signature from equals(String, String) to equals(CharSequence, CharSequence)
 668  
      */
 669  
     public static boolean equals(final CharSequence cs1, final CharSequence cs2) {
 670  270
         if (cs1 == cs2) {
 671  58
             return true;
 672  
         }
 673  212
         if (cs1 == null || cs2 == null) {
 674  4
             return false;
 675  
         }
 676  208
         if (cs1 instanceof String && cs2 instanceof String) {
 677  205
             return cs1.equals(cs2);
 678  
         }
 679  3
         return CharSequenceUtils.regionMatches(cs1, false, 0, cs2, 0, Math.max(cs1.length(), cs2.length()));
 680  
     }
 681  
 
 682  
     /**
 683  
      * <p>Compares two CharSequences, returning {@code true} if they represent
 684  
      * equal sequences of characters, ignoring case.</p>
 685  
      *
 686  
      * <p>{@code null}s are handled without exceptions. Two {@code null}
 687  
      * references are considered equal. Comparison is case insensitive.</p>
 688  
      *
 689  
      * <pre>
 690  
      * StringUtils.equalsIgnoreCase(null, null)   = true
 691  
      * StringUtils.equalsIgnoreCase(null, "abc")  = false
 692  
      * StringUtils.equalsIgnoreCase("abc", null)  = false
 693  
      * StringUtils.equalsIgnoreCase("abc", "abc") = true
 694  
      * StringUtils.equalsIgnoreCase("abc", "ABC") = true
 695  
      * </pre>
 696  
      *
 697  
      * @param str1  the first CharSequence, may be null
 698  
      * @param str2  the second CharSequence, may be null
 699  
      * @return {@code true} if the CharSequence are equal, case insensitive, or
 700  
      *  both {@code null}
 701  
      * @since 3.0 Changed signature from equalsIgnoreCase(String, String) to equalsIgnoreCase(CharSequence, CharSequence)
 702  
      */
 703  
     public static boolean equalsIgnoreCase(final CharSequence str1, final CharSequence str2) {
 704  9
         if (str1 == null || str2 == null) {
 705  3
             return str1 == str2;
 706  6
         } else if (str1 == str2) {
 707  2
             return true;
 708  4
         } else if (str1.length() != str2.length()) {
 709  1
             return false;
 710  
         } else {
 711  3
             return CharSequenceUtils.regionMatches(str1, true, 0, str2, 0, str1.length());
 712  
         }
 713  
     }
 714  
 
 715  
     // IndexOf
 716  
     //-----------------------------------------------------------------------
 717  
     /**
 718  
      * <p>Finds the first index within a CharSequence, handling {@code null}.
 719  
      * This method uses {@link String#indexOf(int, int)} if possible.</p>
 720  
      *
 721  
      * <p>A {@code null} or empty ("") CharSequence will return {@code INDEX_NOT_FOUND (-1)}.</p>
 722  
      *
 723  
      * <pre>
 724  
      * StringUtils.indexOf(null, *)         = -1
 725  
      * StringUtils.indexOf("", *)           = -1
 726  
      * StringUtils.indexOf("aabaabaa", 'a') = 0
 727  
      * StringUtils.indexOf("aabaabaa", 'b') = 2
 728  
      * </pre>
 729  
      *
 730  
      * @param seq  the CharSequence to check, may be null
 731  
      * @param searchChar  the character to find
 732  
      * @return the first index of the search character,
 733  
      *  -1 if no match or {@code null} string input
 734  
      * @since 2.0
 735  
      * @since 3.0 Changed signature from indexOf(String, int) to indexOf(CharSequence, int)
 736  
      */
 737  
     public static int indexOf(final CharSequence seq, final int searchChar) {
 738  5
         if (isEmpty(seq)) {
 739  2
             return INDEX_NOT_FOUND;
 740  
         }
 741  3
         return CharSequenceUtils.indexOf(seq, searchChar, 0);
 742  
     }
 743  
 
 744  
     /**
 745  
      * <p>Finds the first index within a CharSequence from a start position,
 746  
      * handling {@code null}.
 747  
      * This method uses {@link String#indexOf(int, int)} if possible.</p>
 748  
      *
 749  
      * <p>A {@code null} or empty ("") CharSequence will return {@code (INDEX_NOT_FOUND) -1}.
 750  
      * A negative start position is treated as zero.
 751  
      * A start position greater than the string length returns {@code -1}.</p>
 752  
      *
 753  
      * <pre>
 754  
      * StringUtils.indexOf(null, *, *)          = -1
 755  
      * StringUtils.indexOf("", *, *)            = -1
 756  
      * StringUtils.indexOf("aabaabaa", 'b', 0)  = 2
 757  
      * StringUtils.indexOf("aabaabaa", 'b', 3)  = 5
 758  
      * StringUtils.indexOf("aabaabaa", 'b', 9)  = -1
 759  
      * StringUtils.indexOf("aabaabaa", 'b', -1) = 2
 760  
      * </pre>
 761  
      *
 762  
      * @param seq  the CharSequence to check, may be null
 763  
      * @param searchChar  the character to find
 764  
      * @param startPos  the start position, negative treated as zero
 765  
      * @return the first index of the search character,
 766  
      *  -1 if no match or {@code null} string input
 767  
      * @since 2.0
 768  
      * @since 3.0 Changed signature from indexOf(String, int, int) to indexOf(CharSequence, int, int)
 769  
      */
 770  
     public static int indexOf(final CharSequence seq, final int searchChar, final int startPos) {
 771  10
         if (isEmpty(seq)) {
 772  4
             return INDEX_NOT_FOUND;
 773  
         }
 774  6
         return CharSequenceUtils.indexOf(seq, searchChar, startPos);
 775  
     }
 776  
 
 777  
     /**
 778  
      * <p>Finds the first index within a CharSequence, handling {@code null}.
 779  
      * This method uses {@link String#indexOf(String, int)} if possible.</p>
 780  
      *
 781  
      * <p>A {@code null} CharSequence will return {@code -1}.</p>
 782  
      *
 783  
      * <pre>
 784  
      * StringUtils.indexOf(null, *)          = -1
 785  
      * StringUtils.indexOf(*, null)          = -1
 786  
      * StringUtils.indexOf("", "")           = 0
 787  
      * StringUtils.indexOf("", *)            = -1 (except when * = "")
 788  
      * StringUtils.indexOf("aabaabaa", "a")  = 0
 789  
      * StringUtils.indexOf("aabaabaa", "b")  = 2
 790  
      * StringUtils.indexOf("aabaabaa", "ab") = 1
 791  
      * StringUtils.indexOf("aabaabaa", "")   = 0
 792  
      * </pre>
 793  
      *
 794  
      * @param seq  the CharSequence to check, may be null
 795  
      * @param searchSeq  the CharSequence to find, may be null
 796  
      * @return the first index of the search CharSequence,
 797  
      *  -1 if no match or {@code null} string input
 798  
      * @since 2.0
 799  
      * @since 3.0 Changed signature from indexOf(String, String) to indexOf(CharSequence, CharSequence)
 800  
      */
 801  
     public static int indexOf(final CharSequence seq, final CharSequence searchSeq) {
 802  8
         if (seq == null || searchSeq == null) {
 803  2
             return INDEX_NOT_FOUND;
 804  
         }
 805  6
         return CharSequenceUtils.indexOf(seq, searchSeq, 0);
 806  
     }
 807  
 
 808  
     /**
 809  
      * <p>Finds the first index within a CharSequence, handling {@code null}.
 810  
      * This method uses {@link String#indexOf(String, int)} if possible.</p>
 811  
      *
 812  
      * <p>A {@code null} CharSequence will return {@code -1}.
 813  
      * A negative start position is treated as zero.
 814  
      * An empty ("") search CharSequence always matches.
 815  
      * A start position greater than the string length only matches
 816  
      * an empty search CharSequence.</p>
 817  
      *
 818  
      * <pre>
 819  
      * StringUtils.indexOf(null, *, *)          = -1
 820  
      * StringUtils.indexOf(*, null, *)          = -1
 821  
      * StringUtils.indexOf("", "", 0)           = 0
 822  
      * StringUtils.indexOf("", *, 0)            = -1 (except when * = "")
 823  
      * StringUtils.indexOf("aabaabaa", "a", 0)  = 0
 824  
      * StringUtils.indexOf("aabaabaa", "b", 0)  = 2
 825  
      * StringUtils.indexOf("aabaabaa", "ab", 0) = 1
 826  
      * StringUtils.indexOf("aabaabaa", "b", 3)  = 5
 827  
      * StringUtils.indexOf("aabaabaa", "b", 9)  = -1
 828  
      * StringUtils.indexOf("aabaabaa", "b", -1) = 2
 829  
      * StringUtils.indexOf("aabaabaa", "", 2)   = 2
 830  
      * StringUtils.indexOf("abc", "", 9)        = 3
 831  
      * </pre>
 832  
      *
 833  
      * @param seq  the CharSequence to check, may be null
 834  
      * @param searchSeq  the CharSequence to find, may be null
 835  
      * @param startPos  the start position, negative treated as zero
 836  
      * @return the first index of the search CharSequence,
 837  
      *  -1 if no match or {@code null} string input
 838  
      * @since 2.0
 839  
      * @since 3.0 Changed signature from indexOf(String, String, int) to indexOf(CharSequence, CharSequence, int)
 840  
      */
 841  
     public static int indexOf(final CharSequence seq, final CharSequence searchSeq, final int startPos) {
 842  21
         if (seq == null || searchSeq == null) {
 843  6
             return INDEX_NOT_FOUND;
 844  
         }
 845  15
         return CharSequenceUtils.indexOf(seq, searchSeq, startPos);
 846  
     }
 847  
 
 848  
     /**
 849  
      * <p>Finds the n-th index within a CharSequence, handling {@code null}.
 850  
      * This method uses {@link String#indexOf(String)} if possible.</p>
 851  
      *
 852  
      * <p>A {@code null} CharSequence will return {@code -1}.</p>
 853  
      *
 854  
      * <pre>
 855  
      * StringUtils.ordinalIndexOf(null, *, *)          = -1
 856  
      * StringUtils.ordinalIndexOf(*, null, *)          = -1
 857  
      * StringUtils.ordinalIndexOf("", "", *)           = 0
 858  
      * StringUtils.ordinalIndexOf("aabaabaa", "a", 1)  = 0
 859  
      * StringUtils.ordinalIndexOf("aabaabaa", "a", 2)  = 1
 860  
      * StringUtils.ordinalIndexOf("aabaabaa", "b", 1)  = 2
 861  
      * StringUtils.ordinalIndexOf("aabaabaa", "b", 2)  = 5
 862  
      * StringUtils.ordinalIndexOf("aabaabaa", "ab", 1) = 1
 863  
      * StringUtils.ordinalIndexOf("aabaabaa", "ab", 2) = 4
 864  
      * StringUtils.ordinalIndexOf("aabaabaa", "", 1)   = 0
 865  
      * StringUtils.ordinalIndexOf("aabaabaa", "", 2)   = 0
 866  
      * </pre>
 867  
      *
 868  
      * <p>Note that 'head(CharSequence str, int n)' may be implemented as: </p>
 869  
      *
 870  
      * <pre>
 871  
      *   str.substring(0, lastOrdinalIndexOf(str, "\n", n))
 872  
      * </pre>
 873  
      *
 874  
      * @param str  the CharSequence to check, may be null
 875  
      * @param searchStr  the CharSequence to find, may be null
 876  
      * @param ordinal  the n-th {@code searchStr} to find
 877  
      * @return the n-th index of the search CharSequence,
 878  
      *  {@code -1} ({@code INDEX_NOT_FOUND}) if no match or {@code null} string input
 879  
      * @since 2.1
 880  
      * @since 3.0 Changed signature from ordinalIndexOf(String, String, int) to ordinalIndexOf(CharSequence, CharSequence, int)
 881  
      */
 882  
     public static int ordinalIndexOf(final CharSequence str, final CharSequence searchStr, final int ordinal) {
 883  53
         return ordinalIndexOf(str, searchStr, ordinal, false);
 884  
     }
 885  
 
 886  
     /**
 887  
      * <p>Finds the n-th index within a String, handling {@code null}.
 888  
      * This method uses {@link String#indexOf(String)} if possible.</p>
 889  
      *
 890  
      * <p>A {@code null} CharSequence will return {@code -1}.</p>
 891  
      *
 892  
      * @param str  the CharSequence to check, may be null
 893  
      * @param searchStr  the CharSequence to find, may be null
 894  
      * @param ordinal  the n-th {@code searchStr} to find
 895  
      * @param lastIndex true if lastOrdinalIndexOf() otherwise false if ordinalIndexOf()
 896  
      * @return the n-th index of the search CharSequence,
 897  
      *  {@code -1} ({@code INDEX_NOT_FOUND}) if no match or {@code null} string input
 898  
      */
 899  
     // Shared code between ordinalIndexOf(String,String,int) and lastOrdinalIndexOf(String,String,int)
 900  
     private static int ordinalIndexOf(final CharSequence str, final CharSequence searchStr, final int ordinal, final boolean lastIndex) {
 901  64
         if (str == null || searchStr == null || ordinal <= 0) {
 902  30
             return INDEX_NOT_FOUND;
 903  
         }
 904  34
         if (searchStr.length() == 0) {
 905  9
             return lastIndex ? str.length() : 0;
 906  
         }
 907  25
         int found = 0;
 908  25
         int index = lastIndex ? str.length() : INDEX_NOT_FOUND;
 909  
         do {
 910  86
             if (lastIndex) {
 911  9
                 index = CharSequenceUtils.lastIndexOf(str, searchStr, index - 1);
 912  
             } else {
 913  77
                 index = CharSequenceUtils.indexOf(str, searchStr, index + 1);
 914  
             }
 915  86
             if (index < 0) {
 916  4
                 return index;
 917  
             }
 918  82
             found++;
 919  82
         } while (found < ordinal);
 920  21
         return index;
 921  
     }
 922  
 
 923  
     /**
 924  
      * <p>Case in-sensitive find of the first index within a CharSequence.</p>
 925  
      *
 926  
      * <p>A {@code null} CharSequence will return {@code -1}.
 927  
      * A negative start position is treated as zero.
 928  
      * An empty ("") search CharSequence always matches.
 929  
      * A start position greater than the string length only matches
 930  
      * an empty search CharSequence.</p>
 931  
      *
 932  
      * <pre>
 933  
      * StringUtils.indexOfIgnoreCase(null, *)          = -1
 934  
      * StringUtils.indexOfIgnoreCase(*, null)          = -1
 935  
      * StringUtils.indexOfIgnoreCase("", "")           = 0
 936  
      * StringUtils.indexOfIgnoreCase("aabaabaa", "a")  = 0
 937  
      * StringUtils.indexOfIgnoreCase("aabaabaa", "b")  = 2
 938  
      * StringUtils.indexOfIgnoreCase("aabaabaa", "ab") = 1
 939  
      * </pre>
 940  
      *
 941  
      * @param str  the CharSequence to check, may be null
 942  
      * @param searchStr  the CharSequence to find, may be null
 943  
      * @return the first index of the search CharSequence,
 944  
      *  -1 if no match or {@code null} string input
 945  
      * @since 2.5
 946  
      * @since 3.0 Changed signature from indexOfIgnoreCase(String, String) to indexOfIgnoreCase(CharSequence, CharSequence)
 947  
      */
 948  
     public static int indexOfIgnoreCase(final CharSequence str, final CharSequence searchStr) {
 949  11
         return indexOfIgnoreCase(str, searchStr, 0);
 950  
     }
 951  
 
 952  
     /**
 953  
      * <p>Case in-sensitive find of the first index within a CharSequence
 954  
      * from the specified position.</p>
 955  
      *
 956  
      * <p>A {@code null} CharSequence will return {@code -1}.
 957  
      * A negative start position is treated as zero.
 958  
      * An empty ("") search CharSequence always matches.
 959  
      * A start position greater than the string length only matches
 960  
      * an empty search CharSequence.</p>
 961  
      *
 962  
      * <pre>
 963  
      * StringUtils.indexOfIgnoreCase(null, *, *)          = -1
 964  
      * StringUtils.indexOfIgnoreCase(*, null, *)          = -1
 965  
      * StringUtils.indexOfIgnoreCase("", "", 0)           = 0
 966  
      * StringUtils.indexOfIgnoreCase("aabaabaa", "A", 0)  = 0
 967  
      * StringUtils.indexOfIgnoreCase("aabaabaa", "B", 0)  = 2
 968  
      * StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 0) = 1
 969  
      * StringUtils.indexOfIgnoreCase("aabaabaa", "B", 3)  = 5
 970  
      * StringUtils.indexOfIgnoreCase("aabaabaa", "B", 9)  = -1
 971  
      * StringUtils.indexOfIgnoreCase("aabaabaa", "B", -1) = 2
 972  
      * StringUtils.indexOfIgnoreCase("aabaabaa", "", 2)   = 2
 973  
      * StringUtils.indexOfIgnoreCase("abc", "", 9)        = 3
 974  
      * </pre>
 975  
      *
 976  
      * @param str  the CharSequence to check, may be null
 977  
      * @param searchStr  the CharSequence to find, may be null
 978  
      * @param startPos  the start position, negative treated as zero
 979  
      * @return the first index of the search CharSequence,
 980  
      *  -1 if no match or {@code null} string input
 981  
      * @since 2.5
 982  
      * @since 3.0 Changed signature from indexOfIgnoreCase(String, String, int) to indexOfIgnoreCase(CharSequence, CharSequence, int)
 983  
      */
 984  
     public static int indexOfIgnoreCase(final CharSequence str, final CharSequence searchStr, int startPos) {
 985  25
         if (str == null || searchStr == null) {
 986  3
             return INDEX_NOT_FOUND;
 987  
         }
 988  22
         if (startPos < 0) {
 989  1
             startPos = 0;
 990  
         }
 991  22
         final int endLimit = str.length() - searchStr.length() + 1;
 992  22
         if (startPos > endLimit) {
 993  1
             return INDEX_NOT_FOUND;
 994  
         }
 995  21
         if (searchStr.length() == 0) {
 996  3
             return startPos;
 997  
         }
 998  32
         for (int i = startPos; i < endLimit; i++) {
 999  27
             if (CharSequenceUtils.regionMatches(str, true, i, searchStr, 0, searchStr.length())) {
 1000  13
                 return i;
 1001  
             }
 1002  
         }
 1003  5
         return INDEX_NOT_FOUND;
 1004  
     }
 1005  
 
 1006  
     // LastIndexOf
 1007  
     //-----------------------------------------------------------------------
 1008  
     /**
 1009  
      * <p>Finds the last index within a CharSequence, handling {@code null}.
 1010  
      * This method uses {@link String#lastIndexOf(int)} if possible.</p>
 1011  
      *
 1012  
      * <p>A {@code null} or empty ("") CharSequence will return {@code -1}.</p>
 1013  
      *
 1014  
      * <pre>
 1015  
      * StringUtils.lastIndexOf(null, *)         = -1
 1016  
      * StringUtils.lastIndexOf("", *)           = -1
 1017  
      * StringUtils.lastIndexOf("aabaabaa", 'a') = 7
 1018  
      * StringUtils.lastIndexOf("aabaabaa", 'b') = 5
 1019  
      * </pre>
 1020  
      *
 1021  
      * @param seq  the CharSequence to check, may be null
 1022  
      * @param searchChar  the character to find
 1023  
      * @return the last index of the search character,
 1024  
      *  -1 if no match or {@code null} string input
 1025  
      * @since 2.0
 1026  
      * @since 3.0 Changed signature from lastIndexOf(String, int) to lastIndexOf(CharSequence, int)
 1027  
      */
 1028  
     public static int lastIndexOf(final CharSequence seq, final int searchChar) {
 1029  5
         if (isEmpty(seq)) {
 1030  2
             return INDEX_NOT_FOUND;
 1031  
         }
 1032  3
         return CharSequenceUtils.lastIndexOf(seq, searchChar, seq.length());
 1033  
     }
 1034  
 
 1035  
     /**
 1036  
      * <p>Finds the last index within a CharSequence from a start position,
 1037  
      * handling {@code null}.
 1038  
      * This method uses {@link String#lastIndexOf(int, int)} if possible.</p>
 1039  
      *
 1040  
      * <p>A {@code null} or empty ("") CharSequence will return {@code -1}.
 1041  
      * A negative start position returns {@code -1}.
 1042  
      * A start position greater than the string length searches the whole string.</p>
 1043  
      *
 1044  
      * <pre>
 1045  
      * StringUtils.lastIndexOf(null, *, *)          = -1
 1046  
      * StringUtils.lastIndexOf("", *,  *)           = -1
 1047  
      * StringUtils.lastIndexOf("aabaabaa", 'b', 8)  = 5
 1048  
      * StringUtils.lastIndexOf("aabaabaa", 'b', 4)  = 2
 1049  
      * StringUtils.lastIndexOf("aabaabaa", 'b', 0)  = -1
 1050  
      * StringUtils.lastIndexOf("aabaabaa", 'b', 9)  = 5
 1051  
      * StringUtils.lastIndexOf("aabaabaa", 'b', -1) = -1
 1052  
      * StringUtils.lastIndexOf("aabaabaa", 'a', 0)  = 0
 1053  
      * </pre>
 1054  
      *
 1055  
      * @param seq  the CharSequence to check, may be null
 1056  
      * @param searchChar  the character to find
 1057  
      * @param startPos  the start position
 1058  
      * @return the last index of the search character,
 1059  
      *  -1 if no match or {@code null} string input
 1060  
      * @since 2.0
 1061  
      * @since 3.0 Changed signature from lastIndexOf(String, int, int) to lastIndexOf(CharSequence, int, int)
 1062  
      */
 1063  
     public static int lastIndexOf(final CharSequence seq, final int searchChar, final int startPos) {
 1064  11
         if (isEmpty(seq)) {
 1065  4
             return INDEX_NOT_FOUND;
 1066  
         }
 1067  7
         return CharSequenceUtils.lastIndexOf(seq, searchChar, startPos);
 1068  
     }
 1069  
 
 1070  
     /**
 1071  
      * <p>Finds the last index within a CharSequence, handling {@code null}.
 1072  
      * This method uses {@link String#lastIndexOf(String)} if possible.</p>
 1073  
      *
 1074  
      * <p>A {@code null} CharSequence will return {@code -1}.</p>
 1075  
      *
 1076  
      * <pre>
 1077  
      * StringUtils.lastIndexOf(null, *)          = -1
 1078  
      * StringUtils.lastIndexOf(*, null)          = -1
 1079  
      * StringUtils.lastIndexOf("", "")           = 0
 1080  
      * StringUtils.lastIndexOf("aabaabaa", "a")  = 7
 1081  
      * StringUtils.lastIndexOf("aabaabaa", "b")  = 5
 1082  
      * StringUtils.lastIndexOf("aabaabaa", "ab") = 4
 1083  
      * StringUtils.lastIndexOf("aabaabaa", "")   = 8
 1084  
      * </pre>
 1085  
      *
 1086  
      * @param seq  the CharSequence to check, may be null
 1087  
      * @param searchSeq  the CharSequence to find, may be null
 1088  
      * @return the last index of the search String,
 1089  
      *  -1 if no match or {@code null} string input
 1090  
      * @since 2.0
 1091  
      * @since 3.0 Changed signature from lastIndexOf(String, String) to lastIndexOf(CharSequence, CharSequence)
 1092  
      */
 1093  
     public static int lastIndexOf(final CharSequence seq, final CharSequence searchSeq) {
 1094  9
         if (seq == null || searchSeq == null) {
 1095  2
             return INDEX_NOT_FOUND;
 1096  
         }
 1097  7
         return CharSequenceUtils.lastIndexOf(seq, searchSeq, seq.length());
 1098  
     }
 1099  
 
 1100  
     /**
 1101  
      * <p>Finds the n-th last index within a String, handling {@code null}.
 1102  
      * This method uses {@link String#lastIndexOf(String)}.</p>
 1103  
      *
 1104  
      * <p>A {@code null} String will return {@code -1}.</p>
 1105  
      *
 1106  
      * <pre>
 1107  
      * StringUtils.lastOrdinalIndexOf(null, *, *)          = -1
 1108  
      * StringUtils.lastOrdinalIndexOf(*, null, *)          = -1
 1109  
      * StringUtils.lastOrdinalIndexOf("", "", *)           = 0
 1110  
      * StringUtils.lastOrdinalIndexOf("aabaabaa", "a", 1)  = 7
 1111  
      * StringUtils.lastOrdinalIndexOf("aabaabaa", "a", 2)  = 6
 1112  
      * StringUtils.lastOrdinalIndexOf("aabaabaa", "b", 1)  = 5
 1113  
      * StringUtils.lastOrdinalIndexOf("aabaabaa", "b", 2)  = 2
 1114  
      * StringUtils.lastOrdinalIndexOf("aabaabaa", "ab", 1) = 4
 1115  
      * StringUtils.lastOrdinalIndexOf("aabaabaa", "ab", 2) = 1
 1116  
      * StringUtils.lastOrdinalIndexOf("aabaabaa", "", 1)   = 8
 1117  
      * StringUtils.lastOrdinalIndexOf("aabaabaa", "", 2)   = 8
 1118  
      * </pre>
 1119  
      *
 1120  
      * <p>Note that 'tail(CharSequence str, int n)' may be implemented as: </p>
 1121  
      *
 1122  
      * <pre>
 1123  
      *   str.substring(lastOrdinalIndexOf(str, "\n", n) + 1)
 1124  
      * </pre>
 1125  
      *
 1126  
      * @param str  the CharSequence to check, may be null
 1127  
      * @param searchStr  the CharSequence to find, may be null
 1128  
      * @param ordinal  the n-th last {@code searchStr} to find
 1129  
      * @return the n-th last index of the search CharSequence,
 1130  
      *  {@code -1} ({@code INDEX_NOT_FOUND}) if no match or {@code null} string input
 1131  
      * @since 2.5
 1132  
      * @since 3.0 Changed signature from lastOrdinalIndexOf(String, String, int) to lastOrdinalIndexOf(CharSequence, CharSequence, int)
 1133  
      */
 1134  
     public static int lastOrdinalIndexOf(final CharSequence str, final CharSequence searchStr, final int ordinal) {
 1135  11
         return ordinalIndexOf(str, searchStr, ordinal, true);
 1136  
     }
 1137  
 
 1138  
     /**
 1139  
      * <p>Finds the first index within a CharSequence, handling {@code null}.
 1140  
      * This method uses {@link String#lastIndexOf(String, int)} if possible.</p>
 1141  
      *
 1142  
      * <p>A {@code null} CharSequence will return {@code -1}.
 1143  
      * A negative start position returns {@code -1}.
 1144  
      * An empty ("") search CharSequence always matches unless the start position is negative.
 1145  
      * A start position greater than the string length searches the whole string.</p>
 1146  
      *
 1147  
      * <pre>
 1148  
      * StringUtils.lastIndexOf(null, *, *)          = -1
 1149  
      * StringUtils.lastIndexOf(*, null, *)          = -1
 1150  
      * StringUtils.lastIndexOf("aabaabaa", "a", 8)  = 7
 1151  
      * StringUtils.lastIndexOf("aabaabaa", "b", 8)  = 5
 1152  
      * StringUtils.lastIndexOf("aabaabaa", "ab", 8) = 4
 1153  
      * StringUtils.lastIndexOf("aabaabaa", "b", 9)  = 5
 1154  
      * StringUtils.lastIndexOf("aabaabaa", "b", -1) = -1
 1155  
      * StringUtils.lastIndexOf("aabaabaa", "a", 0)  = 0
 1156  
      * StringUtils.lastIndexOf("aabaabaa", "b", 0)  = -1
 1157  
      * </pre>
 1158  
      *
 1159  
      * @param seq  the CharSequence to check, may be null
 1160  
      * @param searchSeq  the CharSequence to find, may be null
 1161  
      * @param startPos  the start position, negative treated as zero
 1162  
      * @return the first index of the search CharSequence,
 1163  
      *  -1 if no match or {@code null} string input
 1164  
      * @since 2.0
 1165  
      * @since 3.0 Changed signature from lastIndexOf(String, String, int) to lastIndexOf(CharSequence, CharSequence, int)
 1166  
      */
 1167  
     public static int lastIndexOf(final CharSequence seq, final CharSequence searchSeq, final int startPos) {
 1168  21
         if (seq == null || searchSeq == null) {
 1169  6
             return INDEX_NOT_FOUND;
 1170  
         }
 1171  15
         return CharSequenceUtils.lastIndexOf(seq, searchSeq, startPos);
 1172  
     }
 1173  
 
 1174  
     /**
 1175  
      * <p>Case in-sensitive find of the last index within a CharSequence.</p>
 1176  
      *
 1177  
      * <p>A {@code null} CharSequence will return {@code -1}.
 1178  
      * A negative start position returns {@code -1}.
 1179  
      * An empty ("") search CharSequence always matches unless the start position is negative.
 1180  
      * A start position greater than the string length searches the whole string.</p>
 1181  
      *
 1182  
      * <pre>
 1183  
      * StringUtils.lastIndexOfIgnoreCase(null, *)          = -1
 1184  
      * StringUtils.lastIndexOfIgnoreCase(*, null)          = -1
 1185  
      * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "A")  = 7
 1186  
      * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B")  = 5
 1187  
      * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "AB") = 4
 1188  
      * </pre>
 1189  
      *
 1190  
      * @param str  the CharSequence to check, may be null
 1191  
      * @param searchStr  the CharSequence to find, may be null
 1192  
      * @return the first index of the search CharSequence,
 1193  
      *  -1 if no match or {@code null} string input
 1194  
      * @since 2.5
 1195  
      * @since 3.0 Changed signature from lastIndexOfIgnoreCase(String, String) to lastIndexOfIgnoreCase(CharSequence, CharSequence)
 1196  
      */
 1197  
     public static int lastIndexOfIgnoreCase(final CharSequence str, final CharSequence searchStr) {
 1198  14
         if (str == null || searchStr == null) {
 1199  3
             return INDEX_NOT_FOUND;
 1200  
         }
 1201  11
         return lastIndexOfIgnoreCase(str, searchStr, str.length());
 1202  
     }
 1203  
 
 1204  
     /**
 1205  
      * <p>Case in-sensitive find of the last index within a CharSequence
 1206  
      * from the specified position.</p>
 1207  
      *
 1208  
      * <p>A {@code null} CharSequence will return {@code -1}.
 1209  
      * A negative start position returns {@code -1}.
 1210  
      * An empty ("") search CharSequence always matches unless the start position is negative.
 1211  
      * A start position greater than the string length searches the whole string.</p>
 1212  
      *
 1213  
      * <pre>
 1214  
      * StringUtils.lastIndexOfIgnoreCase(null, *, *)          = -1
 1215  
      * StringUtils.lastIndexOfIgnoreCase(*, null, *)          = -1
 1216  
      * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "A", 8)  = 7
 1217  
      * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", 8)  = 5
 1218  
      * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "AB", 8) = 4
 1219  
      * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", 9)  = 5
 1220  
      * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", -1) = -1
 1221  
      * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "A", 0)  = 0
 1222  
      * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", 0)  = -1
 1223  
      * </pre>
 1224  
      *
 1225  
      * @param str  the CharSequence to check, may be null
 1226  
      * @param searchStr  the CharSequence to find, may be null
 1227  
      * @param startPos  the start position
 1228  
      * @return the first index of the search CharSequence,
 1229  
      *  -1 if no match or {@code null} input
 1230  
      * @since 2.5
 1231  
      * @since 3.0 Changed signature from lastIndexOfIgnoreCase(String, String, int) to lastIndexOfIgnoreCase(CharSequence, CharSequence, int)
 1232  
      */
 1233  
     public static int lastIndexOfIgnoreCase(final CharSequence str, final CharSequence searchStr, int startPos) {
 1234  32
         if (str == null || searchStr == null) {
 1235  6
             return INDEX_NOT_FOUND;
 1236  
         }
 1237  26
         if (startPos > str.length() - searchStr.length()) {
 1238  15
             startPos = str.length() - searchStr.length();
 1239  
         }
 1240  26
         if (startPos < 0) {
 1241  5
             return INDEX_NOT_FOUND;
 1242  
         }
 1243  21
         if (searchStr.length() == 0) {
 1244  6
             return startPos;
 1245  
         }
 1246  
 
 1247  31
         for (int i = startPos; i >= 0; i--) {
 1248  30
             if (CharSequenceUtils.regionMatches(str, true, i, searchStr, 0, searchStr.length())) {
 1249  14
                 return i;
 1250  
             }
 1251  
         }
 1252  1
         return INDEX_NOT_FOUND;
 1253  
     }
 1254  
 
 1255  
     // Contains
 1256  
     //-----------------------------------------------------------------------
 1257  
     /**
 1258  
      * <p>Checks if CharSequence contains a search character, handling {@code null}.
 1259  
      * This method uses {@link String#indexOf(int)} if possible.</p>
 1260  
      *
 1261  
      * <p>A {@code null} or empty ("") CharSequence will return {@code false}.</p>
 1262  
      *
 1263  
      * <pre>
 1264  
      * StringUtils.contains(null, *)    = false
 1265  
      * StringUtils.contains("", *)      = false
 1266  
      * StringUtils.contains("abc", 'a') = true
 1267  
      * StringUtils.contains("abc", 'z') = false
 1268  
      * </pre>
 1269  
      *
 1270  
      * @param seq  the CharSequence to check, may be null
 1271  
      * @param searchChar  the character to find
 1272  
      * @return true if the CharSequence contains the search character,
 1273  
      *  false if not or {@code null} string input
 1274  
      * @since 2.0
 1275  
      * @since 3.0 Changed signature from contains(String, int) to contains(CharSequence, int)
 1276  
      */
 1277  
     public static boolean contains(final CharSequence seq, final int searchChar) {
 1278  6
         if (isEmpty(seq)) {
 1279  2
             return false;
 1280  
         }
 1281  4
         return CharSequenceUtils.indexOf(seq, searchChar, 0) >= 0;
 1282  
     }
 1283  
 
 1284  
     /**
 1285  
      * <p>Checks if CharSequence contains a search CharSequence, handling {@code null}.
 1286  
      * This method uses {@link String#indexOf(String)} if possible.</p>
 1287  
      *
 1288  
      * <p>A {@code null} CharSequence will return {@code false}.</p>
 1289  
      *
 1290  
      * <pre>
 1291  
      * StringUtils.contains(null, *)     = false
 1292  
      * StringUtils.contains(*, null)     = false
 1293  
      * StringUtils.contains("", "")      = true
 1294  
      * StringUtils.contains("abc", "")   = true
 1295  
      * StringUtils.contains("abc", "a")  = true
 1296  
      * StringUtils.contains("abc", "z")  = false
 1297  
      * </pre>
 1298  
      *
 1299  
      * @param seq  the CharSequence to check, may be null
 1300  
      * @param searchSeq  the CharSequence to find, may be null
 1301  
      * @return true if the CharSequence contains the search CharSequence,
 1302  
      *  false if not or {@code null} string input
 1303  
      * @since 2.0
 1304  
      * @since 3.0 Changed signature from contains(String, String) to contains(CharSequence, CharSequence)
 1305  
      */
 1306  
     public static boolean contains(final CharSequence seq, final CharSequence searchSeq) {
 1307  23
         if (seq == null || searchSeq == null) {
 1308  6
             return false;
 1309  
         }
 1310  17
         return CharSequenceUtils.indexOf(seq, searchSeq, 0) >= 0;
 1311  
     }
 1312  
 
 1313  
     /**
 1314  
      * <p>Checks if CharSequence contains a search CharSequence irrespective of case,
 1315  
      * handling {@code null}. Case-insensitivity is defined as by
 1316  
      * {@link String#equalsIgnoreCase(String)}.
 1317  
      *
 1318  
      * <p>A {@code null} CharSequence will return {@code false}.</p>
 1319  
      *
 1320  
      * <pre>
 1321  
      * StringUtils.contains(null, *) = false
 1322  
      * StringUtils.contains(*, null) = false
 1323  
      * StringUtils.contains("", "") = true
 1324  
      * StringUtils.contains("abc", "") = true
 1325  
      * StringUtils.contains("abc", "a") = true
 1326  
      * StringUtils.contains("abc", "z") = false
 1327  
      * StringUtils.contains("abc", "A") = true
 1328  
      * StringUtils.contains("abc", "Z") = false
 1329  
      * </pre>
 1330  
      *
 1331  
      * @param str  the CharSequence to check, may be null
 1332  
      * @param searchStr  the CharSequence to find, may be null
 1333  
      * @return true if the CharSequence contains the search CharSequence irrespective of
 1334  
      * case or false if not or {@code null} string input
 1335  
      * @since 3.0 Changed signature from containsIgnoreCase(String, String) to containsIgnoreCase(CharSequence, CharSequence)
 1336  
      */
 1337  
     public static boolean containsIgnoreCase(final CharSequence str, final CharSequence searchStr) {
 1338  40
         if (str == null || searchStr == null) {
 1339  7
             return false;
 1340  
         }
 1341  33
         final int len = searchStr.length();
 1342  33
         final int max = str.length() - len;
 1343  35
         for (int i = 0; i <= max; i++) {
 1344  26
             if (CharSequenceUtils.regionMatches(str, true, i, searchStr, 0, len)) {
 1345  24
                 return true;
 1346  
             }
 1347  
         }
 1348  9
         return false;
 1349  
     }
 1350  
 
 1351  
     /**
 1352  
      * Check whether the given CharSequence contains any whitespace characters.
 1353  
      * @param seq the CharSequence to check (may be {@code null})
 1354  
      * @return {@code true} if the CharSequence is not empty and
 1355  
      * contains at least 1 whitespace character
 1356  
      * @see java.lang.Character#isWhitespace
 1357  
      * @since 3.0
 1358  
      */
 1359  
     // From org.springframework.util.StringUtils, under Apache License 2.0
 1360  
     public static boolean containsWhitespace(final CharSequence seq) {
 1361  7
         if (isEmpty(seq)) {
 1362  1
             return false;
 1363  
         }
 1364  6
         final int strLen = seq.length();
 1365  9
         for (int i = 0; i < strLen; i++) {
 1366  8
             if (Character.isWhitespace(seq.charAt(i))) {
 1367  5
                 return true;
 1368  
             }
 1369  
         }
 1370  1
         return false;
 1371  
     }
 1372  
 
 1373  
     // IndexOfAny chars
 1374  
     //-----------------------------------------------------------------------
 1375  
     /**
 1376  
      * <p>Search a CharSequence to find the first index of any
 1377  
      * character in the given set of characters.</p>
 1378  
      *
 1379  
      * <p>A {@code null} String will return {@code -1}.
 1380  
      * A {@code null} or zero length search array will return {@code -1}.</p>
 1381  
      *
 1382  
      * <pre>
 1383  
      * StringUtils.indexOfAny(null, *)                = -1
 1384  
      * StringUtils.indexOfAny("", *)                  = -1
 1385  
      * StringUtils.indexOfAny(*, null)                = -1
 1386  
      * StringUtils.indexOfAny(*, [])                  = -1
 1387  
      * StringUtils.indexOfAny("zzabyycdxx",['z','a']) = 0
 1388  
      * StringUtils.indexOfAny("zzabyycdxx",['b','y']) = 3
 1389  
      * StringUtils.indexOfAny("aba", ['z'])           = -1
 1390  
      * </pre>
 1391  
      *
 1392  
      * @param cs  the CharSequence to check, may be null
 1393  
      * @param searchChars  the chars to search for, may be null
 1394  
      * @return the index of any of the chars, -1 if no match or null input
 1395  
      * @since 2.0
 1396  
      * @since 3.0 Changed signature from indexOfAny(String, char[]) to indexOfAny(CharSequence, char...)
 1397  
      */
 1398  
     public static int indexOfAny(final CharSequence cs, final char... searchChars) {
 1399  22
         if (isEmpty(cs) || ArrayUtils.isEmpty(searchChars)) {
 1400  8
             return INDEX_NOT_FOUND;
 1401  
         }
 1402  14
         final int csLen = cs.length();
 1403  14
         final int csLast = csLen - 1;
 1404  14
         final int searchLen = searchChars.length;
 1405  14
         final int searchLast = searchLen - 1;
 1406  32
         for (int i = 0; i < csLen; i++) {
 1407  28
             final char ch = cs.charAt(i);
 1408  60
             for (int j = 0; j < searchLen; j++) {
 1409  42
                 if (searchChars[j] == ch) {
 1410  14
                     if (i < csLast && j < searchLast && Character.isHighSurrogate(ch)) {
 1411  
                         // ch is a supplementary character
 1412  10
                         if (searchChars[j + 1] == cs.charAt(i + 1)) {
 1413  6
                             return i;
 1414  
                         }
 1415  
                     } else {
 1416  4
                         return i;
 1417  
                     }
 1418  
                 }
 1419  
             }
 1420  
         }
 1421  4
         return INDEX_NOT_FOUND;
 1422  
     }
 1423  
 
 1424  
     /**
 1425  
      * <p>Search a CharSequence to find the first index of any
 1426  
      * character in the given set of characters.</p>
 1427  
      *
 1428  
      * <p>A {@code null} String will return {@code -1}.
 1429  
      * A {@code null} search string will return {@code -1}.</p>
 1430  
      *
 1431  
      * <pre>
 1432  
      * StringUtils.indexOfAny(null, *)            = -1
 1433  
      * StringUtils.indexOfAny("", *)              = -1
 1434  
      * StringUtils.indexOfAny(*, null)            = -1
 1435  
      * StringUtils.indexOfAny(*, "")              = -1
 1436  
      * StringUtils.indexOfAny("zzabyycdxx", "za") = 0
 1437  
      * StringUtils.indexOfAny("zzabyycdxx", "by") = 3
 1438  
      * StringUtils.indexOfAny("aba","z")          = -1
 1439  
      * </pre>
 1440  
      *
 1441  
      * @param cs  the CharSequence to check, may be null
 1442  
      * @param searchChars  the chars to search for, may be null
 1443  
      * @return the index of any of the chars, -1 if no match or null input
 1444  
      * @since 2.0
 1445  
      * @since 3.0 Changed signature from indexOfAny(String, String) to indexOfAny(CharSequence, String)
 1446  
      */
 1447  
     public static int indexOfAny(final CharSequence cs, final String searchChars) {
 1448  15
         if (isEmpty(cs) || isEmpty(searchChars)) {
 1449  8
             return INDEX_NOT_FOUND;
 1450  
         }
 1451  7
         return indexOfAny(cs, searchChars.toCharArray());
 1452  
     }
 1453  
 
 1454  
     // ContainsAny
 1455  
     //-----------------------------------------------------------------------
 1456  
     /**
 1457  
      * <p>Checks if the CharSequence contains any character in the given
 1458  
      * set of characters.</p>
 1459  
      *
 1460  
      * <p>A {@code null} CharSequence will return {@code false}.
 1461  
      * A {@code null} or zero length search array will return {@code false}.</p>
 1462  
      *
 1463  
      * <pre>
 1464  
      * StringUtils.containsAny(null, *)                = false
 1465  
      * StringUtils.containsAny("", *)                  = false
 1466  
      * StringUtils.containsAny(*, null)                = false
 1467  
      * StringUtils.containsAny(*, [])                  = false
 1468  
      * StringUtils.containsAny("zzabyycdxx",['z','a']) = true
 1469  
      * StringUtils.containsAny("zzabyycdxx",['b','y']) = true
 1470  
      * StringUtils.containsAny("aba", ['z'])           = false
 1471  
      * </pre>
 1472  
      *
 1473  
      * @param cs  the CharSequence to check, may be null
 1474  
      * @param searchChars  the chars to search for, may be null
 1475  
      * @return the {@code true} if any of the chars are found,
 1476  
      * {@code false} if no match or null input
 1477  
      * @since 2.4
 1478  
      * @since 3.0 Changed signature from containsAny(String, char[]) to containsAny(CharSequence, char...)
 1479  
      */
 1480  
     public static boolean containsAny(final CharSequence cs, final char... searchChars) {
 1481  51
         if (isEmpty(cs) || ArrayUtils.isEmpty(searchChars)) {
 1482  13
             return false;
 1483  
         }
 1484  38
         final int csLength = cs.length();
 1485  38
         final int searchLength = searchChars.length;
 1486  38
         final int csLast = csLength - 1;
 1487  38
         final int searchLast = searchLength - 1;
 1488  119
         for (int i = 0; i < csLength; i++) {
 1489  104
             final char ch = cs.charAt(i);
 1490  340
             for (int j = 0; j < searchLength; j++) {
 1491  259
                 if (searchChars[j] == ch) {
 1492  31
                     if (Character.isHighSurrogate(ch)) {
 1493  16
                         if (j == searchLast) {
 1494  
                             // missing low surrogate, fine, like String.indexOf(String)
 1495  2
                             return true;
 1496  
                         }
 1497  14
                         if (i < csLast && searchChars[j + 1] == cs.charAt(i + 1)) {
 1498  6
                             return true;
 1499  
                         }
 1500  
                     } else {
 1501  
                         // ch is in the Basic Multilingual Plane
 1502  15
                         return true;
 1503  
                     }
 1504  
                 }
 1505  
             }
 1506  
         }
 1507  15
         return false;
 1508  
     }
 1509  
 
 1510  
     /**
 1511  
      * <p>
 1512  
      * Checks if the CharSequence contains any character in the given set of characters.
 1513  
      * </p>
 1514  
      *
 1515  
      * <p>
 1516  
      * A {@code null} CharSequence will return {@code false}. A {@code null} search CharSequence will return
 1517  
      * {@code false}.
 1518  
      * </p>
 1519  
      *
 1520  
      * <pre>
 1521  
      * StringUtils.containsAny(null, *)            = false
 1522  
      * StringUtils.containsAny("", *)              = false
 1523  
      * StringUtils.containsAny(*, null)            = false
 1524  
      * StringUtils.containsAny(*, "")              = false
 1525  
      * StringUtils.containsAny("zzabyycdxx", "za") = true
 1526  
      * StringUtils.containsAny("zzabyycdxx", "by") = true
 1527  
      * StringUtils.containsAny("aba","z")          = false
 1528  
      * </pre>
 1529  
      *
 1530  
      * @param cs
 1531  
      *            the CharSequence to check, may be null
 1532  
      * @param searchChars
 1533  
      *            the chars to search for, may be null
 1534  
      * @return the {@code true} if any of the chars are found, {@code false} if no match or null input
 1535  
      * @since 2.4
 1536  
      * @since 3.0 Changed signature from containsAny(String, String) to containsAny(CharSequence, CharSequence)
 1537  
      */
 1538  
     public static boolean containsAny(final CharSequence cs, final CharSequence searchChars) {
 1539  20
         if (searchChars == null) {
 1540  3
             return false;
 1541  
         }
 1542  17
         return containsAny(cs, CharSequenceUtils.toCharArray(searchChars));
 1543  
     }
 1544  
 
 1545  
     // IndexOfAnyBut chars
 1546  
     //-----------------------------------------------------------------------
 1547  
     /**
 1548  
      * <p>Searches a CharSequence to find the first index of any
 1549  
      * character not in the given set of characters.</p>
 1550  
      *
 1551  
      * <p>A {@code null} CharSequence will return {@code -1}.
 1552  
      * A {@code null} or zero length search array will return {@code -1}.</p>
 1553  
      *
 1554  
      * <pre>
 1555  
      * StringUtils.indexOfAnyBut(null, *)                              = -1
 1556  
      * StringUtils.indexOfAnyBut("", *)                                = -1
 1557  
      * StringUtils.indexOfAnyBut(*, null)                              = -1
 1558  
      * StringUtils.indexOfAnyBut(*, [])                                = -1
 1559  
      * StringUtils.indexOfAnyBut("zzabyycdxx", new char[] {'z', 'a'} ) = 3
 1560  
      * StringUtils.indexOfAnyBut("aba", new char[] {'z'} )             = 0
 1561  
      * StringUtils.indexOfAnyBut("aba", new char[] {'a', 'b'} )        = -1
 1562  
 
 1563  
      * </pre>
 1564  
      *
 1565  
      * @param cs  the CharSequence to check, may be null
 1566  
      * @param searchChars  the chars to search for, may be null
 1567  
      * @return the index of any of the chars, -1 if no match or null input
 1568  
      * @since 2.0
 1569  
      * @since 3.0 Changed signature from indexOfAnyBut(String, char[]) to indexOfAnyBut(CharSequence, char...)
 1570  
      */
 1571  
     public static int indexOfAnyBut(final CharSequence cs, final char... searchChars) {
 1572  37
         if (isEmpty(cs) || ArrayUtils.isEmpty(searchChars)) {
 1573  8
             return INDEX_NOT_FOUND;
 1574  
         }
 1575  29
         final int csLen = cs.length();
 1576  29
         final int csLast = csLen - 1;
 1577  29
         final int searchLen = searchChars.length;
 1578  29
         final int searchLast = searchLen - 1;
 1579  
         outer:
 1580  30053
         for (int i = 0; i < csLen; i++) {
 1581  30038
             final char ch = cs.charAt(i);
 1582  30065
             for (int j = 0; j < searchLen; j++) {
 1583  30051
                 if (searchChars[j] == ch) {
 1584  30027
                     if (i < csLast && j < searchLast && Character.isHighSurrogate(ch)) {
 1585  5
                         if (searchChars[j + 1] == cs.charAt(i + 1)) {
 1586  2
                             continue outer;
 1587  
                         }
 1588  
                     } else {
 1589  
                         continue outer;
 1590  
                     }
 1591  
                 }
 1592  
             }
 1593  14
             return i;
 1594  
         }
 1595  15
         return INDEX_NOT_FOUND;
 1596  
     }
 1597  
 
 1598  
     /**
 1599  
      * <p>Search a CharSequence to find the first index of any
 1600  
      * character not in the given set of characters.</p>
 1601  
      *
 1602  
      * <p>A {@code null} CharSequence will return {@code -1}.
 1603  
      * A {@code null} or empty search string will return {@code -1}.</p>
 1604  
      *
 1605  
      * <pre>
 1606  
      * StringUtils.indexOfAnyBut(null, *)            = -1
 1607  
      * StringUtils.indexOfAnyBut("", *)              = -1
 1608  
      * StringUtils.indexOfAnyBut(*, null)            = -1
 1609  
      * StringUtils.indexOfAnyBut(*, "")              = -1
 1610  
      * StringUtils.indexOfAnyBut("zzabyycdxx", "za") = 3
 1611  
      * StringUtils.indexOfAnyBut("zzabyycdxx", "")   = -1
 1612  
      * StringUtils.indexOfAnyBut("aba","ab")         = -1
 1613  
      * </pre>
 1614  
      *
 1615  
      * @param seq  the CharSequence to check, may be null
 1616  
      * @param searchChars  the chars to search for, may be null
 1617  
      * @return the index of any of the chars, -1 if no match or null input
 1618  
      * @since 2.0
 1619  
      * @since 3.0 Changed signature from indexOfAnyBut(String, String) to indexOfAnyBut(CharSequence, CharSequence)
 1620  
      */
 1621  
     public static int indexOfAnyBut(final CharSequence seq, final CharSequence searchChars) {
 1622  15
         if (isEmpty(seq) || isEmpty(searchChars)) {
 1623  8
             return INDEX_NOT_FOUND;
 1624  
         }
 1625  7
         final int strLen = seq.length();
 1626  14
         for (int i = 0; i < strLen; i++) {
 1627  13
             final char ch = seq.charAt(i);
 1628  13
             final boolean chFound = CharSequenceUtils.indexOf(searchChars, ch, 0) >= 0;
 1629  13
             if (i + 1 < strLen && Character.isHighSurrogate(ch)) {
 1630  5
                 final char ch2 = seq.charAt(i + 1);
 1631  5
                 if (chFound && CharSequenceUtils.indexOf(searchChars, ch2, 0) < 0) {
 1632  3
                     return i;
 1633  
                 }
 1634  2
             } else {
 1635  8
                 if (!chFound) {
 1636  3
                     return i;
 1637  
                 }
 1638  
             }
 1639  
         }
 1640  1
         return INDEX_NOT_FOUND;
 1641  
     }
 1642  
 
 1643  
     // ContainsOnly
 1644  
     //-----------------------------------------------------------------------
 1645  
     /**
 1646  
      * <p>Checks if the CharSequence contains only certain characters.</p>
 1647  
      *
 1648  
      * <p>A {@code null} CharSequence will return {@code false}.
 1649  
      * A {@code null} valid character array will return {@code false}.
 1650  
      * An empty CharSequence (length()=0) always returns {@code true}.</p>
 1651  
      *
 1652  
      * <pre>
 1653  
      * StringUtils.containsOnly(null, *)       = false
 1654  
      * StringUtils.containsOnly(*, null)       = false
 1655  
      * StringUtils.containsOnly("", *)         = true
 1656  
      * StringUtils.containsOnly("ab", '')      = false
 1657  
      * StringUtils.containsOnly("abab", 'abc') = true
 1658  
      * StringUtils.containsOnly("ab1", 'abc')  = false
 1659  
      * StringUtils.containsOnly("abz", 'abc')  = false
 1660  
      * </pre>
 1661  
      *
 1662  
      * @param cs  the String to check, may be null
 1663  
      * @param valid  an array of valid chars, may be null
 1664  
      * @return true if it only contains valid chars and is non-null
 1665  
      * @since 3.0 Changed signature from containsOnly(String, char[]) to containsOnly(CharSequence, char...)
 1666  
      */
 1667  
     public static boolean containsOnly(final CharSequence cs, final char... valid) {
 1668  
         // All these pre-checks are to maintain API with an older version
 1669  30
         if (valid == null || cs == null) {
 1670  3
             return false;
 1671  
         }
 1672  27
         if (cs.length() == 0) {
 1673  4
             return true;
 1674  
         }
 1675  23
         if (valid.length == 0) {
 1676  2
             return false;
 1677  
         }
 1678  21
         return indexOfAnyBut(cs, valid) == INDEX_NOT_FOUND;
 1679  
     }
 1680  
 
 1681  
     /**
 1682  
      * <p>Checks if the CharSequence contains only certain characters.</p>
 1683  
      *
 1684  
      * <p>A {@code null} CharSequence will return {@code false}.
 1685  
      * A {@code null} valid character String will return {@code false}.
 1686  
      * An empty String (length()=0) always returns {@code true}.</p>
 1687  
      *
 1688  
      * <pre>
 1689  
      * StringUtils.containsOnly(null, *)       = false
 1690  
      * StringUtils.containsOnly(*, null)       = false
 1691  
      * StringUtils.containsOnly("", *)         = true
 1692  
      * StringUtils.containsOnly("ab", "")      = false
 1693  
      * StringUtils.containsOnly("abab", "abc") = true
 1694  
      * StringUtils.containsOnly("ab1", "abc")  = false
 1695  
      * StringUtils.containsOnly("abz", "abc")  = false
 1696  
      * </pre>
 1697  
      *
 1698  
      * @param cs  the CharSequence to check, may be null
 1699  
      * @param validChars  a String of valid chars, may be null
 1700  
      * @return true if it only contains valid chars and is non-null
 1701  
      * @since 2.0
 1702  
      * @since 3.0 Changed signature from containsOnly(String, String) to containsOnly(CharSequence, String)
 1703  
      */
 1704  
     public static boolean containsOnly(final CharSequence cs, final String validChars) {
 1705  15
         if (cs == null || validChars == null) {
 1706  3
             return false;
 1707  
         }
 1708  12
         return containsOnly(cs, validChars.toCharArray());
 1709  
     }
 1710  
 
 1711  
     // ContainsNone
 1712  
     //-----------------------------------------------------------------------
 1713  
     /**
 1714  
      * <p>Checks that the CharSequence does not contain certain characters.</p>
 1715  
      *
 1716  
      * <p>A {@code null} CharSequence will return {@code true}.
 1717  
      * A {@code null} invalid character array will return {@code true}.
 1718  
      * An empty CharSequence (length()=0) always returns true.</p>
 1719  
      *
 1720  
      * <pre>
 1721  
      * StringUtils.containsNone(null, *)       = true
 1722  
      * StringUtils.containsNone(*, null)       = true
 1723  
      * StringUtils.containsNone("", *)         = true
 1724  
      * StringUtils.containsNone("ab", '')      = true
 1725  
      * StringUtils.containsNone("abab", 'xyz') = true
 1726  
      * StringUtils.containsNone("ab1", 'xyz')  = true
 1727  
      * StringUtils.containsNone("abz", 'xyz')  = false
 1728  
      * </pre>
 1729  
      *
 1730  
      * @param cs  the CharSequence to check, may be null
 1731  
      * @param searchChars  an array of invalid chars, may be null
 1732  
      * @return true if it contains none of the invalid chars, or is null
 1733  
      * @since 2.0
 1734  
      * @since 3.0 Changed signature from containsNone(String, char[]) to containsNone(CharSequence, char...)
 1735  
      */
 1736  
     public static boolean containsNone(final CharSequence cs, final char... searchChars) {
 1737  59
         if (cs == null || searchChars == null) {
 1738  3
             return true;
 1739  
         }
 1740  56
         final int csLen = cs.length();
 1741  56
         final int csLast = csLen - 1;
 1742  56
         final int searchLen = searchChars.length;
 1743  56
         final int searchLast = searchLen - 1;
 1744  146
         for (int i = 0; i < csLen; i++) {
 1745  112
             final char ch = cs.charAt(i);
 1746  376
             for (int j = 0; j < searchLen; j++) {
 1747  286
                 if (searchChars[j] == ch) {
 1748  30
                     if (Character.isHighSurrogate(ch)) {
 1749  16
                         if (j == searchLast) {
 1750  
                             // missing low surrogate, fine, like String.indexOf(String)
 1751  2
                             return false;
 1752  
                         }
 1753  14
                         if (i < csLast && searchChars[j + 1] == cs.charAt(i + 1)) {
 1754  6
                             return false;
 1755  
                         }
 1756  
                     } else {
 1757  
                         // ch is in the Basic Multilingual Plane
 1758  14
                         return false;
 1759  
                     }
 1760  
                 }
 1761  
             }
 1762  
         }
 1763  34
         return true;
 1764  
     }
 1765  
 
 1766  
     /**
 1767  
      * <p>Checks that the CharSequence does not contain certain characters.</p>
 1768  
      *
 1769  
      * <p>A {@code null} CharSequence will return {@code true}.
 1770  
      * A {@code null} invalid character array will return {@code true}.
 1771  
      * An empty String ("") always returns true.</p>
 1772  
      *
 1773  
      * <pre>
 1774  
      * StringUtils.containsNone(null, *)       = true
 1775  
      * StringUtils.containsNone(*, null)       = true
 1776  
      * StringUtils.containsNone("", *)         = true
 1777  
      * StringUtils.containsNone("ab", "")      = true
 1778  
      * StringUtils.containsNone("abab", "xyz") = true
 1779  
      * StringUtils.containsNone("ab1", "xyz")  = true
 1780  
      * StringUtils.containsNone("abz", "xyz")  = false
 1781  
      * </pre>
 1782  
      *
 1783  
      * @param cs  the CharSequence to check, may be null
 1784  
      * @param invalidChars  a String of invalid chars, may be null
 1785  
      * @return true if it contains none of the invalid chars, or is null
 1786  
      * @since 2.0
 1787  
      * @since 3.0 Changed signature from containsNone(String, String) to containsNone(CharSequence, String)
 1788  
      */
 1789  
     public static boolean containsNone(final CharSequence cs, final String invalidChars) {
 1790  24
         if (cs == null || invalidChars == null) {
 1791  3
             return true;
 1792  
         }
 1793  21
         return containsNone(cs, invalidChars.toCharArray());
 1794  
     }
 1795  
 
 1796  
     // IndexOfAny strings
 1797  
     //-----------------------------------------------------------------------
 1798  
     /**
 1799  
      * <p>Find the first index of any of a set of potential substrings.</p>
 1800  
      *
 1801  
      * <p>A {@code null} CharSequence will return {@code -1}.
 1802  
      * A {@code null} or zero length search array will return {@code -1}.
 1803  
      * A {@code null} search array entry will be ignored, but a search
 1804  
      * array containing "" will return {@code 0} if {@code str} is not
 1805  
      * null. This method uses {@link String#indexOf(String)} if possible.</p>
 1806  
      *
 1807  
      * <pre>
 1808  
      * StringUtils.indexOfAny(null, *)                     = -1
 1809  
      * StringUtils.indexOfAny(*, null)                     = -1
 1810  
      * StringUtils.indexOfAny(*, [])                       = -1
 1811  
      * StringUtils.indexOfAny("zzabyycdxx", ["ab","cd"])   = 2
 1812  
      * StringUtils.indexOfAny("zzabyycdxx", ["cd","ab"])   = 2
 1813  
      * StringUtils.indexOfAny("zzabyycdxx", ["mn","op"])   = -1
 1814  
      * StringUtils.indexOfAny("zzabyycdxx", ["zab","aby"]) = 1
 1815  
      * StringUtils.indexOfAny("zzabyycdxx", [""])          = 0
 1816  
      * StringUtils.indexOfAny("", [""])                    = 0
 1817  
      * StringUtils.indexOfAny("", ["a"])                   = -1
 1818  
      * </pre>
 1819  
      *
 1820  
      * @param str  the CharSequence to check, may be null
 1821  
      * @param searchStrs  the CharSequences to search for, may be null
 1822  
      * @return the first index of any of the searchStrs in str, -1 if no match
 1823  
      * @since 3.0 Changed signature from indexOfAny(String, String[]) to indexOfAny(CharSequence, CharSequence...)
 1824  
      */
 1825  
     public static int indexOfAny(final CharSequence str, final CharSequence... searchStrs) {
 1826  14
         if (str == null || searchStrs == null) {
 1827  5
             return INDEX_NOT_FOUND;
 1828  
         }
 1829  9
         final int sz = searchStrs.length;
 1830  
 
 1831  
         // String's can't have a MAX_VALUEth index.
 1832  9
         int ret = Integer.MAX_VALUE;
 1833  
 
 1834  9
         int tmp = 0;
 1835  17
         for (int i = 0; i < sz; i++) {
 1836  8
             final CharSequence search = searchStrs[i];
 1837  8
             if (search == null) {
 1838  2
                 continue;
 1839  
             }
 1840  6
             tmp = CharSequenceUtils.indexOf(str, search, 0);
 1841  6
             if (tmp == INDEX_NOT_FOUND) {
 1842  2
                 continue;
 1843  
             }
 1844  
 
 1845  4
             if (tmp < ret) {
 1846  3
                 ret = tmp;
 1847  
             }
 1848  
         }
 1849  
 
 1850  9
         return ret == Integer.MAX_VALUE ? INDEX_NOT_FOUND : ret;
 1851  
     }
 1852  
 
 1853  
     /**
 1854  
      * <p>Find the latest index of any of a set of potential substrings.</p>
 1855  
      *
 1856  
      * <p>A {@code null} CharSequence will return {@code -1}.
 1857  
      * A {@code null} search array will return {@code -1}.
 1858  
      * A {@code null} or zero length search array entry will be ignored,
 1859  
      * but a search array containing "" will return the length of {@code str}
 1860  
      * if {@code str} is not null. This method uses {@link String#indexOf(String)} if possible</p>
 1861  
      *
 1862  
      * <pre>
 1863  
      * StringUtils.lastIndexOfAny(null, *)                   = -1
 1864  
      * StringUtils.lastIndexOfAny(*, null)                   = -1
 1865  
      * StringUtils.lastIndexOfAny(*, [])                     = -1
 1866  
      * StringUtils.lastIndexOfAny(*, [null])                 = -1
 1867  
      * StringUtils.lastIndexOfAny("zzabyycdxx", ["ab","cd"]) = 6
 1868  
      * StringUtils.lastIndexOfAny("zzabyycdxx", ["cd","ab"]) = 6
 1869  
      * StringUtils.lastIndexOfAny("zzabyycdxx", ["mn","op"]) = -1
 1870  
      * StringUtils.lastIndexOfAny("zzabyycdxx", ["mn","op"]) = -1
 1871  
      * StringUtils.lastIndexOfAny("zzabyycdxx", ["mn",""])   = 10
 1872  
      * </pre>
 1873  
      *
 1874  
      * @param str  the CharSequence to check, may be null
 1875  
      * @param searchStrs  the CharSequences to search for, may be null
 1876  
      * @return the last index of any of the CharSequences, -1 if no match
 1877  
      * @since 3.0 Changed signature from lastIndexOfAny(String, String[]) to lastIndexOfAny(CharSequence, CharSequence)
 1878  
      */
 1879  
     public static int lastIndexOfAny(final CharSequence str, final CharSequence... searchStrs) {
 1880  18
         if (str == null || searchStrs == null) {
 1881  7
             return INDEX_NOT_FOUND;
 1882  
         }
 1883  11
         final int sz = searchStrs.length;
 1884  11
         int ret = INDEX_NOT_FOUND;
 1885  11
         int tmp = 0;
 1886  20
         for (int i = 0; i < sz; i++) {
 1887  9
             final CharSequence search = searchStrs[i];
 1888  9
             if (search == null) {
 1889  3
                 continue;
 1890  
             }
 1891  6
             tmp = CharSequenceUtils.lastIndexOf(str, search, str.length());
 1892  6
             if (tmp > ret) {
 1893  4
                 ret = tmp;
 1894  
             }
 1895  
         }
 1896  11
         return ret;
 1897  
     }
 1898  
 
 1899  
     // Substring
 1900  
     //-----------------------------------------------------------------------
 1901  
     /**
 1902  
      * <p>Gets a substring from the specified String avoiding exceptions.</p>
 1903  
      *
 1904  
      * <p>A negative start position can be used to start {@code n}
 1905  
      * characters from the end of the String.</p>
 1906  
      *
 1907  
      * <p>A {@code null} String will return {@code null}.
 1908  
      * An empty ("") String will return "".</p>
 1909  
      *
 1910  
      * <pre>
 1911  
      * StringUtils.substring(null, *)   = null
 1912  
      * StringUtils.substring("", *)     = ""
 1913  
      * StringUtils.substring("abc", 0)  = "abc"
 1914  
      * StringUtils.substring("abc", 2)  = "c"
 1915  
      * StringUtils.substring("abc", 4)  = ""
 1916  
      * StringUtils.substring("abc", -2) = "bc"
 1917  
      * StringUtils.substring("abc", -4) = "abc"
 1918  
      * </pre>
 1919  
      *
 1920  
      * @param str  the String to get the substring from, may be null
 1921  
      * @param start  the position to start from, negative means
 1922  
      *  count back from the end of the String by this many characters
 1923  
      * @return substring from start position, {@code null} if null String input
 1924  
      */
 1925  
     public static String substring(final String str, int start) {
 1926  16
         if (str == null) {
 1927  1
             return null;
 1928  
         }
 1929  
 
 1930  
         // handle negatives, which means last n characters
 1931  15
         if (start < 0) {
 1932  5
             start = str.length() + start; // remember start is negative
 1933  
         }
 1934  
 
 1935  15
         if (start < 0) {
 1936  1
             start = 0;
 1937  
         }
 1938  15
         if (start > str.length()) {
 1939  3
             return EMPTY;
 1940  
         }
 1941  
 
 1942  12
         return str.substring(start);
 1943  
     }
 1944  
 
 1945  
     /**
 1946  
      * <p>Gets a substring from the specified String avoiding exceptions.</p>
 1947  
      *
 1948  
      * <p>A negative start position can be used to start/end {@code n}
 1949  
      * characters from the end of the String.</p>
 1950  
      *
 1951  
      * <p>The returned substring starts with the character in the {@code start}
 1952  
      * position and ends before the {@code end} position. All position counting is
 1953  
      * zero-based -- i.e., to start at the beginning of the string use
 1954  
      * {@code start = 0}. Negative start and end positions can be used to
 1955  
      * specify offsets relative to the end of the String.</p>
 1956  
      *
 1957  
      * <p>If {@code start} is not strictly to the left of {@code end}, ""
 1958  
      * is returned.</p>
 1959  
      *
 1960  
      * <pre>
 1961  
      * StringUtils.substring(null, *, *)    = null
 1962  
      * StringUtils.substring("", * ,  *)    = "";
 1963  
      * StringUtils.substring("abc", 0, 2)   = "ab"
 1964  
      * StringUtils.substring("abc", 2, 0)   = ""
 1965  
      * StringUtils.substring("abc", 2, 4)   = "c"
 1966  
      * StringUtils.substring("abc", 4, 6)   = ""
 1967  
      * StringUtils.substring("abc", 2, 2)   = ""
 1968  
      * StringUtils.substring("abc", -2, -1) = "b"
 1969  
      * StringUtils.substring("abc", -4, 2)  = "ab"
 1970  
      * </pre>
 1971  
      *
 1972  
      * @param str  the String to get the substring from, may be null
 1973  
      * @param start  the position to start from, negative means
 1974  
      *  count back from the end of the String by this many characters
 1975  
      * @param end  the position to end at (exclusive), negative means
 1976  
      *  count back from the end of the String by this many characters
 1977  
      * @return substring from start position to end position,
 1978  
      *  {@code null} if null String input
 1979  
      */
 1980  
     public static String substring(final String str, int start, int end) {
 1981  13
         if (str == null) {
 1982  2
             return null;
 1983  
         }
 1984  
 
 1985  
         // handle negatives
 1986  11
         if (end < 0) {
 1987  4
             end = str.length() + end; // remember end is negative
 1988  
         }
 1989  11
         if (start < 0) {
 1990  4
             start = str.length() + start; // remember start is negative
 1991  
         }
 1992  
 
 1993  
         // check length next
 1994  11
         if (end > str.length()) {
 1995  2
             end = str.length();
 1996  
         }
 1997  
 
 1998  
         // if start is greater than end, return ""
 1999  11
         if (start > end) {
 2000  2
             return EMPTY;
 2001  
         }
 2002  
 
 2003  9
         if (start < 0) {
 2004  1
             start = 0;
 2005  
         }
 2006  9
         if (end < 0) {
 2007  1
             end = 0;
 2008  
         }
 2009  
 
 2010  9
         return str.substring(start, end);
 2011  
     }
 2012  
 
 2013  
     // Left/Right/Mid
 2014  
     //-----------------------------------------------------------------------
 2015  
     /**
 2016  
      * <p>Gets the leftmost {@code len} characters of a String.</p>
 2017  
      *
 2018  
      * <p>If {@code len} characters are not available, or the
 2019  
      * String is {@code null}, the String will be returned without
 2020  
      * an exception. An empty String is returned if len is negative.</p>
 2021  
      *
 2022  
      * <pre>
 2023  
      * StringUtils.left(null, *)    = null
 2024  
      * StringUtils.left(*, -ve)     = ""
 2025  
      * StringUtils.left("", *)      = ""
 2026  
      * StringUtils.left("abc", 0)   = ""
 2027  
      * StringUtils.left("abc", 2)   = "ab"
 2028  
      * StringUtils.left("abc", 4)   = "abc"
 2029  
      * </pre>
 2030  
      *
 2031  
      * @param str  the String to get the leftmost characters from, may be null
 2032  
      * @param len  the length of the required String
 2033  
      * @return the leftmost characters, {@code null} if null String input
 2034  
      */
 2035  
     public static String left(final String str, final int len) {
 2036  10
         if (str == null) {
 2037  3
             return null;
 2038  
         }
 2039  7
         if (len < 0) {
 2040  2
             return EMPTY;
 2041  
         }
 2042  5
         if (str.length() <= len) {
 2043  3
             return str;
 2044  
         }
 2045  2
         return str.substring(0, len);
 2046  
     }
 2047  
 
 2048  
     /**
 2049  
      * <p>Gets the rightmost {@code len} characters of a String.</p>
 2050  
      *
 2051  
      * <p>If {@code len} characters are not available, or the String
 2052  
      * is {@code null}, the String will be returned without an
 2053  
      * an exception. An empty String is returned if len is negative.</p>
 2054  
      *
 2055  
      * <pre>
 2056  
      * StringUtils.right(null, *)    = null
 2057  
      * StringUtils.right(*, -ve)     = ""
 2058  
      * StringUtils.right("", *)      = ""
 2059  
      * StringUtils.right("abc", 0)   = ""
 2060  
      * StringUtils.right("abc", 2)   = "bc"
 2061  
      * StringUtils.right("abc", 4)   = "abc"
 2062  
      * </pre>
 2063  
      *
 2064  
      * @param str  the String to get the rightmost characters from, may be null
 2065  
      * @param len  the length of the required String
 2066  
      * @return the rightmost characters, {@code null} if null String input
 2067  
      */
 2068  
     public static String right(final String str, final int len) {
 2069  10
         if (str == null) {
 2070  3
             return null;
 2071  
         }
 2072  7
         if (len < 0) {
 2073  2
             return EMPTY;
 2074  
         }
 2075  5
         if (str.length() <= len) {
 2076  3
             return str;
 2077  
         }
 2078  2
         return str.substring(str.length() - len);
 2079  
     }
 2080  
 
 2081  
     /**
 2082  
      * <p>Gets {@code len} characters from the middle of a String.</p>
 2083  
      *
 2084  
      * <p>If {@code len} characters are not available, the remainder
 2085  
      * of the String will be returned without an exception. If the
 2086  
      * String is {@code null}, {@code null} will be returned.
 2087  
      * An empty String is returned if len is negative or exceeds the
 2088  
      * length of {@code str}.</p>
 2089  
      *
 2090  
      * <pre>
 2091  
      * StringUtils.mid(null, *, *)    = null
 2092  
      * StringUtils.mid(*, *, -ve)     = ""
 2093  
      * StringUtils.mid("", 0, *)      = ""
 2094  
      * StringUtils.mid("abc", 0, 2)   = "ab"
 2095  
      * StringUtils.mid("abc", 0, 4)   = "abc"
 2096  
      * StringUtils.mid("abc", 2, 4)   = "c"
 2097  
      * StringUtils.mid("abc", 4, 2)   = ""
 2098  
      * StringUtils.mid("abc", -2, 2)  = "ab"
 2099  
      * </pre>
 2100  
      *
 2101  
      * @param str  the String to get the characters from, may be null
 2102  
      * @param pos  the position to start from, negative treated as zero
 2103  
      * @param len  the length of the required String
 2104  
      * @return the middle characters, {@code null} if null String input
 2105  
      */
 2106  
     public static String mid(final String str, int pos, final int len) {
 2107  16
         if (str == null) {
 2108  4
             return null;
 2109  
         }
 2110  12
         if (len < 0 || pos > str.length()) {
 2111  3
             return EMPTY;
 2112  
         }
 2113  9
         if (pos < 0) {
 2114  1
             pos = 0;
 2115  
         }
 2116  9
         if (str.length() <= pos + len) {
 2117  5
             return str.substring(pos);
 2118  
         }
 2119  4
         return str.substring(pos, pos + len);
 2120  
     }
 2121  
 
 2122  
     // SubStringAfter/SubStringBefore
 2123  
     //-----------------------------------------------------------------------
 2124  
     /**
 2125  
      * <p>Gets the substring before the first occurrence of a separator.
 2126  
      * The separator is not returned.</p>
 2127  
      *
 2128  
      * <p>A {@code null} string input will return {@code null}.
 2129  
      * An empty ("") string input will return the empty string.
 2130  
      * A {@code null} separator will return the input string.</p>
 2131  
      *
 2132  
      * <p>If nothing is found, the string input is returned.</p>
 2133  
      *
 2134  
      * <pre>
 2135  
      * StringUtils.substringBefore(null, *)      = null
 2136  
      * StringUtils.substringBefore("", *)        = ""
 2137  
      * StringUtils.substringBefore("abc", "a")   = ""
 2138  
      * StringUtils.substringBefore("abcba", "b") = "a"
 2139  
      * StringUtils.substringBefore("abc", "c")   = "ab"
 2140  
      * StringUtils.substringBefore("abc", "d")   = "abc"
 2141  
      * StringUtils.substringBefore("abc", "")    = ""
 2142  
      * StringUtils.substringBefore("abc", null)  = "abc"
 2143  
      * </pre>
 2144  
      *
 2145  
      * @param str  the String to get a substring from, may be null
 2146  
      * @param separator  the String to search for, may be null
 2147  
      * @return the substring before the first occurrence of the separator,
 2148  
      *  {@code null} if null String input
 2149  
      * @since 2.0
 2150  
      */
 2151  
     public static String substringBefore(final String str, final String separator) {
 2152  14
         if (isEmpty(str) || separator == null) {
 2153  7
             return str;
 2154  
         }
 2155  7
         if (separator.length() == 0) {
 2156  1
             return EMPTY;
 2157  
         }
 2158  6
         final int pos = str.indexOf(separator);
 2159  6
         if (pos == INDEX_NOT_FOUND) {
 2160  1
             return str;
 2161  
         }
 2162  5
         return str.substring(0, pos);
 2163  
     }
 2164  
 
 2165  
     /**
 2166  
      * <p>Gets the substring after the first occurrence of a separator.
 2167  
      * The separator is not returned.</p>
 2168  
      *
 2169  
      * <p>A {@code null} string input will return {@code null}.
 2170  
      * An empty ("") string input will return the empty string.
 2171  
      * A {@code null} separator will return the empty string if the
 2172  
      * input string is not {@code null}.</p>
 2173  
      *
 2174  
      * <p>If nothing is found, the empty string is returned.</p>
 2175  
      *
 2176  
      * <pre>
 2177  
      * StringUtils.substringAfter(null, *)      = null
 2178  
      * StringUtils.substringAfter("", *)        = ""
 2179  
      * StringUtils.substringAfter(*, null)      = ""
 2180  
      * StringUtils.substringAfter("abc", "a")   = "bc"
 2181  
      * StringUtils.substringAfter("abcba", "b") = "cba"
 2182  
      * StringUtils.substringAfter("abc", "c")   = ""
 2183  
      * StringUtils.substringAfter("abc", "d")   = ""
 2184  
      * StringUtils.substringAfter("abc", "")    = "abc"
 2185  
      * </pre>
 2186  
      *
 2187  
      * @param str  the String to get a substring from, may be null
 2188  
      * @param separator  the String to search for, may be null
 2189  
      * @return the substring after the first occurrence of the separator,
 2190  
      *  {@code null} if null String input
 2191  
      * @since 2.0
 2192  
      */
 2193  
     public static String substringAfter(final String str, final String separator) {
 2194  14
         if (isEmpty(str)) {
 2195  6
             return str;
 2196  
         }
 2197  8
         if (separator == null) {
 2198  1
             return EMPTY;
 2199  
         }
 2200  7
         final int pos = str.indexOf(separator);
 2201  7
         if (pos == INDEX_NOT_FOUND) {
 2202  1
             return EMPTY;
 2203  
         }
 2204  6
         return str.substring(pos + separator.length());
 2205  
     }
 2206  
 
 2207  
     /**
 2208  
      * <p>Gets the substring before the last occurrence of a separator.
 2209  
      * The separator is not returned.</p>
 2210  
      *
 2211  
      * <p>A {@code null} string input will return {@code null}.
 2212  
      * An empty ("") string input will return the empty string.
 2213  
      * An empty or {@code null} separator will return the input string.</p>
 2214  
      *
 2215  
      * <p>If nothing is found, the string input is returned.</p>
 2216  
      *
 2217  
      * <pre>
 2218  
      * StringUtils.substringBeforeLast(null, *)      = null
 2219  
      * StringUtils.substringBeforeLast("", *)        = ""
 2220  
      * StringUtils.substringBeforeLast("abcba", "b") = "abc"
 2221  
      * StringUtils.substringBeforeLast("abc", "c")   = "ab"
 2222  
      * StringUtils.substringBeforeLast("a", "a")     = ""
 2223  
      * StringUtils.substringBeforeLast("a", "z")     = "a"
 2224  
      * StringUtils.substringBeforeLast("a", null)    = "a"
 2225  
      * StringUtils.substringBeforeLast("a", "")      = "a"
 2226  
      * </pre>
 2227  
      *
 2228  
      * @param str  the String to get a substring from, may be null
 2229  
      * @param separator  the String to search for, may be null
 2230  
      * @return the substring before the last occurrence of the separator,
 2231  
      *  {@code null} if null String input
 2232  
      * @since 2.0
 2233  
      */
 2234  
     public static String substringBeforeLast(final String str, final String separator) {
 2235  18
         if (isEmpty(str) || isEmpty(separator)) {
 2236  9
             return str;
 2237  
         }
 2238  9
         final int pos = str.lastIndexOf(separator);
 2239  9
         if (pos == INDEX_NOT_FOUND) {
 2240  2
             return str;
 2241  
         }
 2242  7
         return str.substring(0, pos);
 2243  
     }
 2244  
 
 2245  
     /**
 2246  
      * <p>Gets the substring after the last occurrence of a separator.
 2247  
      * The separator is not returned.</p>
 2248  
      *
 2249  
      * <p>A {@code null} string input will return {@code null}.
 2250  
      * An empty ("") string input will return the empty string.
 2251  
      * An empty or {@code null} separator will return the empty string if
 2252  
      * the input string is not {@code null}.</p>
 2253  
      *
 2254  
      * <p>If nothing is found, the empty string is returned.</p>
 2255  
      *
 2256  
      * <pre>
 2257  
      * StringUtils.substringAfterLast(null, *)      = null
 2258  
      * StringUtils.substringAfterLast("", *)        = ""
 2259  
      * StringUtils.substringAfterLast(*, "")        = ""
 2260  
      * StringUtils.substringAfterLast(*, null)      = ""
 2261  
      * StringUtils.substringAfterLast("abc", "a")   = "bc"
 2262  
      * StringUtils.substringAfterLast("abcba", "b") = "a"
 2263  
      * StringUtils.substringAfterLast("abc", "c")   = ""
 2264  
      * StringUtils.substringAfterLast("a", "a")     = ""
 2265  
      * StringUtils.substringAfterLast("a", "z")     = ""
 2266  
      * </pre>
 2267  
      *
 2268  
      * @param str  the String to get a substring from, may be null
 2269  
      * @param separator  the String to search for, may be null
 2270  
      * @return the substring after the last occurrence of the separator,
 2271  
      *  {@code null} if null String input
 2272  
      * @since 2.0
 2273  
      */
 2274  
     public static String substringAfterLast(final String str, final String separator) {
 2275  15
         if (isEmpty(str)) {
 2276  7
             return str;
 2277  
         }
 2278  8
         if (isEmpty(separator)) {
 2279  2
             return EMPTY;
 2280  
         }
 2281  6
         final int pos = str.lastIndexOf(separator);
 2282  6
         if (pos == INDEX_NOT_FOUND || pos == str.length() - separator.length()) {
 2283  2
             return EMPTY;
 2284  
         }
 2285  4
         return str.substring(pos + separator.length());
 2286  
     }
 2287  
 
 2288  
     // Substring between
 2289  
     //-----------------------------------------------------------------------
 2290  
     /**
 2291  
      * <p>Gets the String that is nested in between two instances of the
 2292  
      * same String.</p>
 2293  
      *
 2294  
      * <p>A {@code null} input String returns {@code null}.
 2295  
      * A {@code null} tag returns {@code null}.</p>
 2296  
      *
 2297  
      * <pre>
 2298  
      * StringUtils.substringBetween(null, *)            = null
 2299  
      * StringUtils.substringBetween("", "")             = ""
 2300  
      * StringUtils.substringBetween("", "tag")          = null
 2301  
      * StringUtils.substringBetween("tagabctag", null)  = null
 2302  
      * StringUtils.substringBetween("tagabctag", "")    = ""
 2303  
      * StringUtils.substringBetween("tagabctag", "tag") = "abc"
 2304  
      * </pre>
 2305  
      *
 2306  
      * @param str  the String containing the substring, may be null
 2307  
      * @param tag  the String before and after the substring, may be null
 2308  
      * @return the substring, {@code null} if no match
 2309  
      * @since 2.0
 2310  
      */
 2311  
     public static String substringBetween(final String str, final String tag) {
 2312  10
         return substringBetween(str, tag, tag);
 2313  
     }
 2314  
 
 2315  
     /**
 2316  
      * <p>Gets the String that is nested in between two Strings.
 2317  
      * Only the first match is returned.</p>
 2318  
      *
 2319  
      * <p>A {@code null} input String returns {@code null}.
 2320  
      * A {@code null} open/close returns {@code null} (no match).
 2321  
      * An empty ("") open and close returns an empty string.</p>
 2322  
      *
 2323  
      * <pre>
 2324  
      * StringUtils.substringBetween("wx[b]yz", "[", "]") = "b"
 2325  
      * StringUtils.substringBetween(null, *, *)          = null
 2326  
      * StringUtils.substringBetween(*, null, *)          = null
 2327  
      * StringUtils.substringBetween(*, *, null)          = null
 2328  
      * StringUtils.substringBetween("", "", "")          = ""
 2329  
      * StringUtils.substringBetween("", "", "]")         = null
 2330  
      * StringUtils.substringBetween("", "[", "]")        = null
 2331  
      * StringUtils.substringBetween("yabcz", "", "")     = ""
 2332  
      * StringUtils.substringBetween("yabcz", "y", "z")   = "abc"
 2333  
      * StringUtils.substringBetween("yabczyabcz", "y", "z")   = "abc"
 2334  
      * </pre>
 2335  
      *
 2336  
      * @param str  the String containing the substring, may be null
 2337  
      * @param open  the String before the substring, may be null
 2338  
      * @param close  the String after the substring, may be null
 2339  
      * @return the substring, {@code null} if no match
 2340  
      * @since 2.0
 2341  
      */
 2342  
     public static String substringBetween(final String str, final String open, final String close) {
 2343  19
         if (str == null || open == null || close == null) {
 2344  5
             return null;
 2345  
         }
 2346  14
         final int start = str.indexOf(open);
 2347  14
         if (start != INDEX_NOT_FOUND) {
 2348  12
             final int end = str.indexOf(close, start + open.length());
 2349  12
             if (end != INDEX_NOT_FOUND) {
 2350  10
                 return str.substring(start + open.length(), end);
 2351  
             }
 2352  
         }
 2353  4
         return null;
 2354  
     }
 2355  
 
 2356  
     /**
 2357  
      * <p>Searches a String for substrings delimited by a start and end tag,
 2358  
      * returning all matching substrings in an array.</p>
 2359  
      *
 2360  
      * <p>A {@code null} input String returns {@code null}.
 2361  
      * A {@code null} open/close returns {@code null} (no match).
 2362  
      * An empty ("") open/close returns {@code null} (no match).</p>
 2363  
      *
 2364  
      * <pre>
 2365  
      * StringUtils.substringsBetween("[a][b][c]", "[", "]") = ["a","b","c"]
 2366  
      * StringUtils.substringsBetween(null, *, *)            = null
 2367  
      * StringUtils.substringsBetween(*, null, *)            = null
 2368  
      * StringUtils.substringsBetween(*, *, null)            = null
 2369  
      * StringUtils.substringsBetween("", "[", "]")          = []
 2370  
      * </pre>
 2371  
      *
 2372  
      * @param str  the String containing the substrings, null returns null, empty returns empty
 2373  
      * @param open  the String identifying the start of the substring, empty returns null
 2374  
      * @param close  the String identifying the end of the substring, empty returns null
 2375  
      * @return a String Array of substrings, or {@code null} if no match
 2376  
      * @since 2.3
 2377  
      */
 2378  
     public static String[] substringsBetween(final String str, final String open, final String close) {
 2379  14
         if (str == null || isEmpty(open) || isEmpty(close)) {
 2380  4
             return null;
 2381  
         }
 2382  10
         final int strLen = str.length();
 2383  10
         if (strLen == 0) {
 2384  1
             return ArrayUtils.EMPTY_STRING_ARRAY;
 2385  
         }
 2386  9
         final int closeLen = close.length();
 2387  9
         final int openLen = open.length();
 2388  9
         final List<String> list = new ArrayList<String>();
 2389  9
         int pos = 0;
 2390  19
         while (pos < strLen - closeLen) {
 2391  17
             int start = str.indexOf(open, pos);
 2392  17
             if (start < 0) {
 2393  6
                 break;
 2394  
             }
 2395  11
             start += openLen;
 2396  11
             final int end = str.indexOf(close, start);
 2397  11
             if (end < 0) {
 2398  1
                 break;
 2399  
             }
 2400  10
             list.add(str.substring(start, end));
 2401  10
             pos = end + closeLen;
 2402  10
         }
 2403  9
         if (list.isEmpty()) {
 2404  3
             return null;
 2405  
         }
 2406  6
         return list.toArray(new String [list.size()]);
 2407  
     }
 2408  
 
 2409  
     // Nested extraction
 2410  
     //-----------------------------------------------------------------------
 2411  
 
 2412  
     // Splitting
 2413  
     //-----------------------------------------------------------------------
 2414  
     /**
 2415  
      * <p>Splits the provided text into an array, using whitespace as the
 2416  
      * separator.
 2417  
      * Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
 2418  
      *
 2419  
      * <p>The separator is not included in the returned String array.
 2420  
      * Adjacent separators are treated as one separator.
 2421  
      * For more control over the split use the StrTokenizer class.</p>
 2422  
      *
 2423  
      * <p>A {@code null} input String returns {@code null}.</p>
 2424  
      *
 2425  
      * <pre>
 2426  
      * StringUtils.split(null)       = null
 2427  
      * StringUtils.split("")         = []
 2428  
      * StringUtils.split("abc def")  = ["abc", "def"]
 2429  
      * StringUtils.split("abc  def") = ["abc", "def"]
 2430  
      * StringUtils.split(" abc ")    = ["abc"]
 2431  
      * </pre>
 2432  
      *
 2433  
      * @param str  the String to parse, may be null
 2434  
      * @return an array of parsed Strings, {@code null} if null String input
 2435  
      */
 2436  
     public static String[] split(final String str) {
 2437  5
         return split(str, null, -1);
 2438  
     }
 2439  
 
 2440  
     /**
 2441  
      * <p>Splits the provided text into an array, separator specified.
 2442  
      * This is an alternative to using StringTokenizer.</p>
 2443  
      *
 2444  
      * <p>The separator is not included in the returned String array.
 2445  
      * Adjacent separators are treated as one separator.
 2446  
      * For more control over the split use the StrTokenizer class.</p>
 2447  
      *
 2448  
      * <p>A {@code null} input String returns {@code null}.</p>
 2449  
      *
 2450  
      * <pre>
 2451  
      * StringUtils.split(null, *)         = null
 2452  
      * StringUtils.split("", *)           = []
 2453  
      * StringUtils.split("a.b.c", '.')    = ["a", "b", "c"]
 2454  
      * StringUtils.split("a..b.c", '.')   = ["a", "b", "c"]
 2455  
      * StringUtils.split("a:b:c", '.')    = ["a:b:c"]
 2456  
      * StringUtils.split("a b c", ' ')    = ["a", "b", "c"]
 2457  
      * </pre>
 2458  
      *
 2459  
      * @param str  the String to parse, may be null
 2460  
      * @param separatorChar  the character used as the delimiter
 2461  
      * @return an array of parsed Strings, {@code null} if null String input
 2462  
      * @since 2.0
 2463  
      */
 2464  
     public static String[] split(final String str, final char separatorChar) {
 2465  9
         return splitWorker(str, separatorChar, false);
 2466  
     }
 2467  
 
 2468  
     /**
 2469  
      * <p>Splits the provided text into an array, separators specified.
 2470  
      * This is an alternative to using StringTokenizer.</p>
 2471  
      *
 2472  
      * <p>The separator is not included in the returned String array.
 2473  
      * Adjacent separators are treated as one separator.
 2474  
      * For more control over the split use the StrTokenizer class.</p>
 2475  
      *
 2476  
      * <p>A {@code null} input String returns {@code null}.
 2477  
      * A {@code null} separatorChars splits on whitespace.</p>
 2478  
      *
 2479  
      * <pre>
 2480  
      * StringUtils.split(null, *)         = null
 2481  
      * StringUtils.split("", *)           = []
 2482  
      * StringUtils.split("abc def", null) = ["abc", "def"]
 2483  
      * StringUtils.split("abc def", " ")  = ["abc", "def"]
 2484  
      * StringUtils.split("abc  def", " ") = ["abc", "def"]
 2485  
      * StringUtils.split("ab:cd:ef", ":") = ["ab", "cd", "ef"]
 2486  
      * </pre>
 2487  
      *
 2488  
      * @param str  the String to parse, may be null
 2489  
      * @param separatorChars  the characters used as the delimiters,
 2490  
      *  {@code null} splits on whitespace
 2491  
      * @return an array of parsed Strings, {@code null} if null String input
 2492  
      */
 2493  
     public static String[] split(final String str, final String separatorChars) {
 2494  3128
         return splitWorker(str, separatorChars, -1, false);
 2495  
     }
 2496  
 
 2497  
     /**
 2498  
      * <p>Splits the provided text into an array with a maximum length,
 2499  
      * separators specified.</p>
 2500  
      *
 2501  
      * <p>The separator is not included in the returned String array.
 2502  
      * Adjacent separators are treated as one separator.</p>
 2503  
      *
 2504  
      * <p>A {@code null} input String returns {@code null}.
 2505  
      * A {@code null} separatorChars splits on whitespace.</p>
 2506  
      *
 2507  
      * <p>If more than {@code max} delimited substrings are found, the last
 2508  
      * returned string includes all characters after the first {@code max - 1}
 2509  
      * returned strings (including separator characters).</p>
 2510  
      *
 2511  
      * <pre>
 2512  
      * StringUtils.split(null, *, *)            = null
 2513  
      * StringUtils.split("", *, *)              = []
 2514  
      * StringUtils.split("ab cd ef", null, 0)   = ["ab", "cd", "ef"]
 2515  
      * StringUtils.split("ab   cd ef", null, 0) = ["ab", "cd", "ef"]
 2516  
      * StringUtils.split("ab:cd:ef", ":", 0)    = ["ab", "cd", "ef"]
 2517  
      * StringUtils.split("ab:cd:ef", ":", 2)    = ["ab", "cd:ef"]
 2518  
      * </pre>
 2519  
      *
 2520  
      * @param str  the String to parse, may be null
 2521  
      * @param separatorChars  the characters used as the delimiters,
 2522  
      *  {@code null} splits on whitespace
 2523  
      * @param max  the maximum number of elements to include in the
 2524  
      *  array. A zero or negative value implies no limit
 2525  
      * @return an array of parsed Strings, {@code null} if null String input
 2526  
      */
 2527  
     public static String[] split(final String str, final String separatorChars, final int max) {
 2528  6261
         return splitWorker(str, separatorChars, max, false);
 2529  
     }
 2530  
 
 2531  
     /**
 2532  
      * <p>Splits the provided text into an array, separator string specified.</p>
 2533  
      *
 2534  
      * <p>The separator(s) will not be included in the returned String array.
 2535  
      * Adjacent separators are treated as one separator.</p>
 2536  
      *
 2537  
      * <p>A {@code null} input String returns {@code null}.
 2538  
      * A {@code null} separator splits on whitespace.</p>
 2539  
      *
 2540  
      * <pre>
 2541  
      * StringUtils.splitByWholeSeparator(null, *)               = null
 2542  
      * StringUtils.splitByWholeSeparator("", *)                 = []
 2543  
      * StringUtils.splitByWholeSeparator("ab de fg", null)      = ["ab", "de", "fg"]
 2544  
      * StringUtils.splitByWholeSeparator("ab   de fg", null)    = ["ab", "de", "fg"]
 2545  
      * StringUtils.splitByWholeSeparator("ab:cd:ef", ":")       = ["ab", "cd", "ef"]
 2546  
      * StringUtils.splitByWholeSeparator("ab-!-cd-!-ef", "-!-") = ["ab", "cd", "ef"]
 2547  
      * </pre>
 2548  
      *
 2549  
      * @param str  the String to parse, may be null
 2550  
      * @param separator  String containing the String to be used as a delimiter,
 2551  
      *  {@code null} splits on whitespace
 2552  
      * @return an array of parsed Strings, {@code null} if null String was input
 2553  
      */
 2554  
     public static String[] splitByWholeSeparator(final String str, final String separator) {
 2555  5
         return splitByWholeSeparatorWorker( str, separator, -1, false ) ;
 2556  
     }
 2557  
 
 2558  
     /**
 2559  
      * <p>Splits the provided text into an array, separator string specified.
 2560  
      * Returns a maximum of {@code max} substrings.</p>
 2561  
      *
 2562  
      * <p>The separator(s) will not be included in the returned String array.
 2563  
      * Adjacent separators are treated as one separator.</p>
 2564  
      *
 2565  
      * <p>A {@code null} input String returns {@code null}.
 2566  
      * A {@code null} separator splits on whitespace.</p>
 2567  
      *
 2568  
      * <pre>
 2569  
      * StringUtils.splitByWholeSeparator(null, *, *)               = null
 2570  
      * StringUtils.splitByWholeSeparator("", *, *)                 = []
 2571  
      * StringUtils.splitByWholeSeparator("ab de fg", null, 0)      = ["ab", "de", "fg"]
 2572  
      * StringUtils.splitByWholeSeparator("ab   de fg", null, 0)    = ["ab", "de", "fg"]
 2573  
      * StringUtils.splitByWholeSeparator("ab:cd:ef", ":", 2)       = ["ab", "cd:ef"]
 2574  
      * StringUtils.splitByWholeSeparator("ab-!-cd-!-ef", "-!-", 5) = ["ab", "cd", "ef"]
 2575  
      * StringUtils.splitByWholeSeparator("ab-!-cd-!-ef", "-!-", 2) = ["ab", "cd-!-ef"]
 2576  
      * </pre>
 2577  
      *
 2578  
      * @param str  the String to parse, may be null
 2579  
      * @param separator  String containing the String to be used as a delimiter,
 2580  
      *  {@code null} splits on whitespace
 2581  
      * @param max  the maximum number of elements to include in the returned
 2582  
      *  array. A zero or negative value implies no limit.
 2583  
      * @return an array of parsed Strings, {@code null} if null String was input
 2584  
      */
 2585  
     public static String[] splitByWholeSeparator( final String str, final String separator, final int max ) {
 2586  4
         return splitByWholeSeparatorWorker(str, separator, max, false);
 2587  
     }
 2588  
 
 2589  
     /**
 2590  
      * <p>Splits the provided text into an array, separator string specified. </p>
 2591  
      *
 2592  
      * <p>The separator is not included in the returned String array.
 2593  
      * Adjacent separators are treated as separators for empty tokens.
 2594  
      * For more control over the split use the StrTokenizer class.</p>
 2595  
      *
 2596  
      * <p>A {@code null} input String returns {@code null}.
 2597  
      * A {@code null} separator splits on whitespace.</p>
 2598  
      *
 2599  
      * <pre>
 2600  
      * StringUtils.splitByWholeSeparatorPreserveAllTokens(null, *)               = null
 2601  
      * StringUtils.splitByWholeSeparatorPreserveAllTokens("", *)                 = []
 2602  
      * StringUtils.splitByWholeSeparatorPreserveAllTokens("ab de fg", null)      = ["ab", "de", "fg"]
 2603  
      * StringUtils.splitByWholeSeparatorPreserveAllTokens("ab   de fg", null)    = ["ab", "", "", "de", "fg"]
 2604  
      * StringUtils.splitByWholeSeparatorPreserveAllTokens("ab:cd:ef", ":")       = ["ab", "cd", "ef"]
 2605  
      * StringUtils.splitByWholeSeparatorPreserveAllTokens("ab-!-cd-!-ef", "-!-") = ["ab", "cd", "ef"]
 2606  
      * </pre>
 2607  
      *
 2608  
      * @param str  the String to parse, may be null
 2609  
      * @param separator  String containing the String to be used as a delimiter,
 2610  
      *  {@code null} splits on whitespace
 2611  
      * @return an array of parsed Strings, {@code null} if null String was input
 2612  
      * @since 2.4
 2613  
      */
 2614  
     public static String[] splitByWholeSeparatorPreserveAllTokens(final String str, final String separator) {
 2615  0
         return splitByWholeSeparatorWorker(str, separator, -1, true);
 2616  
     }
 2617  
 
 2618  
     /**
 2619  
      * <p>Splits the provided text into an array, separator string specified.
 2620  
      * Returns a maximum of {@code max} substrings.</p>
 2621  
      *
 2622  
      * <p>The separator is not included in the returned String array.
 2623  
      * Adjacent separators are treated as separators for empty tokens.
 2624  
      * For more control over the split use the StrTokenizer class.</p>
 2625  
      *
 2626  
      * <p>A {@code null} input String returns {@code null}.
 2627  
      * A {@code null} separator splits on whitespace.</p>
 2628  
      *
 2629  
      * <pre>
 2630  
      * StringUtils.splitByWholeSeparatorPreserveAllTokens(null, *, *)               = null
 2631  
      * StringUtils.splitByWholeSeparatorPreserveAllTokens("", *, *)                 = []
 2632  
      * StringUtils.splitByWholeSeparatorPreserveAllTokens("ab de fg", null, 0)      = ["ab", "de", "fg"]
 2633  
      * StringUtils.splitByWholeSeparatorPreserveAllTokens("ab   de fg", null, 0)    = ["ab", "", "", "de", "fg"]
 2634  
      * StringUtils.splitByWholeSeparatorPreserveAllTokens("ab:cd:ef", ":", 2)       = ["ab", "cd:ef"]
 2635  
      * StringUtils.splitByWholeSeparatorPreserveAllTokens("ab-!-cd-!-ef", "-!-", 5) = ["ab", "cd", "ef"]
 2636  
      * StringUtils.splitByWholeSeparatorPreserveAllTokens("ab-!-cd-!-ef", "-!-", 2) = ["ab", "cd-!-ef"]
 2637  
      * </pre>
 2638  
      *
 2639  
      * @param str  the String to parse, may be null
 2640  
      * @param separator  String containing the String to be used as a delimiter,
 2641  
      *  {@code null} splits on whitespace
 2642  
      * @param max  the maximum number of elements to include in the returned
 2643  
      *  array. A zero or negative value implies no limit.
 2644  
      * @return an array of parsed Strings, {@code null} if null String was input
 2645  
      * @since 2.4
 2646  
      */
 2647  
     public static String[] splitByWholeSeparatorPreserveAllTokens(final String str, final String separator, final int max) {
 2648  6
         return splitByWholeSeparatorWorker(str, separator, max, true);
 2649  
     }
 2650  
 
 2651  
     /**
 2652  
      * Performs the logic for the {@code splitByWholeSeparatorPreserveAllTokens} methods.
 2653  
      *
 2654  
      * @param str  the String to parse, may be {@code null}
 2655  
      * @param separator  String containing the String to be used as a delimiter,
 2656  
      *  {@code null} splits on whitespace
 2657  
      * @param max  the maximum number of elements to include in the returned
 2658  
      *  array. A zero or negative value implies no limit.
 2659  
      * @param preserveAllTokens if {@code true}, adjacent separators are
 2660  
      * treated as empty token separators; if {@code false}, adjacent
 2661  
      * separators are treated as one separator.
 2662  
      * @return an array of parsed Strings, {@code null} if null String input
 2663  
      * @since 2.4
 2664  
      */
 2665  
     private static String[] splitByWholeSeparatorWorker(
 2666  
             final String str, final String separator, final int max, final boolean preserveAllTokens) {
 2667  15
         if (str == null) {
 2668  3
             return null;
 2669  
         }
 2670  
 
 2671  12
         final int len = str.length();
 2672  
 
 2673  12
         if (len == 0) {
 2674  3
             return ArrayUtils.EMPTY_STRING_ARRAY;
 2675  
         }
 2676  
 
 2677  9
         if (separator == null || EMPTY.equals(separator)) {
 2678  
             // Split on whitespace.
 2679  3
             return splitWorker(str, null, max, preserveAllTokens);
 2680  
         }
 2681  
 
 2682  6
         final int separatorLength = separator.length();
 2683  
 
 2684  6
         final ArrayList<String> substrings = new ArrayList<String>();
 2685  6
         int numberOfSubstrings = 0;
 2686  6
         int beg = 0;
 2687  6
         int end = 0;
 2688  33
         while (end < len) {
 2689  27
             end = str.indexOf(separator, beg);
 2690  
 
 2691  27
             if (end > -1) {
 2692  23
                 if (end > beg) {
 2693  13
                     numberOfSubstrings += 1;
 2694  
 
 2695  13
                     if (numberOfSubstrings == max) {
 2696  1
                         end = len;
 2697  1
                         substrings.add(str.substring(beg));
 2698  
                     } else {
 2699  
                         // The following is OK, because String.substring( beg, end ) excludes
 2700  
                         // the character at the position 'end'.
 2701  12
                         substrings.add(str.substring(beg, end));
 2702  
 
 2703  
                         // Set the starting point for the next search.
 2704  
                         // The following is equivalent to beg = end + (separatorLength - 1) + 1,
 2705  
                         // which is the right calculation:
 2706  12
                         beg = end + separatorLength;
 2707  
                     }
 2708  
                 } else {
 2709  
                     // We found a consecutive occurrence of the separator, so skip it.
 2710  10
                     if (preserveAllTokens) {
 2711  9
                         numberOfSubstrings += 1;
 2712  9
                         if (numberOfSubstrings == max) {
 2713  1
                             end = len;
 2714  1
                             substrings.add(str.substring(beg));
 2715  
                         } else {
 2716  8
                             substrings.add(EMPTY);
 2717  
                         }
 2718  
                     }
 2719  10
                     beg = end + separatorLength;
 2720  
                 }
 2721  
             } else {
 2722  
                 // String.substring( beg ) goes from 'beg' to the end of the String.
 2723  4
                 substrings.add(str.substring(beg));
 2724  4
                 end = len;
 2725  
             }
 2726  
         }
 2727  
 
 2728  6
         return substrings.toArray(new String[substrings.size()]);
 2729  
     }
 2730  
 
 2731  
     // -----------------------------------------------------------------------
 2732  
     /**
 2733  
      * <p>Splits the provided text into an array, using whitespace as the
 2734  
      * separator, preserving all tokens, including empty tokens created by
 2735  
      * adjacent separators. This is an alternative to using StringTokenizer.
 2736  
      * Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
 2737  
      *
 2738  
      * <p>The separator is not included in the returned String array.
 2739  
      * Adjacent separators are treated as separators for empty tokens.
 2740  
      * For more control over the split use the StrTokenizer class.</p>
 2741  
      *
 2742  
      * <p>A {@code null} input String returns {@code null}.</p>
 2743  
      *
 2744  
      * <pre>
 2745  
      * StringUtils.splitPreserveAllTokens(null)       = null
 2746  
      * StringUtils.splitPreserveAllTokens("")         = []
 2747  
      * StringUtils.splitPreserveAllTokens("abc def")  = ["abc", "def"]
 2748  
      * StringUtils.splitPreserveAllTokens("abc  def") = ["abc", "", "def"]
 2749  
      * StringUtils.splitPreserveAllTokens(" abc ")    = ["", "abc", ""]
 2750  
      * </pre>
 2751  
      *
 2752  
      * @param str  the String to parse, may be {@code null}
 2753  
      * @return an array of parsed Strings, {@code null} if null String input
 2754  
      * @since 2.1
 2755  
      */
 2756  
     public static String[] splitPreserveAllTokens(final String str) {
 2757  11
         return splitWorker(str, null, -1, true);
 2758  
     }
 2759  
 
 2760  
     /**
 2761  
      * <p>Splits the provided text into an array, separator specified,
 2762  
      * preserving all tokens, including empty tokens created by adjacent
 2763  
      * separators. This is an alternative to using StringTokenizer.</p>
 2764  
      *
 2765  
      * <p>The separator is not included in the returned String array.
 2766  
      * Adjacent separators are treated as separators for empty tokens.
 2767  
      * For more control over the split use the StrTokenizer class.</p>
 2768  
      *
 2769  
      * <p>A {@code null} input String returns {@code null}.</p>
 2770  
      *
 2771  
      * <pre>
 2772  
      * StringUtils.splitPreserveAllTokens(null, *)         = null
 2773  
      * StringUtils.splitPreserveAllTokens("", *)           = []
 2774  
      * StringUtils.splitPreserveAllTokens("a.b.c", '.')    = ["a", "b", "c"]
 2775  
      * StringUtils.splitPreserveAllTokens("a..b.c", '.')   = ["a", "", "b", "c"]
 2776  
      * StringUtils.splitPreserveAllTokens("a:b:c", '.')    = ["a:b:c"]
 2777  
      * StringUtils.splitPreserveAllTokens("a\tb\nc", null) = ["a", "b", "c"]
 2778  
      * StringUtils.splitPreserveAllTokens("a b c", ' ')    = ["a", "b", "c"]
 2779  
      * StringUtils.splitPreserveAllTokens("a b c ", ' ')   = ["a", "b", "c", ""]
 2780  
      * StringUtils.splitPreserveAllTokens("a b c  ", ' ')   = ["a", "b", "c", "", ""]
 2781  
      * StringUtils.splitPreserveAllTokens(" a b c", ' ')   = ["", a", "b", "c"]
 2782  
      * StringUtils.splitPreserveAllTokens("  a b c", ' ')  = ["", "", a", "b", "c"]
 2783  
      * StringUtils.splitPreserveAllTokens(" a b c ", ' ')  = ["", a", "b", "c", ""]
 2784  
      * </pre>
 2785  
      *
 2786  
      * @param str  the String to parse, may be {@code null}
 2787  
      * @param separatorChar  the character used as the delimiter,
 2788  
      *  {@code null} splits on whitespace
 2789  
      * @return an array of parsed Strings, {@code null} if null String input
 2790  
      * @since 2.1
 2791  
      */
 2792  
     public static String[] splitPreserveAllTokens(final String str, final char separatorChar) {
 2793  15
         return splitWorker(str, separatorChar, true);
 2794  
     }
 2795  
 
 2796  
     /**
 2797  
      * Performs the logic for the {@code split} and
 2798  
      * {@code splitPreserveAllTokens} methods that do not return a
 2799  
      * maximum array length.
 2800  
      *
 2801  
      * @param str  the String to parse, may be {@code null}
 2802  
      * @param separatorChar the separate character
 2803  
      * @param preserveAllTokens if {@code true}, adjacent separators are
 2804  
      * treated as empty token separators; if {@code false}, adjacent
 2805  
      * separators are treated as one separator.
 2806  
      * @return an array of parsed Strings, {@code null} if null String input
 2807  
      */
 2808  
     private static String[] splitWorker(final String str, final char separatorChar, final boolean preserveAllTokens) {
 2809  
         // Performance tuned for 2.0 (JDK1.4)
 2810  
 
 2811  24
         if (str == null) {
 2812  2
             return null;
 2813  
         }
 2814  22
         final int len = str.length();
 2815  22
         if (len == 0) {
 2816  4
             return ArrayUtils.EMPTY_STRING_ARRAY;
 2817  
         }
 2818  18
         final List<String> list = new ArrayList<String>();
 2819  18
         int i = 0, start = 0;
 2820  18
         boolean match = false;
 2821  18
         boolean lastMatch = false;
 2822  114
         while (i < len) {
 2823  96
             if (str.charAt(i) == separatorChar) {
 2824  47
                 if (match || preserveAllTokens) {
 2825  45
                     list.add(str.substring(start, i));
 2826  45
                     match = false;
 2827  45
                     lastMatch = true;
 2828  
                 }
 2829  47
                 start = ++i;
 2830  47
                 continue;
 2831  
             }
 2832  49
             lastMatch = false;
 2833  49
             match = true;
 2834  49
             i++;
 2835  
         }
 2836  18
         if (match || preserveAllTokens && lastMatch) {
 2837  17
             list.add(str.substring(start, i));
 2838  
         }
 2839  18
         return list.toArray(new String[list.size()]);
 2840  
     }
 2841  
 
 2842  
     /**
 2843  
      * <p>Splits the provided text into an array, separators specified,
 2844  
      * preserving all tokens, including empty tokens created by adjacent
 2845  
      * separators. This is an alternative to using StringTokenizer.</p>
 2846  
      *
 2847  
      * <p>The separator is not included in the returned String array.
 2848  
      * Adjacent separators are treated as separators for empty tokens.
 2849  
      * For more control over the split use the StrTokenizer class.</p>
 2850  
      *
 2851  
      * <p>A {@code null} input String returns {@code null}.
 2852  
      * A {@code null} separatorChars splits on whitespace.</p>
 2853  
      *
 2854  
      * <pre>
 2855  
      * StringUtils.splitPreserveAllTokens(null, *)           = null
 2856  
      * StringUtils.splitPreserveAllTokens("", *)             = []
 2857  
      * StringUtils.splitPreserveAllTokens("abc def", null)   = ["abc", "def"]
 2858  
      * StringUtils.splitPreserveAllTokens("abc def", " ")    = ["abc", "def"]
 2859  
      * StringUtils.splitPreserveAllTokens("abc  def", " ")   = ["abc", "", def"]
 2860  
      * StringUtils.splitPreserveAllTokens("ab:cd:ef", ":")   = ["ab", "cd", "ef"]
 2861  
      * StringUtils.splitPreserveAllTokens("ab:cd:ef:", ":")  = ["ab", "cd", "ef", ""]
 2862  
      * StringUtils.splitPreserveAllTokens("ab:cd:ef::", ":") = ["ab", "cd", "ef", "", ""]
 2863  
      * StringUtils.splitPreserveAllTokens("ab::cd:ef", ":")  = ["ab", "", cd", "ef"]
 2864  
      * StringUtils.splitPreserveAllTokens(":cd:ef", ":")     = ["", cd", "ef"]
 2865  
      * StringUtils.splitPreserveAllTokens("::cd:ef", ":")    = ["", "", cd", "ef"]
 2866  
      * StringUtils.splitPreserveAllTokens(":cd:ef:", ":")    = ["", cd", "ef", ""]
 2867  
      * </pre>
 2868  
      *
 2869  
      * @param str  the String to parse, may be {@code null}
 2870  
      * @param separatorChars  the characters used as the delimiters,
 2871  
      *  {@code null} splits on whitespace
 2872  
      * @return an array of parsed Strings, {@code null} if null String input
 2873  
      * @since 2.1
 2874  
      */
 2875  
     public static String[] splitPreserveAllTokens(final String str, final String separatorChars) {
 2876  3128
         return splitWorker(str, separatorChars, -1, true);
 2877  
     }
 2878  
 
 2879  
     /**
 2880  
      * <p>Splits the provided text into an array with a maximum length,
 2881  
      * separators specified, preserving all tokens, including empty tokens
 2882  
      * created by adjacent separators.</p>
 2883  
      *
 2884  
      * <p>The separator is not included in the returned String array.
 2885  
      * Adjacent separators are treated as separators for empty tokens.
 2886  
      * Adjacent separators are treated as one separator.</p>
 2887  
      *
 2888  
      * <p>A {@code null} input String returns {@code null}.
 2889  
      * A {@code null} separatorChars splits on whitespace.</p>
 2890  
      *
 2891  
      * <p>If more than {@code max} delimited substrings are found, the last
 2892  
      * returned string includes all characters after the first {@code max - 1}
 2893  
      * returned strings (including separator characters).</p>
 2894  
      *
 2895  
      * <pre>
 2896  
      * StringUtils.splitPreserveAllTokens(null, *, *)            = null
 2897  
      * StringUtils.splitPreserveAllTokens("", *, *)              = []
 2898  
      * StringUtils.splitPreserveAllTokens("ab de fg", null, 0)   = ["ab", "cd", "ef"]
 2899  
      * StringUtils.splitPreserveAllTokens("ab   de fg", null, 0) = ["ab", "cd", "ef"]
 2900  
      * StringUtils.splitPreserveAllTokens("ab:cd:ef", ":", 0)    = ["ab", "cd", "ef"]
 2901  
      * StringUtils.splitPreserveAllTokens("ab:cd:ef", ":", 2)    = ["ab", "cd:ef"]
 2902  
      * StringUtils.splitPreserveAllTokens("ab   de fg", null, 2) = ["ab", "  de fg"]
 2903  
      * StringUtils.splitPreserveAllTokens("ab   de fg", null, 3) = ["ab", "", " de fg"]
 2904  
      * StringUtils.splitPreserveAllTokens("ab   de fg", null, 4) = ["ab", "", "", "de fg"]
 2905  
      * </pre>
 2906  
      *
 2907  
      * @param str  the String to parse, may be {@code null}
 2908  
      * @param separatorChars  the characters used as the delimiters,
 2909  
      *  {@code null} splits on whitespace
 2910  
      * @param max  the maximum number of elements to include in the
 2911  
      *  array. A zero or negative value implies no limit
 2912  
      * @return an array of parsed Strings, {@code null} if null String input
 2913  
      * @since 2.1
 2914  
      */
 2915  
     public static String[] splitPreserveAllTokens(final String str, final String separatorChars, final int max) {
 2916  6265
         return splitWorker(str, separatorChars, max, true);
 2917  
     }
 2918  
 
 2919  
     /**
 2920  
      * Performs the logic for the {@code split} and
 2921  
      * {@code splitPreserveAllTokens} methods that return a maximum array
 2922  
      * length.
 2923  
      *
 2924  
      * @param str  the String to parse, may be {@code null}
 2925  
      * @param separatorChars the separate character
 2926  
      * @param max  the maximum number of elements to include in the
 2927  
      *  array. A zero or negative value implies no limit.
 2928  
      * @param preserveAllTokens if {@code true}, adjacent separators are
 2929  
      * treated as empty token separators; if {@code false}, adjacent
 2930  
      * separators are treated as one separator.
 2931  
      * @return an array of parsed Strings, {@code null} if null String input
 2932  
      */
 2933  
     private static String[] splitWorker(final String str, final String separatorChars, final int max, final boolean preserveAllTokens) {
 2934  
         // Performance tuned for 2.0 (JDK1.4)
 2935  
         // Direct code is quicker than StringTokenizer.
 2936  
         // Also, StringTokenizer uses isSpace() not isWhitespace()
 2937  
 
 2938  18796
         if (str == null) {
 2939  6
             return null;
 2940  
         }
 2941  18790
         final int len = str.length();
 2942  18790
         if (len == 0) {
 2943  6
             return ArrayUtils.EMPTY_STRING_ARRAY;
 2944  
         }
 2945  18784
         final List<String> list = new ArrayList<String>();
 2946  18784
         int sizePlus1 = 1;
 2947  18784
         int i = 0, start = 0;
 2948  18784
         boolean match = false;
 2949  18784
         boolean lastMatch = false;
 2950  18784
         if (separatorChars == null) {
 2951  
             // Null separator means use whitespace
 2952  56417
             while (i < len) {
 2953  47037
                 if (Character.isWhitespace(str.charAt(i))) {
 2954  21944
                     if (match || preserveAllTokens) {
 2955  18791
                         lastMatch = true;
 2956  18791
                         if (sizePlus1++ == max) {
 2957  3126
                             i = len;
 2958  3126
                             lastMatch = false;
 2959  
                         }
 2960  18791
                         list.add(str.substring(start, i));
 2961  18791
                         match = false;
 2962  
                     }
 2963  21944
                     start = ++i;
 2964  21944
                     continue;
 2965  
                 }
 2966  25093
                 lastMatch = false;
 2967  25093
                 match = true;
 2968  25093
                 i++;
 2969  
             }
 2970  9404
         } else if (separatorChars.length() == 1) {
 2971  
             // Optimise 1 character case
 2972  9392
             final char sep = separatorChars.charAt(0);
 2973  56364
             while (i < len) {
 2974  46972
                 if (str.charAt(i) == sep) {
 2975  21922
                     if (match || preserveAllTokens) {
 2976  18794
                         lastMatch = true;
 2977  18794
                         if (sizePlus1++ == max) {
 2978  3136
                             i = len;
 2979  3136
                             lastMatch = false;
 2980  
                         }
 2981  18794
                         list.add(str.substring(start, i));
 2982  18794
                         match = false;
 2983  
                     }
 2984  21922
                     start = ++i;
 2985  21922
                     continue;
 2986  
                 }
 2987  25050
                 lastMatch = false;
 2988  25050
                 match = true;
 2989  25050
                 i++;
 2990  
             }
 2991  9392
         } else {
 2992  
             // standard case
 2993  72
             while (i < len) {
 2994  60
                 if (separatorChars.indexOf(str.charAt(i)) >= 0) {
 2995  28
                     if (match || preserveAllTokens) {
 2996  24
                         lastMatch = true;
 2997  24
                         if (sizePlus1++ == max) {
 2998  4
                             i = len;
 2999  4
                             lastMatch = false;
 3000  
                         }
 3001  24
                         list.add(str.substring(start, i));
 3002  24
                         match = false;
 3003  
                     }
 3004  28
                     start = ++i;
 3005  28
                     continue;
 3006  
                 }
 3007  32
                 lastMatch = false;
 3008  32
                 match = true;
 3009  32
                 i++;
 3010  
             }
 3011  
         }
 3012  18784
         if (match || preserveAllTokens && lastMatch) {
 3013  10954
             list.add(str.substring(start, i));
 3014  
         }
 3015  18784
         return list.toArray(new String[list.size()]);
 3016  
     }
 3017  
 
 3018  
     /**
 3019  
      * <p>Splits a String by Character type as returned by
 3020  
      * {@code java.lang.Character.getType(char)}. Groups of contiguous
 3021  
      * characters of the same type are returned as complete tokens.
 3022  
      * <pre>
 3023  
      * StringUtils.splitByCharacterType(null)         = null
 3024  
      * StringUtils.splitByCharacterType("")           = []
 3025  
      * StringUtils.splitByCharacterType("ab de fg")   = ["ab", " ", "de", " ", "fg"]
 3026  
      * StringUtils.splitByCharacterType("ab   de fg") = ["ab", "   ", "de", " ", "fg"]
 3027  
      * StringUtils.splitByCharacterType("ab:cd:ef")   = ["ab", ":", "cd", ":", "ef"]
 3028  
      * StringUtils.splitByCharacterType("number5")    = ["number", "5"]
 3029  
      * StringUtils.splitByCharacterType("fooBar")     = ["foo", "B", "ar"]
 3030  
      * StringUtils.splitByCharacterType("foo200Bar")  = ["foo", "200", "B", "ar"]
 3031  
      * StringUtils.splitByCharacterType("ASFRules")   = ["ASFR", "ules"]
 3032  
      * </pre>
 3033  
      * @param str the String to split, may be {@code null}
 3034  
      * @return an array of parsed Strings, {@code null} if null String input
 3035  
      * @since 2.4
 3036  
      */
 3037  
     public static String[] splitByCharacterType(final String str) {
 3038  9
         return splitByCharacterType(str, false);
 3039  
     }
 3040  
 
 3041  
     /**
 3042  
      * <p>Splits a String by Character type as returned by
 3043  
      * {@code java.lang.Character.getType(char)}. Groups of contiguous
 3044  
      * characters of the same type are returned as complete tokens, with the
 3045  
      * following exception: the character of type
 3046  
      * {@code Character.UPPERCASE_LETTER}, if any, immediately
 3047  
      * preceding a token of type {@code Character.LOWERCASE_LETTER}
 3048  
      * will belong to the following token rather than to the preceding, if any,
 3049  
      * {@code Character.UPPERCASE_LETTER} token.
 3050  
      * <pre>
 3051  
      * StringUtils.splitByCharacterTypeCamelCase(null)         = null
 3052  
      * StringUtils.splitByCharacterTypeCamelCase("")           = []
 3053  
      * StringUtils.splitByCharacterTypeCamelCase("ab de fg")   = ["ab", " ", "de", " ", "fg"]
 3054  
      * StringUtils.splitByCharacterTypeCamelCase("ab   de fg") = ["ab", "   ", "de", " ", "fg"]
 3055  
      * StringUtils.splitByCharacterTypeCamelCase("ab:cd:ef")   = ["ab", ":", "cd", ":", "ef"]
 3056  
      * StringUtils.splitByCharacterTypeCamelCase("number5")    = ["number", "5"]
 3057  
      * StringUtils.splitByCharacterTypeCamelCase("fooBar")     = ["foo", "Bar"]
 3058  
      * StringUtils.splitByCharacterTypeCamelCase("foo200Bar")  = ["foo", "200", "Bar"]
 3059  
      * StringUtils.splitByCharacterTypeCamelCase("ASFRules")   = ["ASF", "Rules"]
 3060  
      * </pre>
 3061  
      * @param str the String to split, may be {@code null}
 3062  
      * @return an array of parsed Strings, {@code null} if null String input
 3063  
      * @since 2.4
 3064  
      */
 3065  
     public static String[] splitByCharacterTypeCamelCase(final String str) {
 3066  9
         return splitByCharacterType(str, true);
 3067  
     }
 3068  
 
 3069  
     /**
 3070  
      * <p>Splits a String by Character type as returned by
 3071  
      * {@code java.lang.Character.getType(char)}. Groups of contiguous
 3072  
      * characters of the same type are returned as complete tokens, with the
 3073  
      * following exception: if {@code camelCase} is {@code true},
 3074  
      * the character of type {@code Character.UPPERCASE_LETTER}, if any,
 3075  
      * immediately preceding a token of type {@code Character.LOWERCASE_LETTER}
 3076  
      * will belong to the following token rather than to the preceding, if any,
 3077  
      * {@code Character.UPPERCASE_LETTER} token.
 3078  
      * @param str the String to split, may be {@code null}
 3079  
      * @param camelCase whether to use so-called "camel-case" for letter types
 3080  
      * @return an array of parsed Strings, {@code null} if null String input
 3081  
      * @since 2.4
 3082  
      */
 3083  
     private static String[] splitByCharacterType(final String str, final boolean camelCase) {
 3084  18
         if (str == null) {
 3085  2
             return null;
 3086  
         }
 3087  16
         if (str.length() == 0) {
 3088  2
             return ArrayUtils.EMPTY_STRING_ARRAY;
 3089  
         }
 3090  14
         final char[] c = str.toCharArray();
 3091  14
         final List<String> list = new ArrayList<String>();
 3092  14
         int tokenStart = 0;
 3093  14
         int currentType = Character.getType(c[tokenStart]);
 3094  112
         for (int pos = tokenStart + 1; pos < c.length; pos++) {
 3095  98
             final int type = Character.getType(c[pos]);
 3096  98
             if (type == currentType) {
 3097  60
                 continue;
 3098  
             }
 3099  38
             if (camelCase && type == Character.LOWERCASE_LETTER && currentType == Character.UPPERCASE_LETTER) {
 3100  3
                 final int newTokenStart = pos - 1;
 3101  3
                 if (newTokenStart != tokenStart) {
 3102  1
                     list.add(new String(c, tokenStart, newTokenStart - tokenStart));
 3103  1
                     tokenStart = newTokenStart;
 3104  
                 }
 3105  3
             } else {
 3106  35
                 list.add(new String(c, tokenStart, pos - tokenStart));
 3107  35
                 tokenStart = pos;
 3108  
             }
 3109  38
             currentType = type;
 3110  
         }
 3111  14
         list.add(new String(c, tokenStart, c.length - tokenStart));
 3112  14
         return list.toArray(new String[list.size()]);
 3113  
     }
 3114  
 
 3115  
     // Joining
 3116  
     //-----------------------------------------------------------------------
 3117  
     /**
 3118  
      * <p>Joins the elements of the provided array into a single String
 3119  
      * containing the provided list of elements.</p>
 3120  
      *
 3121  
      * <p>No separator is added to the joined String.
 3122  
      * Null objects or empty strings within the array are represented by
 3123  
      * empty strings.</p>
 3124  
      *
 3125  
      * <pre>
 3126  
      * StringUtils.join(null)            = null
 3127  
      * StringUtils.join([])              = ""
 3128  
      * StringUtils.join([null])          = ""
 3129  
      * StringUtils.join(["a", "b", "c"]) = "abc"
 3130  
      * StringUtils.join([null, "", "a"]) = "a"
 3131  
      * </pre>
 3132  
      *
 3133  
      * @param <T> the specific type of values to join together
 3134  
      * @param elements  the values to join together, may be null
 3135  
      * @return the joined String, {@code null} if null array input
 3136  
      * @since 2.0
 3137  
      * @since 3.0 Changed signature to use varargs
 3138  
      */
 3139  
     public static <T> String join(final T... elements) {
 3140  24
         return join(elements, null);
 3141  
     }
 3142  
 
 3143  
     /**
 3144  
      * <p>Joins the elements of the provided array into a single String
 3145  
      * containing the provided list of elements.</p>
 3146  
      *
 3147  
      * <p>No delimiter is added before or after the list.
 3148  
      * Null objects or empty strings within the array are represented by
 3149  
      * empty strings.</p>
 3150  
      *
 3151  
      * <pre>
 3152  
      * StringUtils.join(null, *)               = null
 3153  
      * StringUtils.join([], *)                 = ""
 3154  
      * StringUtils.join([null], *)             = ""
 3155  
      * StringUtils.join(["a", "b", "c"], ';')  = "a;b;c"
 3156  
      * StringUtils.join(["a", "b", "c"], null) = "abc"
 3157  
      * StringUtils.join([null, "", "a"], ';')  = ";;a"
 3158  
      * </pre>
 3159  
      *
 3160  
      * @param array  the array of values to join together, may be null
 3161  
      * @param separator  the separator character to use
 3162  
      * @return the joined String, {@code null} if null array input
 3163  
      * @since 2.0
 3164  
      */
 3165  
     public static String join(final Object[] array, final char separator) {
 3166  9
         if (array == null) {
 3167  1
             return null;
 3168  
         }
 3169  8
         return join(array, separator, 0, array.length);
 3170  
     }
 3171  
 
 3172  
     /**
 3173  
      * <p>
 3174  
      * Joins the elements of the provided array into a single String containing the provided list of elements.
 3175  
      * </p>
 3176  
      * 
 3177  
      * <p>
 3178  
      * No delimiter is added before or after the list. Null objects or empty strings within the array are represented
 3179  
      * by empty strings.
 3180  
      * </p>
 3181  
      * 
 3182  
      * <pre>
 3183  
      * StringUtils.join(null, *)               = null
 3184  
      * StringUtils.join([], *)                 = ""
 3185  
      * StringUtils.join([null], *)             = ""
 3186  
      * StringUtils.join([1, 2, 3], ';')  = "1;2;3"
 3187  
      * StringUtils.join([1, 2, 3], null) = "123"
 3188  
      * </pre>
 3189  
      * 
 3190  
      * @param array
 3191  
      *            the array of values to join together, may be null
 3192  
      * @param separator
 3193  
      *            the separator character to use
 3194  
      * @return the joined String, {@code null} if null array input
 3195  
      * @since 3.2
 3196  
      */
 3197  
     public static String join(final long[] array, final char separator) {
 3198  2
         if (array == null) {
 3199  1
             return null;
 3200  
         }
 3201  1
         return join(array, separator, 0, array.length);
 3202  
     }
 3203  
 
 3204  
     /**
 3205  
      * <p>
 3206  
      * Joins the elements of the provided array into a single String containing the provided list of elements.
 3207  
      * </p>
 3208  
      * 
 3209  
      * <p>
 3210  
      * No delimiter is added before or after the list. Null objects or empty strings within the array are represented
 3211  
      * by empty strings.
 3212  
      * </p>
 3213  
      * 
 3214  
      * <pre>
 3215  
      * StringUtils.join(null, *)               = null
 3216  
      * StringUtils.join([], *)                 = ""
 3217  
      * StringUtils.join([null], *)             = ""
 3218  
      * StringUtils.join([1, 2, 3], ';')  = "1;2;3"
 3219  
      * StringUtils.join([1, 2, 3], null) = "123"
 3220  
      * </pre>
 3221  
      * 
 3222  
      * @param array
 3223  
      *            the array of values to join together, may be null
 3224  
      * @param separator
 3225  
      *            the separator character to use
 3226  
      * @return the joined String, {@code null} if null array input
 3227  
      * @since 3.2
 3228  
      */
 3229  
     public static String join(final int[] array, final char separator) {
 3230  2
         if (array == null) {
 3231  1
             return null;
 3232  
         }
 3233  1
         return join(array, separator, 0, array.length);
 3234  
     }
 3235  
 
 3236  
     /**
 3237  
      * <p>
 3238  
      * Joins the elements of the provided array into a single String containing the provided list of elements.
 3239  
      * </p>
 3240  
      * 
 3241  
      * <p>
 3242  
      * No delimiter is added before or after the list. Null objects or empty strings within the array are represented
 3243  
      * by empty strings.
 3244  
      * </p>
 3245  
      * 
 3246  
      * <pre>
 3247  
      * StringUtils.join(null, *)               = null
 3248  
      * StringUtils.join([], *)                 = ""
 3249  
      * StringUtils.join([null], *)             = ""
 3250  
      * StringUtils.join([1, 2, 3], ';')  = "1;2;3"
 3251  
      * StringUtils.join([1, 2, 3], null) = "123"
 3252  
      * </pre>
 3253  
      * 
 3254  
      * @param array
 3255  
      *            the array of values to join together, may be null
 3256  
      * @param separator
 3257  
      *            the separator character to use
 3258  
      * @return the joined String, {@code null} if null array input
 3259  
      * @since 3.2
 3260  
      */
 3261  
     public static String join(final short[] array, final char separator) {
 3262  2
         if (array == null) {
 3263  1
             return null;
 3264  
         }
 3265  1
         return join(array, separator, 0, array.length);
 3266  
     }
 3267  
 
 3268  
     /**
 3269  
      * <p>
 3270  
      * Joins the elements of the provided array into a single String containing the provided list of elements.
 3271  
      * </p>
 3272  
      * 
 3273  
      * <p>
 3274  
      * No delimiter is added before or after the list. Null objects or empty strings within the array are represented
 3275  
      * by empty strings.
 3276  
      * </p>
 3277  
      * 
 3278  
      * <pre>
 3279  
      * StringUtils.join(null, *)               = null
 3280  
      * StringUtils.join([], *)                 = ""
 3281  
      * StringUtils.join([null], *)             = ""
 3282  
      * StringUtils.join([1, 2, 3], ';')  = "1;2;3"
 3283  
      * StringUtils.join([1, 2, 3], null) = "123"
 3284  
      * </pre>
 3285  
      * 
 3286  
      * @param array
 3287  
      *            the array of values to join together, may be null
 3288  
      * @param separator
 3289  
      *            the separator character to use
 3290  
      * @return the joined String, {@code null} if null array input
 3291  
      * @since 3.2
 3292  
      */
 3293  
     public static String join(final byte[] array, final char separator) {
 3294  2
         if (array == null) {
 3295  1
             return null;
 3296  
         }
 3297  1
         return join(array, separator, 0, array.length);
 3298  
     }
 3299  
 
 3300  
     /**
 3301  
      * <p>
 3302  
      * Joins the elements of the provided array into a single String containing the provided list of elements.
 3303  
      * </p>
 3304  
      * 
 3305  
      * <p>
 3306  
      * No delimiter is added before or after the list. Null objects or empty strings within the array are represented
 3307  
      * by empty strings.
 3308  
      * </p>
 3309  
      * 
 3310  
      * <pre>
 3311  
      * StringUtils.join(null, *)               = null
 3312  
      * StringUtils.join([], *)                 = ""
 3313  
      * StringUtils.join([null], *)             = ""
 3314  
      * StringUtils.join([1, 2, 3], ';')  = "1;2;3"
 3315  
      * StringUtils.join([1, 2, 3], null) = "123"
 3316  
      * </pre>
 3317  
      * 
 3318  
      * @param array
 3319  
      *            the array of values to join together, may be null
 3320  
      * @param separator
 3321  
      *            the separator character to use
 3322  
      * @return the joined String, {@code null} if null array input
 3323  
      * @since 3.2
 3324  
      */
 3325  
     public static String join(final char[] array, final char separator) {
 3326  2
         if (array == null) {
 3327  1
             return null;
 3328  
         }
 3329  1
         return join(array, separator, 0, array.length);
 3330  
     }
 3331  
 
 3332  
     /**
 3333  
      * <p>
 3334  
      * Joins the elements of the provided array into a single String containing the provided list of elements.
 3335  
      * </p>
 3336  
      * 
 3337  
      * <p>
 3338  
      * No delimiter is added before or after the list. Null objects or empty strings within the array are represented
 3339  
      * by empty strings.
 3340  
      * </p>
 3341  
      * 
 3342  
      * <pre>
 3343  
      * StringUtils.join(null, *)               = null
 3344  
      * StringUtils.join([], *)                 = ""
 3345  
      * StringUtils.join([null], *)             = ""
 3346  
      * StringUtils.join([1, 2, 3], ';')  = "1;2;3"
 3347  
      * StringUtils.join([1, 2, 3], null) = "123"
 3348  
      * </pre>
 3349  
      * 
 3350  
      * @param array
 3351  
      *            the array of values to join together, may be null
 3352  
      * @param separator
 3353  
      *            the separator character to use
 3354  
      * @return the joined String, {@code null} if null array input
 3355  
      * @since 3.2
 3356  
      */
 3357  
     public static String join(final float[] array, final char separator) {
 3358  2
         if (array == null) {
 3359  1
             return null;
 3360  
         }
 3361  1
         return join(array, separator, 0, array.length);
 3362  
     }
 3363  
 
 3364  
     /**
 3365  
      * <p>
 3366  
      * Joins the elements of the provided array into a single String containing the provided list of elements.
 3367  
      * </p>
 3368  
      * 
 3369  
      * <p>
 3370  
      * No delimiter is added before or after the list. Null objects or empty strings within the array are represented
 3371  
      * by empty strings.
 3372  
      * </p>
 3373  
      * 
 3374  
      * <pre>
 3375  
      * StringUtils.join(null, *)               = null
 3376  
      * StringUtils.join([], *)                 = ""
 3377  
      * StringUtils.join([null], *)             = ""
 3378  
      * StringUtils.join([1, 2, 3], ';')  = "1;2;3"
 3379  
      * StringUtils.join([1, 2, 3], null) = "123"
 3380  
      * </pre>
 3381  
      * 
 3382  
      * @param array
 3383  
      *            the array of values to join together, may be null
 3384  
      * @param separator
 3385  
      *            the separator character to use
 3386  
      * @return the joined String, {@code null} if null array input
 3387  
      * @since 3.2
 3388  
      */
 3389  
     public static String join(final double[] array, final char separator) {
 3390  2
         if (array == null) {
 3391  1
             return null;
 3392  
         }
 3393  1
         return join(array, separator, 0, array.length);
 3394  
     }
 3395  
 
 3396  
 
 3397  
     /**
 3398  
      * <p>Joins the elements of the provided array into a single String
 3399  
      * containing the provided list of elements.</p>
 3400  
      *
 3401  
      * <p>No delimiter is added before or after the list.
 3402  
      * Null objects or empty strings within the array are represented by
 3403  
      * empty strings.</p>
 3404  
      *
 3405  
      * <pre>
 3406  
      * StringUtils.join(null, *)               = null
 3407  
      * StringUtils.join([], *)                 = ""
 3408  
      * StringUtils.join([null], *)             = ""
 3409  
      * StringUtils.join(["a", "b", "c"], ';')  = "a;b;c"
 3410  
      * StringUtils.join(["a", "b", "c"], null) = "abc"
 3411  
      * StringUtils.join([null, "", "a"], ';')  = ";;a"
 3412  
      * </pre>
 3413  
      *
 3414  
      * @param array  the array of values to join together, may be null
 3415  
      * @param separator  the separator character to use
 3416  
      * @param startIndex the first index to start joining from.  It is
 3417  
      * an error to pass in an end index past the end of the array
 3418  
      * @param endIndex the index to stop joining from (exclusive). It is
 3419  
      * an error to pass in an end index past the end of the array
 3420  
      * @return the joined String, {@code null} if null array input
 3421  
      * @since 2.0
 3422  
      */
 3423  
     public static String join(final Object[] array, final char separator, final int startIndex, final int endIndex) {
 3424  14
         if (array == null) {
 3425  0
             return null;
 3426  
         }
 3427  14
         final int noOfItems = endIndex - startIndex;
 3428  14
         if (noOfItems <= 0) {
 3429  4
             return EMPTY;
 3430  
         }
 3431  10
         final StringBuilder buf = new StringBuilder(noOfItems * 16);
 3432  29
         for (int i = startIndex; i < endIndex; i++) {
 3433  19
             if (i > startIndex) {
 3434  9
                 buf.append(separator);
 3435  
             }
 3436  19
             if (array[i] != null) {
 3437  17
                 buf.append(array[i]);
 3438  
             }
 3439  
         }
 3440  10
         return buf.toString();
 3441  
     }
 3442  
 
 3443  
     /**
 3444  
      * <p>
 3445  
      * Joins the elements of the provided array into a single String containing the provided list of elements.
 3446  
      * </p>
 3447  
      * 
 3448  
      * <p>
 3449  
      * No delimiter is added before or after the list. Null objects or empty strings within the array are represented
 3450  
      * by empty strings.
 3451  
      * </p>
 3452  
      * 
 3453  
      * <pre>
 3454  
      * StringUtils.join(null, *)               = null
 3455  
      * StringUtils.join([], *)                 = ""
 3456  
      * StringUtils.join([null], *)             = ""
 3457  
      * StringUtils.join([1, 2, 3], ';')  = "1;2;3"
 3458  
      * StringUtils.join([1, 2, 3], null) = "123"
 3459  
      * </pre>
 3460  
      * 
 3461  
      * @param array
 3462  
      *            the array of values to join together, may be null
 3463  
      * @param separator
 3464  
      *            the separator character to use
 3465  
      * @param startIndex
 3466  
      *            the first index to start joining from. It is an error to pass in an end index past the end of the
 3467  
      *            array
 3468  
      * @param endIndex
 3469  
      *            the index to stop joining from (exclusive). It is an error to pass in an end index past the end of
 3470  
      *            the array
 3471  
      * @return the joined String, {@code null} if null array input
 3472  
      * @since 3.2
 3473  
      */
 3474  
     public static String join(final long[] array, final char separator, final int startIndex, final int endIndex) {
 3475  2
         if (array == null) {
 3476  0
             return null;
 3477  
         }
 3478  2
         final int noOfItems = endIndex - startIndex;
 3479  2
         if (noOfItems <= 0) {
 3480  0
             return EMPTY;
 3481  
         }
 3482  2
         final StringBuilder buf = new StringBuilder(noOfItems * 16);
 3483  5
         for (int i = startIndex; i < endIndex; i++) {
 3484  3
             if (i > startIndex) {
 3485  1
                 buf.append(separator);
 3486  
             }
 3487  3
             buf.append(array[i]);
 3488  
         }
 3489  2
         return buf.toString();
 3490  
     }
 3491  
 
 3492  
     /**
 3493  
      * <p>
 3494  
      * Joins the elements of the provided array into a single String containing the provided list of elements.
 3495  
      * </p>
 3496  
      * 
 3497  
      * <p>
 3498  
      * No delimiter is added before or after the list. Null objects or empty strings within the array are represented
 3499  
      * by empty strings.
 3500  
      * </p>
 3501  
      * 
 3502  
      * <pre>
 3503  
      * StringUtils.join(null, *)               = null
 3504  
      * StringUtils.join([], *)                 = ""
 3505  
      * StringUtils.join([null], *)             = ""
 3506  
      * StringUtils.join([1, 2, 3], ';')  = "1;2;3"
 3507  
      * StringUtils.join([1, 2, 3], null) = "123"
 3508  
      * </pre>
 3509  
      * 
 3510  
      * @param array
 3511  
      *            the array of values to join together, may be null
 3512  
      * @param separator
 3513  
      *            the separator character to use
 3514  
      * @param startIndex
 3515  
      *            the first index to start joining from. It is an error to pass in an end index past the end of the
 3516  
      *            array
 3517  
      * @param endIndex
 3518  
      *            the index to stop joining from (exclusive). It is an error to pass in an end index past the end of
 3519  
      *            the array
 3520  
      * @return the joined String, {@code null} if null array input
 3521  
      * @since 3.2
 3522  
      */
 3523  
     public static String join(final int[] array, final char separator, final int startIndex, final int endIndex) {
 3524  2
         if (array == null) {
 3525  0
             return null;
 3526  
         }
 3527  2
         final int noOfItems = endIndex - startIndex;
 3528  2
         if (noOfItems <= 0) {
 3529  0
             return EMPTY;
 3530  
         }
 3531  2
         final StringBuilder buf = new StringBuilder(noOfItems * 16);
 3532  5
         for (int i = startIndex; i < endIndex; i++) {
 3533  3
             if (i > startIndex) {
 3534  1
                 buf.append(separator);
 3535  
             }
 3536  3
             buf.append(array[i]);
 3537  
         }
 3538  2
         return buf.toString();
 3539  
     }
 3540  
 
 3541  
     /**
 3542  
      * <p>
 3543  
      * Joins the elements of the provided array into a single String containing the provided list of elements.
 3544  
      * </p>
 3545  
      * 
 3546  
      * <p>
 3547  
      * No delimiter is added before or after the list. Null objects or empty strings within the array are represented
 3548  
      * by empty strings.
 3549  
      * </p>
 3550  
      * 
 3551  
      * <pre>
 3552  
      * StringUtils.join(null, *)               = null
 3553  
      * StringUtils.join([], *)                 = ""
 3554  
      * StringUtils.join([null], *)             = ""
 3555  
      * StringUtils.join([1, 2, 3], ';')  = "1;2;3"
 3556  
      * StringUtils.join([1, 2, 3], null) = "123"
 3557  
      * </pre>
 3558  
      * 
 3559  
      * @param array
 3560  
      *            the array of values to join together, may be null
 3561  
      * @param separator
 3562  
      *            the separator character to use
 3563  
      * @param startIndex
 3564  
      *            the first index to start joining from. It is an error to pass in an end index past the end of the
 3565  
      *            array
 3566  
      * @param endIndex
 3567  
      *            the index to stop joining from (exclusive). It is an error to pass in an end index past the end of
 3568  
      *            the array
 3569  
      * @return the joined String, {@code null} if null array input
 3570  
      * @since 3.2
 3571  
      */
 3572  
     public static String join(final byte[] array, final char separator, final int startIndex, final int endIndex) {
 3573  2
         if (array == null) {
 3574  0
             return null;
 3575  
         }
 3576  2
         final int noOfItems = endIndex - startIndex;
 3577  2
         if (noOfItems <= 0) {
 3578  0
             return EMPTY;
 3579  
         }
 3580  2
         final StringBuilder buf = new StringBuilder(noOfItems * 16);
 3581  5
         for (int i = startIndex; i < endIndex; i++) {
 3582  3
             if (i > startIndex) {
 3583  1
                 buf.append(separator);
 3584  
             }
 3585  3
             buf.append(array[i]);
 3586  
         }
 3587  2
         return buf.toString();
 3588  
     }
 3589  
 
 3590  
     /**
 3591  
      * <p>
 3592  
      * Joins the elements of the provided array into a single String containing the provided list of elements.
 3593  
      * </p>
 3594  
      * 
 3595  
      * <p>
 3596  
      * No delimiter is added before or after the list. Null objects or empty strings within the array are represented
 3597  
      * by empty strings.
 3598  
      * </p>
 3599  
      * 
 3600  
      * <pre>
 3601  
      * StringUtils.join(null, *)               = null
 3602  
      * StringUtils.join([], *)                 = ""
 3603  
      * StringUtils.join([null], *)             = ""
 3604  
      * StringUtils.join([1, 2, 3], ';')  = "1;2;3"
 3605  
      * StringUtils.join([1, 2, 3], null) = "123"
 3606  
      * </pre>
 3607  
      * 
 3608  
      * @param array
 3609  
      *            the array of values to join together, may be null
 3610  
      * @param separator
 3611  
      *            the separator character to use
 3612  
      * @param startIndex
 3613  
      *            the first index to start joining from. It is an error to pass in an end index past the end of the
 3614  
      *            array
 3615  
      * @param endIndex
 3616  
      *            the index to stop joining from (exclusive). It is an error to pass in an end index past the end of
 3617  
      *            the array
 3618  
      * @return the joined String, {@code null} if null array input
 3619  
      * @since 3.2
 3620  
      */
 3621  
     public static String join(final short[] array, final char separator, final int startIndex, final int endIndex) {
 3622  2
         if (array == null) {
 3623  0
             return null;
 3624  
         }
 3625  2
         final int noOfItems = endIndex - startIndex;
 3626  2
         if (noOfItems <= 0) {
 3627  0
             return EMPTY;
 3628  
         }
 3629  2
         final StringBuilder buf = new StringBuilder(noOfItems * 16);
 3630  5
         for (int i = startIndex; i < endIndex; i++) {
 3631  3
             if (i > startIndex) {
 3632  1
                 buf.append(separator);
 3633  
             }
 3634  3
             buf.append(array[i]);
 3635  
         }
 3636  2
         return buf.toString();
 3637  
     }
 3638  
 
 3639  
     /**
 3640  
      * <p>
 3641  
      * Joins the elements of the provided array into a single String containing the provided list of elements.
 3642  
      * </p>
 3643  
      * 
 3644  
      * <p>
 3645  
      * No delimiter is added before or after the list. Null objects or empty strings within the array are represented
 3646  
      * by empty strings.
 3647  
      * </p>
 3648  
      * 
 3649  
      * <pre>
 3650  
      * StringUtils.join(null, *)               = null
 3651  
      * StringUtils.join([], *)                 = ""
 3652  
      * StringUtils.join([null], *)             = ""
 3653  
      * StringUtils.join([1, 2, 3], ';')  = "1;2;3"
 3654  
      * StringUtils.join([1, 2, 3], null) = "123"
 3655  
      * </pre>
 3656  
      * 
 3657  
      * @param array
 3658  
      *            the array of values to join together, may be null
 3659  
      * @param separator
 3660  
      *            the separator character to use
 3661  
      * @param startIndex
 3662  
      *            the first index to start joining from. It is an error to pass in an end index past the end of the
 3663  
      *            array
 3664  
      * @param endIndex
 3665  
      *            the index to stop joining from (exclusive). It is an error to pass in an end index past the end of
 3666  
      *            the array
 3667  
      * @return the joined String, {@code null} if null array input
 3668  
      * @since 3.2
 3669  
      */
 3670  
     public static String join(final char[] array, final char separator, final int startIndex, final int endIndex) {
 3671  2
         if (array == null) {
 3672  0
             return null;
 3673  
         }
 3674  2
         final int noOfItems = endIndex - startIndex;
 3675  2
         if (noOfItems <= 0) {
 3676  0
             return EMPTY;
 3677  
         }
 3678  2
         final StringBuilder buf = new StringBuilder(noOfItems * 16);
 3679  5
         for (int i = startIndex; i < endIndex; i++) {
 3680  3
             if (i > startIndex) {
 3681  1
                 buf.append(separator);
 3682  
             }
 3683  3
             buf.append(array[i]);
 3684  
         }
 3685  2
         return buf.toString();
 3686  
     }
 3687  
 
 3688  
     /**
 3689  
      * <p>
 3690  
      * Joins the elements of the provided array into a single String containing the provided list of elements.
 3691  
      * </p>
 3692  
      * 
 3693  
      * <p>
 3694  
      * No delimiter is added before or after the list. Null objects or empty strings within the array are represented
 3695  
      * by empty strings.
 3696  
      * </p>
 3697  
      * 
 3698  
      * <pre>
 3699  
      * StringUtils.join(null, *)               = null
 3700  
      * StringUtils.join([], *)                 = ""
 3701  
      * StringUtils.join([null], *)             = ""
 3702  
      * StringUtils.join([1, 2, 3], ';')  = "1;2;3"
 3703  
      * StringUtils.join([1, 2, 3], null) = "123"
 3704  
      * </pre>
 3705  
      * 
 3706  
      * @param array
 3707  
      *            the array of values to join together, may be null
 3708  
      * @param separator
 3709  
      *            the separator character to use
 3710  
      * @param startIndex
 3711  
      *            the first index to start joining from. It is an error to pass in an end index past the end of the
 3712  
      *            array
 3713  
      * @param endIndex
 3714  
      *            the index to stop joining from (exclusive). It is an error to pass in an end index past the end of
 3715  
      *            the array
 3716  
      * @return the joined String, {@code null} if null array input
 3717  
      * @since 3.2
 3718  
      */
 3719  
     public static String join(final double[] array, final char separator, final int startIndex, final int endIndex) {
 3720  2
         if (array == null) {
 3721  0
             return null;
 3722  
         }
 3723  2
         final int noOfItems = endIndex - startIndex;
 3724  2
         if (noOfItems <= 0) {
 3725  0
             return EMPTY;
 3726  
         }
 3727  2
         final StringBuilder buf = new StringBuilder(noOfItems * 16);
 3728  5
         for (int i = startIndex; i < endIndex; i++) {
 3729  3
             if (i > startIndex) {
 3730  1
                 buf.append(separator);
 3731  
             }
 3732  3
             buf.append(array[i]);
 3733  
         }
 3734  2
         return buf.toString();
 3735  
     }
 3736  
 
 3737  
     /**
 3738  
      * <p>
 3739  
      * Joins the elements of the provided array into a single String containing the provided list of elements.
 3740  
      * </p>
 3741  
      * 
 3742  
      * <p>
 3743  
      * No delimiter is added before or after the list. Null objects or empty strings within the array are represented
 3744  
      * by empty strings.
 3745  
      * </p>
 3746  
      * 
 3747  
      * <pre>
 3748  
      * StringUtils.join(null, *)               = null
 3749  
      * StringUtils.join([], *)                 = ""
 3750  
      * StringUtils.join([null], *)             = ""
 3751  
      * StringUtils.join([1, 2, 3], ';')  = "1;2;3"
 3752  
      * StringUtils.join([1, 2, 3], null) = "123"
 3753  
      * </pre>
 3754  
      * 
 3755  
      * @param array
 3756  
      *            the array of values to join together, may be null
 3757  
      * @param separator
 3758  
      *            the separator character to use
 3759  
      * @param startIndex
 3760  
      *            the first index to start joining from. It is an error to pass in an end index past the end of the
 3761  
      *            array
 3762  
      * @param endIndex
 3763  
      *            the index to stop joining from (exclusive). It is an error to pass in an end index past the end of
 3764  
      *            the array
 3765  
      * @return the joined String, {@code null} if null array input
 3766  
      * @since 3.2
 3767  
      */
 3768  
     public static String join(final float[] array, final char separator, final int startIndex, final int endIndex) {
 3769  2
         if (array == null) {
 3770  0
             return null;
 3771  
         }
 3772  2
         final int noOfItems = endIndex - startIndex;
 3773  2
         if (noOfItems <= 0) {
 3774  0
             return EMPTY;
 3775  
         }
 3776  2
         final StringBuilder buf = new StringBuilder(noOfItems * 16);
 3777  5
         for (int i = startIndex; i < endIndex; i++) {
 3778  3
             if (i > startIndex) {
 3779  1
                 buf.append(separator);
 3780  
             }
 3781  3
             buf.append(array[i]);
 3782  
         }
 3783  2
         return buf.toString();
 3784  
     }
 3785  
 
 3786  
 
 3787  
     /**
 3788  
      * <p>Joins the elements of the provided array into a single String
 3789  
      * containing the provided list of elements.</p>
 3790  
      *
 3791  
      * <p>No delimiter is added before or after the list.
 3792  
      * A {@code null} separator is the same as an empty String ("").
 3793  
      * Null objects or empty strings within the array are represented by
 3794  
      * empty strings.</p>
 3795  
      *
 3796  
      * <pre>
 3797  
      * StringUtils.join(null, *)                = null
 3798  
      * StringUtils.join([], *)                  = ""
 3799  
      * StringUtils.join([null], *)              = ""
 3800  
      * StringUtils.join(["a", "b", "c"], "--")  = "a--b--c"
 3801  
      * StringUtils.join(["a", "b", "c"], null)  = "abc"
 3802  
      * StringUtils.join(["a", "b", "c"], "")    = "abc"
 3803  
      * StringUtils.join([null, "", "a"], ',')   = ",,a"
 3804  
      * </pre>
 3805  
      *
 3806  
      * @param array  the array of values to join together, may be null
 3807  
      * @param separator  the separator character to use, null treated as ""
 3808  
      * @return the joined String, {@code null} if null array input
 3809  
      */
 3810  
     public static String join(final Object[] array, final String separator) {
 3811  34
         if (array == null) {
 3812  3
             return null;
 3813  
         }
 3814  31
         return join(array, separator, 0, array.length);
 3815  
     }
 3816  
 
 3817  
     /**
 3818  
      * <p>Joins the elements of the provided array into a single String
 3819  
      * containing the provided list of elements.</p>
 3820  
      *
 3821  
      * <p>No delimiter is added before or after the list.
 3822  
      * A {@code null} separator is the same as an empty String ("").
 3823  
      * Null objects or empty strings within the array are represented by
 3824  
      * empty strings.</p>
 3825  
      *
 3826  
      * <pre>
 3827  
      * StringUtils.join(null, *, *, *)                = null
 3828  
      * StringUtils.join([], *, *, *)                  = ""
 3829  
      * StringUtils.join([null], *, *, *)              = ""
 3830  
      * StringUtils.join(["a", "b", "c"], "--", 0, 3)  = "a--b--c"
 3831  
      * StringUtils.join(["a", "b", "c"], "--", 1, 3)  = "b--c"
 3832  
      * StringUtils.join(["a", "b", "c"], "--", 2, 3)  = "c"
 3833  
      * StringUtils.join(["a", "b", "c"], "--", 2, 2)  = ""
 3834  
      * StringUtils.join(["a", "b", "c"], null, 0, 3)  = "abc"
 3835  
      * StringUtils.join(["a", "b", "c"], "", 0, 3)    = "abc"
 3836  
      * StringUtils.join([null, "", "a"], ',', 0, 3)   = ",,a"
 3837  
      * </pre>
 3838  
      *
 3839  
      * @param array  the array of values to join together, may be null
 3840  
      * @param separator  the separator character to use, null treated as ""
 3841  
      * @param startIndex the first index to start joining from.
 3842  
      * @param endIndex the index to stop joining from (exclusive).
 3843  
      * @return the joined String, {@code null} if null array input; or the empty string
 3844  
      * if {@code endIndex - startIndex <= 0}. The number of joined entries is given by
 3845  
      * {@code endIndex - startIndex}
 3846  
      * @throws ArrayIndexOutOfBoundsException ife<br/>
 3847  
      * {@code startIndex < 0} or <br/>
 3848  
      * {@code startIndex >= array.length()} or <br/>
 3849  
      * {@code endIndex < 0} or <br/>
 3850  
      * {@code endIndex > array.length()} 
 3851  
      */
 3852  
     public static String join(final Object[] array, String separator, final int startIndex, final int endIndex) {
 3853  37
         if (array == null) {
 3854  0
             return null;
 3855  
         }
 3856  37
         if (separator == null) {
 3857  25
             separator = EMPTY;
 3858  
         }
 3859  
 
 3860  
         // endIndex - startIndex > 0:   Len = NofStrings *(len(firstString) + len(separator))
 3861  
         //           (Assuming that all Strings are roughly equally long)
 3862  37
         final int noOfItems = endIndex - startIndex;
 3863  37
         if (noOfItems <= 0) {
 3864  10
             return EMPTY;
 3865  
         }
 3866  
 
 3867  27
         final StringBuilder buf = new StringBuilder(noOfItems * 16);
 3868  
 
 3869  102
         for (int i = startIndex; i < endIndex; i++) {
 3870  75
             if (i > startIndex) {
 3871  48
                 buf.append(separator);
 3872  
             }
 3873  75
             if (array[i] != null) {
 3874  66
                 buf.append(array[i]);
 3875  
             }
 3876  
         }
 3877  27
         return buf.toString();
 3878  
     }
 3879  
 
 3880  
     /**
 3881  
      * <p>Joins the elements of the provided {@code Iterator} into
 3882  
      * a single String containing the provided elements.</p>
 3883  
      *
 3884  
      * <p>No delimiter is added before or after the list. Null objects or empty
 3885  
      * strings within the iteration are represented by empty strings.</p>
 3886  
      *
 3887  
      * <p>See the examples here: {@link #join(Object[],char)}. </p>
 3888  
      *
 3889  
      * @param iterator  the {@code Iterator} of values to join together, may be null
 3890  
      * @param separator  the separator character to use
 3891  
      * @return the joined String, {@code null} if null iterator input
 3892  
      * @since 2.0
 3893  
      */
 3894  
     public static String join(final Iterator<?> iterator, final char separator) {
 3895  
 
 3896  
         // handle null, zero and one elements before building a buffer
 3897  9
         if (iterator == null) {
 3898  1
             return null;
 3899  
         }
 3900  8
         if (!iterator.hasNext()) {
 3901  2
             return EMPTY;
 3902  
         }
 3903  6
         final Object first = iterator.next();
 3904  6
         if (!iterator.hasNext()) {
 3905  4
             return ObjectUtils.toString(first);
 3906  
         }
 3907  
 
 3908  
         // two or more elements
 3909  2
         final StringBuilder buf = new StringBuilder(256); // Java default is 16, probably too small
 3910  2
         if (first != null) {
 3911  2
             buf.append(first);
 3912  
         }
 3913  
 
 3914  6
         while (iterator.hasNext()) {
 3915  4
             buf.append(separator);
 3916  4
             final Object obj = iterator.next();
 3917  4
             if (obj != null) {
 3918  4
                 buf.append(obj);
 3919  
             }
 3920  4
         }
 3921  
 
 3922  2
         return buf.toString();
 3923  
     }
 3924  
 
 3925  
     /**
 3926  
      * <p>Joins the elements of the provided {@code Iterator} into
 3927  
      * a single String containing the provided elements.</p>
 3928  
      *
 3929  
      * <p>No delimiter is added before or after the list.
 3930  
      * A {@code null} separator is the same as an empty String ("").</p>
 3931  
      *
 3932  
      * <p>See the examples here: {@link #join(Object[],String)}. </p>
 3933  
      *
 3934  
      * @param iterator  the {@code Iterator} of values to join together, may be null
 3935  
      * @param separator  the separator character to use, null treated as ""
 3936  
      * @return the joined String, {@code null} if null iterator input
 3937  
      */
 3938  
     public static String join(final Iterator<?> iterator, final String separator) {
 3939  
 
 3940  
         // handle null, zero and one elements before building a buffer
 3941  19
         if (iterator == null) {
 3942  1
             return null;
 3943  
         }
 3944  18
         if (!iterator.hasNext()) {
 3945  6
             return EMPTY;
 3946  
         }
 3947  12
         final Object first = iterator.next();
 3948  12
         if (!iterator.hasNext()) {
 3949  6
             return ObjectUtils.toString(first);
 3950  
         }
 3951  
 
 3952  
         // two or more elements
 3953  6
         final StringBuilder buf = new StringBuilder(256); // Java default is 16, probably too small
 3954  6
         if (first != null) {
 3955  6
             buf.append(first);
 3956  
         }
 3957  
 
 3958  18
         while (iterator.hasNext()) {
 3959  12
             if (separator != null) {
 3960  8
                 buf.append(separator);
 3961  
             }
 3962  12
             final Object obj = iterator.next();
 3963  12
             if (obj != null) {
 3964  12
                 buf.append(obj);
 3965  
             }
 3966  12
         }
 3967  6
         return buf.toString();
 3968  
     }
 3969  
 
 3970  
     /**
 3971  
      * <p>Joins the elements of the provided {@code Iterable} into
 3972  
      * a single String containing the provided elements.</p>
 3973  
      *
 3974  
      * <p>No delimiter is added before or after the list. Null objects or empty
 3975  
      * strings within the iteration are represented by empty strings.</p>
 3976  
      *
 3977  
      * <p>See the examples here: {@link #join(Object[],char)}. </p>
 3978  
      *
 3979  
      * @param iterable  the {@code Iterable} providing the values to join together, may be null
 3980  
      * @param separator  the separator character to use
 3981  
      * @return the joined String, {@code null} if null iterator input
 3982  
      * @since 2.3
 3983  
      */
 3984  
     public static String join(final Iterable<?> iterable, final char separator) {
 3985  5
         if (iterable == null) {
 3986  1
             return null;
 3987  
         }
 3988  4
         return join(iterable.iterator(), separator);
 3989  
     }
 3990  
 
 3991  
     /**
 3992  
      * <p>Joins the elements of the provided {@code Iterable} into
 3993  
      * a single String containing the provided elements.</p>
 3994  
      *
 3995  
      * <p>No delimiter is added before or after the list.
 3996  
      * A {@code null} separator is the same as an empty String ("").</p>
 3997  
      *
 3998  
      * <p>See the examples here: {@link #join(Object[],String)}. </p>
 3999  
      *
 4000  
      * @param iterable  the {@code Iterable} providing the values to join together, may be null
 4001  
      * @param separator  the separator character to use, null treated as ""
 4002  
      * @return the joined String, {@code null} if null iterator input
 4003  
      * @since 2.3
 4004  
      */
 4005  
     public static String join(final Iterable<?> iterable, final String separator) {
 4006  10
         if (iterable == null) {
 4007  1
             return null;
 4008  
         }
 4009  9
         return join(iterable.iterator(), separator);
 4010  
     }
 4011  
 
 4012  
     // Delete
 4013  
     //-----------------------------------------------------------------------
 4014  
     /**
 4015  
      * <p>Deletes all whitespaces from a String as defined by
 4016  
      * {@link Character#isWhitespace(char)}.</p>
 4017  
      *
 4018  
      * <pre>
 4019  
      * StringUtils.deleteWhitespace(null)         = null
 4020  
      * StringUtils.deleteWhitespace("")           = ""
 4021  
      * StringUtils.deleteWhitespace("abc")        = "abc"
 4022  
      * StringUtils.deleteWhitespace("   ab  c  ") = "abc"
 4023  
      * </pre>
 4024  
      *
 4025  
      * @param str  the String to delete whitespace from, may be null
 4026  
      * @return the String without whitespaces, {@code null} if null String input
 4027  
      */
 4028  
     public static String deleteWhitespace(final String str) {
 4029  134
         if (isEmpty(str)) {
 4030  3
             return str;
 4031  
         }
 4032  131
         final int sz = str.length();
 4033  131
         final char[] chs = new char[sz];
 4034  131
         int count = 0;
 4035  2950
         for (int i = 0; i < sz; i++) {
 4036  2819
             if (!Character.isWhitespace(str.charAt(i))) {
 4037  2721
                 chs[count++] = str.charAt(i);
 4038  
             }
 4039  
         }
 4040  131
         if (count == sz) {
 4041  123
             return str;
 4042  
         }
 4043  8
         return new String(chs, 0, count);
 4044  
     }
 4045  
 
 4046  
     // Remove
 4047  
     //-----------------------------------------------------------------------
 4048  
     /**
 4049  
      * <p>Removes a substring only if it is at the beginning of a source string,
 4050  
      * otherwise returns the source string.</p>
 4051  
      *
 4052  
      * <p>A {@code null} source string will return {@code null}.
 4053  
      * An empty ("") source string will return the empty string.
 4054  
      * A {@code null} search string will return the source string.</p>
 4055  
      *
 4056  
      * <pre>
 4057  
      * StringUtils.removeStart(null, *)      = null
 4058  
      * StringUtils.removeStart("", *)        = ""
 4059  
      * StringUtils.removeStart(*, null)      = *
 4060  
      * StringUtils.removeStart("www.domain.com", "www.")   = "domain.com"
 4061  
      * StringUtils.removeStart("domain.com", "www.")       = "domain.com"
 4062  
      * StringUtils.removeStart("www.domain.com", "domain") = "www.domain.com"
 4063  
      * StringUtils.removeStart("abc", "")    = "abc"
 4064  
      * </pre>
 4065  
      *
 4066  
      * @param str  the source String to search, may be null
 4067  
      * @param remove  the String to search for and remove, may be null
 4068  
      * @return the substring with the string removed if found,
 4069  
      *  {@code null} if null String input
 4070  
      * @since 2.1
 4071  
      */
 4072  
     public static String removeStart(final String str, final String remove) {
 4073  10
         if (isEmpty(str) || isEmpty(remove)) {
 4074  8
             return str;
 4075  
         }
 4076  2
         if (str.startsWith(remove)){
 4077  1
             return str.substring(remove.length());
 4078  
         }
 4079  1
         return str;
 4080  
     }
 4081  
 
 4082  
     /**
 4083  
      * <p>Case insensitive removal of a substring if it is at the beginning of a source string,
 4084  
      * otherwise returns the source string.</p>
 4085  
      *
 4086  
      * <p>A {@code null} source string will return {@code null}.
 4087  
      * An empty ("") source string will return the empty string.
 4088  
      * A {@code null} search string will return the source string.</p>
 4089  
      *
 4090  
      * <pre>
 4091  
      * StringUtils.removeStartIgnoreCase(null, *)      = null
 4092  
      * StringUtils.removeStartIgnoreCase("", *)        = ""
 4093  
      * StringUtils.removeStartIgnoreCase(*, null)      = *
 4094  
      * StringUtils.removeStartIgnoreCase("www.domain.com", "www.")   = "domain.com"
 4095  
      * StringUtils.removeStartIgnoreCase("www.domain.com", "WWW.")   = "domain.com"
 4096  
      * StringUtils.removeStartIgnoreCase("domain.com", "www.")       = "domain.com"
 4097  
      * StringUtils.removeStartIgnoreCase("www.domain.com", "domain") = "www.domain.com"
 4098  
      * StringUtils.removeStartIgnoreCase("abc", "")    = "abc"
 4099  
      * </pre>
 4100  
      *
 4101  
      * @param str  the source String to search, may be null
 4102  
      * @param remove  the String to search for (case insensitive) and remove, may be null
 4103  
      * @return the substring with the string removed if found,
 4104  
      *  {@code null} if null String input
 4105  
      * @since 2.4
 4106  
      */
 4107  
     public static String removeStartIgnoreCase(final String str, final String remove) {
 4108  11
         if (isEmpty(str) || isEmpty(remove)) {
 4109  8
             return str;
 4110  
         }
 4111  3
         if (startsWithIgnoreCase(str, remove)) {
 4112  2
             return str.substring(remove.length());
 4113  
         }
 4114  1
         return str;
 4115  
     }
 4116  
 
 4117  
     /**
 4118  
      * <p>Removes a substring only if it is at the end of a source string,
 4119  
      * otherwise returns the source string.</p>
 4120  
      *
 4121  
      * <p>A {@code null} source string will return {@code null}.
 4122  
      * An empty ("") source string will return the empty string.
 4123  
      * A {@code null} search string will return the source string.</p>
 4124  
      *
 4125  
      * <pre>
 4126  
      * StringUtils.removeEnd(null, *)      = null
 4127  
      * StringUtils.removeEnd("", *)        = ""
 4128  
      * StringUtils.removeEnd(*, null)      = *
 4129  
      * StringUtils.removeEnd("www.domain.com", ".com.")  = "www.domain.com"
 4130  
      * StringUtils.removeEnd("www.domain.com", ".com")   = "www.domain"
 4131  
      * StringUtils.removeEnd("www.domain.com", "domain") = "www.domain.com"
 4132  
      * StringUtils.removeEnd("abc", "")    = "abc"
 4133  
      * </pre>
 4134  
      *
 4135  
      * @param str  the source String to search, may be null
 4136  
      * @param remove  the String to search for and remove, may be null
 4137  
      * @return the substring with the string removed if found,
 4138  
      *  {@code null} if null String input
 4139  
      * @since 2.1
 4140  
      */
 4141  
     public static String removeEnd(final String str, final String remove) {
 4142  29
         if (isEmpty(str) || isEmpty(remove)) {
 4143  18
             return str;
 4144  
         }
 4145  11
         if (str.endsWith(remove)) {
 4146  6
             return str.substring(0, str.length() - remove.length());
 4147  
         }
 4148  5
         return str;
 4149  
     }
 4150  
 
 4151  
     /**
 4152  
      * <p>Case insensitive removal of a substring if it is at the end of a source string,
 4153  
      * otherwise returns the source string.</p>
 4154  
      *
 4155  
      * <p>A {@code null} source string will return {@code null}.
 4156  
      * An empty ("") source string will return the empty string.
 4157  
      * A {@code null} search string will return the source string.</p>
 4158  
      *
 4159  
      * <pre>
 4160  
      * StringUtils.removeEndIgnoreCase(null, *)      = null
 4161  
      * StringUtils.removeEndIgnoreCase("", *)        = ""
 4162  
      * StringUtils.removeEndIgnoreCase(*, null)      = *
 4163  
      * StringUtils.removeEndIgnoreCase("www.domain.com", ".com.")  = "www.domain.com"
 4164  
      * StringUtils.removeEndIgnoreCase("www.domain.com", ".com")   = "www.domain"
 4165  
      * StringUtils.removeEndIgnoreCase("www.domain.com", "domain") = "www.domain.com"
 4166  
      * StringUtils.removeEndIgnoreCase("abc", "")    = "abc"
 4167  
      * StringUtils.removeEndIgnoreCase("www.domain.com", ".COM") = "www.domain")
 4168  
      * StringUtils.removeEndIgnoreCase("www.domain.COM", ".com") = "www.domain")
 4169  
      * </pre>
 4170  
      *
 4171  
      * @param str  the source String to search, may be null
 4172  
      * @param remove  the String to search for (case insensitive) and remove, may be null
 4173  
      * @return the substring with the string removed if found,
 4174  
      *  {@code null} if null String input
 4175  
      * @since 2.4
 4176  
      */
 4177  
     public static String removeEndIgnoreCase(final String str, final String remove) {
 4178  13
         if (isEmpty(str) || isEmpty(remove)) {
 4179  8
             return str;
 4180  
         }
 4181  5
         if (endsWithIgnoreCase(str, remove)) {
 4182  3
             return str.substring(0, str.length() - remove.length());
 4183  
         }
 4184  2
         return str;
 4185  
     }
 4186  
 
 4187  
     /**
 4188  
      * <p>Removes all occurrences of a substring from within the source string.</p>
 4189  
      *
 4190  
      * <p>A {@code null} source string will return {@code null}.
 4191  
      * An empty ("") source string will return the empty string.
 4192  
      * A {@code null} remove string will return the source string.
 4193  
      * An empty ("") remove string will return the source string.</p>
 4194  
      *
 4195  
      * <pre>
 4196  
      * StringUtils.remove(null, *)        = null
 4197  
      * StringUtils.remove("", *)          = ""
 4198  
      * StringUtils.remove(*, null)        = *
 4199  
      * StringUtils.remove(*, "")          = *
 4200  
      * StringUtils.remove("queued", "ue") = "qd"
 4201  
      * StringUtils.remove("queued", "zz") = "queued"
 4202  
      * </pre>
 4203  
      *
 4204  
      * @param str  the source String to search, may be null
 4205  
      * @param remove  the String to search for and remove, may be null
 4206  
      * @return the substring with the string removed if found,
 4207  
      *  {@code null} if null String input
 4208  
      * @since 2.1
 4209  
      */
 4210  
     public static String remove(final String str, final String remove) {
 4211  14
         if (isEmpty(str) || isEmpty(remove)) {
 4212  12
             return str;
 4213  
         }
 4214  2
         return replace(str, remove, EMPTY, -1);
 4215  
     }
 4216  
 
 4217  
     /**
 4218  
      * <p>Removes all occurrences of a character from within the source string.</p>
 4219  
      *
 4220  
      * <p>A {@code null} source string will return {@code null}.
 4221  
      * An empty ("") source string will return the empty string.</p>
 4222  
      *
 4223  
      * <pre>
 4224  
      * StringUtils.remove(null, *)       = null
 4225  
      * StringUtils.remove("", *)         = ""
 4226  
      * StringUtils.remove("queued", 'u') = "qeed"
 4227  
      * StringUtils.remove("queued", 'z') = "queued"
 4228  
      * </pre>
 4229  
      *
 4230  
      * @param str  the source String to search, may be null
 4231  
      * @param remove  the char to search for and remove, may be null
 4232  
      * @return the substring with the char removed if found,
 4233  
      *  {@code null} if null String input
 4234  
      * @since 2.1
 4235  
      */
 4236  
     public static String remove(final String str, final char remove) {
 4237  8
         if (isEmpty(str) || str.indexOf(remove) == INDEX_NOT_FOUND) {
 4238  7
             return str;
 4239  
         }
 4240  1
         final char[] chars = str.toCharArray();
 4241  1
         int pos = 0;
 4242  7
         for (int i = 0; i < chars.length; i++) {
 4243  6
             if (chars[i] != remove) {
 4244  4
                 chars[pos++] = chars[i];
 4245  
             }
 4246  
         }
 4247  1
         return new String(chars, 0, pos);
 4248  
     }
 4249  
 
 4250  
     // Replacing
 4251  
     //-----------------------------------------------------------------------
 4252  
     /**
 4253  
      * <p>Replaces a String with another String inside a larger String, once.</p>
 4254  
      *
 4255  
      * <p>A {@code null} reference passed to this method is a no-op.</p>
 4256  
      *
 4257  
      * <pre>
 4258  
      * StringUtils.replaceOnce(null, *, *)        = null
 4259  
      * StringUtils.replaceOnce("", *, *)          = ""
 4260  
      * StringUtils.replaceOnce("any", null, *)    = "any"
 4261  
      * StringUtils.replaceOnce("any", *, null)    = "any"
 4262  
      * StringUtils.replaceOnce("any", "", *)      = "any"
 4263  
      * StringUtils.replaceOnce("aba", "a", null)  = "aba"
 4264  
      * StringUtils.replaceOnce("aba", "a", "")    = "ba"
 4265  
      * StringUtils.replaceOnce("aba", "a", "z")   = "zba"
 4266  
      * </pre>
 4267  
      *
 4268  
      * @see #replace(String text, String searchString, String replacement, int max)
 4269  
      * @param text  text to search and replace in, may be null
 4270  
      * @param searchString  the String to search for, may be null
 4271  
      * @param replacement  the String to replace with, may be null
 4272  
      * @return the text with any replacements processed,
 4273  
      *  {@code null} if null String input
 4274  
      */
 4275  
     public static String replaceOnce(final String text, final String searchString, final String replacement) {
 4276  341
         return replace(text, searchString, replacement, 1);
 4277  
     }
 4278  
 
 4279  
     /**
 4280  
      * Replaces each substring of the source String that matches the given regular expression with the given
 4281  
      * replacement using the {@link Pattern#DOTALL} option. DOTALL is also know as single-line mode in Perl. This call
 4282  
      * is also equivalent to:
 4283  
      * <ul>
 4284  
      * <li>{@code source.replaceAll(&quot;(?s)&quot; + regex, replacement)}</li>
 4285  
      * <li>{@code Pattern.compile(regex, Pattern.DOTALL).matcher(source).replaceAll(replacement)}</li>
 4286  
      * </ul>
 4287  
      * 
 4288  
      * @param source
 4289  
      *            the source string
 4290  
      * @param regex
 4291  
      *            the regular expression to which this string is to be matched
 4292  
      * @param replacement
 4293  
      *            the string to be substituted for each match
 4294  
      * @return The resulting {@code String}
 4295  
      * @see String#replaceAll(String, String)
 4296  
      * @see Pattern#DOTALL
 4297  
      * @since 3.2
 4298  
      */
 4299  
     public static String replacePattern(final String source, final String regex, final String replacement) {
 4300  2
         return Pattern.compile(regex, Pattern.DOTALL).matcher(source).replaceAll(replacement);
 4301  
     }
 4302  
 
 4303  
     /**
 4304  
      * Removes each substring of the source String that matches the given regular expression using the DOTALL option.
 4305  
      * 
 4306  
      * @param source
 4307  
      *            the source string
 4308  
      * @param regex
 4309  
      *            the regular expression to which this string is to be matched
 4310  
      * @return The resulting {@code String}
 4311  
      * @see String#replaceAll(String, String)
 4312  
      * @see Pattern#DOTALL
 4313  
      * @since 3.2
 4314  
      */
 4315  
     public static String removePattern(final String source, final String regex) {
 4316  1
         return replacePattern(source, regex, StringUtils.EMPTY);
 4317  
     }
 4318  
 
 4319  
     /**
 4320  
      * <p>Replaces all occurrences of a String within another String.</p>
 4321  
      *
 4322  
      * <p>A {@code null} reference passed to this method is a no-op.</p>
 4323  
      *
 4324  
      * <pre>
 4325  
      * StringUtils.replace(null, *, *)        = null
 4326  
      * StringUtils.replace("", *, *)          = ""
 4327  
      * StringUtils.replace("any", null, *)    = "any"
 4328  
      * StringUtils.replace("any", *, null)    = "any"
 4329  
      * StringUtils.replace("any", "", *)      = "any"
 4330  
      * StringUtils.replace("aba", "a", null)  = "aba"
 4331  
      * StringUtils.replace("aba", "a", "")    = "b"
 4332  
      * StringUtils.replace("aba", "a", "z")   = "zbz"
 4333  
      * </pre>
 4334  
      *
 4335  
      * @see #replace(String text, String searchString, String replacement, int max)
 4336  
      * @param text  text to search and replace in, may be null
 4337  
      * @param searchString  the String to search for, may be null
 4338  
      * @param replacement  the String to replace it with, may be null
 4339  
      * @return the text with any replacements processed,
 4340  
      *  {@code null} if null String input
 4341  
      */
 4342  
     public static String replace(final String text, final String searchString, final String replacement) {
 4343  31
         return replace(text, searchString, replacement, -1);
 4344  
     }
 4345  
 
 4346  
     /**
 4347  
      * <p>Replaces a String with another String inside a larger String,
 4348  
      * for the first {@code max} values of the search String.</p>
 4349  
      *
 4350  
      * <p>A {@code null} reference passed to this method is a no-op.</p>
 4351  
      *
 4352  
      * <pre>
 4353  
      * StringUtils.replace(null, *, *, *)         = null
 4354  
      * StringUtils.replace("", *, *, *)           = ""
 4355  
      * StringUtils.replace("any", null, *, *)     = "any"
 4356  
      * StringUtils.replace("any", *, null, *)     = "any"
 4357  
      * StringUtils.replace("any", "", *, *)       = "any"
 4358  
      * StringUtils.replace("any", *, *, 0)        = "any"
 4359  
      * StringUtils.replace("abaa", "a", null, -1) = "abaa"
 4360  
      * StringUtils.replace("abaa", "a", "", -1)   = "b"
 4361  
      * StringUtils.replace("abaa", "a", "z", 0)   = "abaa"
 4362  
      * StringUtils.replace("abaa", "a", "z", 1)   = "zbaa"
 4363  
      * StringUtils.replace("abaa", "a", "z", 2)   = "zbza"
 4364  
      * StringUtils.replace("abaa", "a", "z", -1)  = "zbzz"
 4365  
      * </pre>
 4366  
      *
 4367  
      * @param text  text to search and replace in, may be null
 4368  
      * @param searchString  the String to search for, may be null
 4369  
      * @param replacement  the String to replace it with, may be null
 4370  
      * @param max  maximum number of values to replace, or {@code -1} if no maximum
 4371  
      * @return the text with any replacements processed,
 4372  
      *  {@code null} if null String input
 4373  
      */
 4374  
     public static String replace(final String text, final String searchString, final String replacement, int max) {
 4375  391
         if (isEmpty(text) || isEmpty(searchString) || replacement == null || max == 0) {
 4376  33
             return text;
 4377  
         }
 4378  358
         int start = 0;
 4379  358
         int end = text.indexOf(searchString, start);
 4380  358
         if (end == INDEX_NOT_FOUND) {
 4381  281
             return text;
 4382  
         }
 4383  77
         final int replLength = searchString.length();
 4384  77
         int increase = replacement.length() - replLength;
 4385  77
         increase = increase < 0 ? 0 : increase;
 4386  77
         increase *= max < 0 ? 16 : max > 64 ? 64 : max;
 4387  77
         final StringBuilder buf = new StringBuilder(text.length() + increase);
 4388  110
         while (end != INDEX_NOT_FOUND) {
 4389  99
             buf.append(text.substring(start, end)).append(replacement);
 4390  99
             start = end + replLength;
 4391  99
             if (--max == 0) {
 4392  66
                 break;
 4393  
             }
 4394  33
             end = text.indexOf(searchString, start);
 4395  
         }
 4396  77
         buf.append(text.substring(start));
 4397  77
         return buf.toString();
 4398  
     }
 4399  
 
 4400  
     /**
 4401  
      * <p>
 4402  
      * Replaces all occurrences of Strings within another String.
 4403  
      * </p>
 4404  
      *
 4405  
      * <p>
 4406  
      * A {@code null} reference passed to this method is a no-op, or if
 4407  
      * any "search string" or "string to replace" is null, that replace will be
 4408  
      * ignored. This will not repeat. For repeating replaces, call the
 4409  
      * overloaded method.
 4410  
      * </p>
 4411  
      *
 4412  
      * <pre>
 4413  
      *  StringUtils.replaceEach(null, *, *)        = null
 4414  
      *  StringUtils.replaceEach("", *, *)          = ""
 4415  
      *  StringUtils.replaceEach("aba", null, null) = "aba"
 4416  
      *  StringUtils.replaceEach("aba", new String[0], null) = "aba"
 4417  
      *  StringUtils.replaceEach("aba", null, new String[0]) = "aba"
 4418  
      *  StringUtils.replaceEach("aba", new String[]{"a"}, null)  = "aba"
 4419  
      *  StringUtils.replaceEach("aba", new String[]{"a"}, new String[]{""})  = "b"
 4420  
      *  StringUtils.replaceEach("aba", new String[]{null}, new String[]{"a"})  = "aba"
 4421  
      *  StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"w", "t"})  = "wcte"
 4422  
      *  (example of how it does not repeat)
 4423  
      *  StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"})  = "dcte"
 4424  
      * </pre>
 4425  
      *
 4426  
      * @param text
 4427  
      *            text to search and replace in, no-op if null
 4428  
      * @param searchList
 4429  
      *            the Strings to search for, no-op if null
 4430  
      * @param replacementList
 4431  
      *            the Strings to replace them with, no-op if null
 4432  
      * @return the text with any replacements processed, {@code null} if
 4433  
      *         null String input
 4434  
      * @throws IllegalArgumentException
 4435  
      *             if the lengths of the arrays are not the same (null is ok,
 4436  
      *             and/or size 0)
 4437  
      * @since 2.4
 4438  
      */
 4439  
     public static String replaceEach(final String text, final String[] searchList, final String[] replacementList) {
 4440  14
         return replaceEach(text, searchList, replacementList, false, 0);
 4441  
     }
 4442  
 
 4443  
     /**
 4444  
      * <p>
 4445  
      * Replaces all occurrences of Strings within another String.
 4446  
      * </p>
 4447  
      *
 4448  
      * <p>
 4449  
      * A {@code null} reference passed to this method is a no-op, or if
 4450  
      * any "search string" or "string to replace" is null, that replace will be
 4451  
      * ignored. 
 4452  
      * </p>
 4453  
      *
 4454  
      * <pre>
 4455  
      *  StringUtils.replaceEach(null, *, *, *) = null
 4456  
      *  StringUtils.replaceEach("", *, *, *) = ""
 4457  
      *  StringUtils.replaceEach("aba", null, null, *) = "aba"
 4458  
      *  StringUtils.replaceEach("aba", new String[0], null, *) = "aba"
 4459  
      *  StringUtils.replaceEach("aba", null, new String[0], *) = "aba"
 4460  
      *  StringUtils.replaceEach("aba", new String[]{"a"}, null, *) = "aba"
 4461  
      *  StringUtils.replaceEach("aba", new String[]{"a"}, new String[]{""}, *) = "b"
 4462  
      *  StringUtils.replaceEach("aba", new String[]{null}, new String[]{"a"}, *) = "aba"
 4463  
      *  StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"w", "t"}, *) = "wcte"
 4464  
      *  (example of how it repeats)
 4465  
      *  StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"}, false) = "dcte"
 4466  
      *  StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"}, true) = "tcte"
 4467  
      *  StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "ab"}, true) = IllegalStateException
 4468  
      *  StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "ab"}, false) = "dcabe"
 4469  
      * </pre>
 4470  
      *
 4471  
      * @param text
 4472  
      *            text to search and replace in, no-op if null
 4473  
      * @param searchList
 4474  
      *            the Strings to search for, no-op if null
 4475  
      * @param replacementList
 4476  
      *            the Strings to replace them with, no-op if null
 4477  
      * @return the text with any replacements processed, {@code null} if
 4478  
      *         null String input
 4479  
      * @throws IllegalStateException
 4480  
      *             if the search is repeating and there is an endless loop due
 4481  
      *             to outputs of one being inputs to another
 4482  
      * @throws IllegalArgumentException
 4483  
      *             if the lengths of the arrays are not the same (null is ok,
 4484  
      *             and/or size 0)
 4485  
      * @since 2.4
 4486  
      */
 4487  
     public static String replaceEachRepeatedly(final String text, final String[] searchList, final String[] replacementList) {
 4488  
         // timeToLive should be 0 if not used or nothing to replace, else it's
 4489  
         // the length of the replace array
 4490  11
         final int timeToLive = searchList == null ? 0 : searchList.length;
 4491  11
         return replaceEach(text, searchList, replacementList, true, timeToLive);
 4492  
     }
 4493  
 
 4494  
     /**
 4495  
      * <p>
 4496  
      * Replaces all occurrences of Strings within another String.
 4497  
      * </p>
 4498  
      *
 4499  
      * <p>
 4500  
      * A {@code null} reference passed to this method is a no-op, or if
 4501  
      * any "search string" or "string to replace" is null, that replace will be
 4502  
      * ignored.
 4503  
      * </p>
 4504  
      *
 4505  
      * <pre>
 4506  
      *  StringUtils.replaceEach(null, *, *, *) = null
 4507  
      *  StringUtils.replaceEach("", *, *, *) = ""
 4508  
      *  StringUtils.replaceEach("aba", null, null, *) = "aba"
 4509  
      *  StringUtils.replaceEach("aba", new String[0], null, *) = "aba"
 4510  
      *  StringUtils.replaceEach("aba", null, new String[0], *) = "aba"
 4511  
      *  StringUtils.replaceEach("aba", new String[]{"a"}, null, *) = "aba"
 4512  
      *  StringUtils.replaceEach("aba", new String[]{"a"}, new String[]{""}, *) = "b"
 4513  
      *  StringUtils.replaceEach("aba", new String[]{null}, new String[]{"a"}, *) = "aba"
 4514  
      *  StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"w", "t"}, *) = "wcte"
 4515  
      *  (example of how it repeats)
 4516  
      *  StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"}, false) = "dcte"
 4517  
      *  StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"}, true) = "tcte"
 4518  
      *  StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "ab"}, *) = IllegalStateException
 4519  
      * </pre>
 4520  
      *
 4521  
      * @param text
 4522  
      *            text to search and replace in, no-op if null
 4523  
      * @param searchList
 4524  
      *            the Strings to search for, no-op if null
 4525  
      * @param replacementList
 4526  
      *            the Strings to replace them with, no-op if null
 4527  
      * @param repeat if true, then replace repeatedly
 4528  
      *       until there are no more possible replacements or timeToLive < 0
 4529  
      * @param timeToLive
 4530  
      *            if less than 0 then there is a circular reference and endless
 4531  
      *            loop
 4532  
      * @return the text with any replacements processed, {@code null} if
 4533  
      *         null String input
 4534  
      * @throws IllegalStateException
 4535  
      *             if the search is repeating and there is an endless loop due
 4536  
      *             to outputs of one being inputs to another
 4537  
      * @throws IllegalArgumentException
 4538  
      *             if the lengths of the arrays are not the same (null is ok,
 4539  
      *             and/or size 0)
 4540  
      * @since 2.4
 4541  
      */
 4542  
     private static String replaceEach(
 4543  
             final String text, final String[] searchList, final String[] replacementList, final boolean repeat, final int timeToLive) {
 4544  
 
 4545  
         // mchyzer Performance note: This creates very few new objects (one major goal)
 4546  
         // let me know if there are performance requests, we can create a harness to measure
 4547  
 
 4548  32
         if (text == null || text.length() == 0 || searchList == null ||
 4549  
                 searchList.length == 0 || replacementList == null || replacementList.length == 0) {
 4550  12
             return text;
 4551  
         }
 4552  
 
 4553  
         // if recursing, this shouldn't be less than 0
 4554  20
         if (timeToLive < 0) {
 4555  1
             throw new IllegalStateException("Aborting to protect against StackOverflowError - " +
 4556  
                                             "output of one loop is the input of another");
 4557  
         }
 4558  
 
 4559  19
         final int searchLength = searchList.length;
 4560  19
         final int replacementLength = replacementList.length;
 4561  
 
 4562  
         // make sure lengths are ok, these need to be equal
 4563  19
         if (searchLength != replacementLength) {
 4564  0
             throw new IllegalArgumentException("Search and Replace array lengths don't match: "
 4565  
                 + searchLength
 4566  
                 + " vs "
 4567  
                 + replacementLength);
 4568  
         }
 4569  
 
 4570  
         // keep track of which still have matches
 4571  19
         final boolean[] noMoreMatchesForReplIndex = new boolean[searchLength];
 4572  
 
 4573  
         // index on index that the match was found
 4574  19
         int textIndex = -1;
 4575  19
         int replaceIndex = -1;
 4576  19
         int tempIndex = -1;
 4577  
 
 4578  
         // index of replace array that will replace the search string found
 4579  
         // NOTE: logic duplicated below START
 4580  110
         for (int i = 0; i < searchLength; i++) {
 4581  91
             if (noMoreMatchesForReplIndex[i] || searchList[i] == null ||
 4582  
                     searchList[i].length() == 0 || replacementList[i] == null) {
 4583  2
                 continue;
 4584  
             }
 4585  87
             tempIndex = text.indexOf(searchList[i]);
 4586  
 
 4587  
             // see if we need to keep searching for this
 4588  87
             if (tempIndex == -1) {
 4589  59
                 noMoreMatchesForReplIndex[i] = true;
 4590  
             } else {
 4591  28
                 if (textIndex == -1 || tempIndex < textIndex) {
 4592  14
                     textIndex = tempIndex;
 4593  14
                     replaceIndex = i;
 4594  
                 }
 4595  
             }
 4596  
         }
 4597  
         // NOTE: logic mostly below END
 4598  
 
 4599  
         // no search strings found, we are done
 4600  19
         if (textIndex == -1) {
 4601  6
             return text;
 4602  
         }
 4603  
 
 4604  13
         int start = 0;
 4605  
 
 4606  
         // get a good guess on the size of the result buffer so it doesn't have to double if it goes over a bit
 4607  13
         int increase = 0;
 4608  
 
 4609  
         // count the replacement text elements that are larger than their corresponding text being replaced
 4610  96
         for (int i = 0; i < searchList.length; i++) {
 4611  83
             if (searchList[i] == null || replacementList[i] == null) {
 4612  1
                 continue;
 4613  
             }
 4614  82
             final int greater = replacementList[i].length() - searchList[i].length();
 4615  82
             if (greater > 0) {
 4616  3
                 increase += 3 * greater; // assume 3 matches
 4617  
             }
 4618  
         }
 4619  
         // have upper-bound at 20% increase, then let Java take over
 4620  13
         increase = Math.min(increase, text.length() / 5);
 4621  
 
 4622  13
         final StringBuilder buf = new StringBuilder(text.length() + increase);
 4623  
 
 4624  46
         while (textIndex != -1) {
 4625  
 
 4626  45
             for (int i = start; i < textIndex; i++) {
 4627  12
                 buf.append(text.charAt(i));
 4628  
             }
 4629  33
             buf.append(replacementList[replaceIndex]);
 4630  
 
 4631  33
             start = textIndex + searchList[replaceIndex].length();
 4632  
 
 4633  33
             textIndex = -1;
 4634  33
             replaceIndex = -1;
 4635  33
             tempIndex = -1;
 4636  
             // find the next earliest match
 4637  
             // NOTE: logic mostly duplicated above START
 4638  685
             for (int i = 0; i < searchLength; i++) {
 4639  652
                 if (noMoreMatchesForReplIndex[i] || searchList[i] == null ||
 4640  
                         searchList[i].length() == 0 || replacementList[i] == null) {
 4641  2
                     continue;
 4642  
                 }
 4643  81
                 tempIndex = text.indexOf(searchList[i], start);
 4644  
 
 4645  
                 // see if we need to keep searching for this
 4646  81
                 if (tempIndex == -1) {
 4647  28
                     noMoreMatchesForReplIndex[i] = true;
 4648  
                 } else {
 4649  53
                     if (textIndex == -1 || tempIndex < textIndex) {
 4650  34
                         textIndex = tempIndex;
 4651  34
                         replaceIndex = i;
 4652  
                     }
 4653  
                 }
 4654  
             }
 4655  
             // NOTE: logic duplicated above END
 4656  
 
 4657  
         }
 4658  13
         final int textLength = text.length();
 4659  24
         for (int i = start; i < textLength; i++) {
 4660  11
             buf.append(text.charAt(i));
 4661  
         }
 4662  13
         final String result = buf.toString();
 4663  13
         if (!repeat) {
 4664  6
             return result;
 4665  
         }
 4666  
 
 4667  7
         return replaceEach(result, searchList, replacementList, repeat, timeToLive - 1);
 4668  
     }
 4669  
 
 4670  
     // Replace, character based
 4671  
     //-----------------------------------------------------------------------
 4672  
     /**
 4673  
      * <p>Replaces all occurrences of a character in a String with another.
 4674  
      * This is a null-safe version of {@link String#replace(char, char)}.</p>
 4675  
      *
 4676  
      * <p>A {@code null} string input returns {@code null}.
 4677  
      * An empty ("") string input returns an empty string.</p>
 4678  
      *
 4679  
      * <pre>
 4680  
      * StringUtils.replaceChars(null, *, *)        = null
 4681  
      * StringUtils.replaceChars("", *, *)          = ""
 4682  
      * StringUtils.replaceChars("abcba", 'b', 'y') = "aycya"
 4683  
      * StringUtils.replaceChars("abcba", 'z', 'y') = "abcba"
 4684  
      * </pre>
 4685  
      *
 4686  
      * @param str  String to replace characters in, may be null
 4687  
      * @param searchChar  the character to search for, may be null
 4688  
      * @param replaceChar  the character to replace, may be null
 4689  
      * @return modified String, {@code null} if null string input
 4690  
      * @since 2.0
 4691  
      */
 4692  
     public static String replaceChars(final String str, final char searchChar, final char replaceChar) {
 4693  6
         if (str == null) {
 4694  1
             return null;
 4695  
         }
 4696  5
         return str.replace(searchChar, replaceChar);
 4697  
     }
 4698  
 
 4699  
     /**
 4700  
      * <p>Replaces multiple characters in a String in one go.
 4701  
      * This method can also be used to delete characters.</p>
 4702  
      *
 4703  
      * <p>For example:<br />
 4704  
      * <code>replaceChars(&quot;hello&quot;, &quot;ho&quot;, &quot;jy&quot;) = jelly</code>.</p>
 4705  
      *
 4706  
      * <p>A {@code null} string input returns {@code null}.
 4707  
      * An empty ("") string input returns an empty string.
 4708  
      * A null or empty set of search characters returns the input string.</p>
 4709  
      *
 4710  
      * <p>The length of the search characters should normally equal the length
 4711  
      * of the replace characters.
 4712  
      * If the search characters is longer, then the extra search characters
 4713  
      * are deleted.
 4714  
      * If the search characters is shorter, then the extra replace characters
 4715  
      * are ignored.</p>
 4716  
      *
 4717  
      * <pre>
 4718  
      * StringUtils.replaceChars(null, *, *)           = null
 4719  
      * StringUtils.replaceChars("", *, *)             = ""
 4720  
      * StringUtils.replaceChars("abc", null, *)       = "abc"
 4721  
      * StringUtils.replaceChars("abc", "", *)         = "abc"
 4722  
      * StringUtils.replaceChars("abc", "b", null)     = "ac"
 4723  
      * StringUtils.replaceChars("abc", "b", "")       = "ac"
 4724  
      * StringUtils.replaceChars("abcba", "bc", "yz")  = "ayzya"
 4725  
      * StringUtils.replaceChars("abcba", "bc", "y")   = "ayya"
 4726  
      * StringUtils.replaceChars("abcba", "bc", "yzx") = "ayzya"
 4727  
      * </pre>
 4728  
      *
 4729  
      * @param str  String to replace characters in, may be null
 4730  
      * @param searchChars  a set of characters to search for, may be null
 4731  
      * @param replaceChars  a set of characters to replace, may be null
 4732  
      * @return modified String, {@code null} if null string input
 4733  
      * @since 2.0
 4734  
      */
 4735  
     public static String replaceChars(final String str, final String searchChars, String replaceChars) {
 4736  30
         if (isEmpty(str) || isEmpty(searchChars)) {
 4737  16
             return str;
 4738  
         }
 4739  14
         if (replaceChars == null) {
 4740  1
             replaceChars = EMPTY;
 4741  
         }
 4742  14
         boolean modified = false;
 4743  14
         final int replaceCharsLength = replaceChars.length();
 4744  14
         final int strLength = str.length();
 4745  14
         final StringBuilder buf = new StringBuilder(strLength);
 4746  83
         for (int i = 0; i < strLength; i++) {
 4747  69
             final char ch = str.charAt(i);
 4748  69
             final int index = searchChars.indexOf(ch);
 4749  69
             if (index >= 0) {
 4750  35
                 modified = true;
 4751  35
                 if (index < replaceCharsLength) {
 4752  31
                     buf.append(replaceChars.charAt(index));
 4753  
                 }
 4754  
             } else {
 4755  34
                 buf.append(ch);
 4756  
             }
 4757  
         }
 4758  14
         if (modified) {
 4759  12
             return buf.toString();
 4760  
         }
 4761  2
         return str;
 4762  
     }
 4763  
 
 4764  
     // Overlay
 4765  
     //-----------------------------------------------------------------------
 4766  
     /**
 4767  
      * <p>Overlays part of a String with another String.</p>
 4768  
      *
 4769  
      * <p>A {@code null} string input returns {@code null}.
 4770  
      * A negative index is treated as zero.
 4771  
      * An index greater than the string length is treated as the string length.
 4772  
      * The start index is always the smaller of the two indices.</p>
 4773  
      *
 4774  
      * <pre>
 4775  
      * StringUtils.overlay(null, *, *, *)            = null
 4776  
      * StringUtils.overlay("", "abc", 0, 0)          = "abc"
 4777  
      * StringUtils.overlay("abcdef", null, 2, 4)     = "abef"
 4778  
      * StringUtils.overlay("abcdef", "", 2, 4)       = "abef"
 4779  
      * StringUtils.overlay("abcdef", "", 4, 2)       = "abef"
 4780  
      * StringUtils.overlay("abcdef", "zzzz", 2, 4)   = "abzzzzef"
 4781  
      * StringUtils.overlay("abcdef", "zzzz", 4, 2)   = "abzzzzef"
 4782  
      * StringUtils.overlay("abcdef", "zzzz", -1, 4)  = "zzzzef"
 4783  
      * StringUtils.overlay("abcdef", "zzzz", 2, 8)   = "abzzzz"
 4784  
      * StringUtils.overlay("abcdef", "zzzz", -2, -3) = "zzzzabcdef"
 4785  
      * StringUtils.overlay("abcdef", "zzzz", 8, 10)  = "abcdefzzzz"
 4786  
      * </pre>
 4787  
      *
 4788  
      * @param str  the String to do overlaying in, may be null
 4789  
      * @param overlay  the String to overlay, may be null
 4790  
      * @param start  the position to start overlaying at
 4791  
      * @param end  the position to stop overlaying before
 4792  
      * @return overlayed String, {@code null} if null String input
 4793  
      * @since 2.0
 4794  
      */
 4795  
     public static String overlay(final String str, String overlay, int start, int end) {
 4796  21
         if (str == null) {
 4797  2
             return null;
 4798  
         }
 4799  19
         if (overlay == null) {
 4800  3
             overlay = EMPTY;
 4801  
         }
 4802  19
         final int len = str.length();
 4803  19
         if (start < 0) {
 4804  4
             start = 0;
 4805  
         }
 4806  19
         if (start > len) {
 4807  4
             start = len;
 4808  
         }
 4809  19
         if (end < 0) {
 4810  4
             end = 0;
 4811  
         }
 4812  19
         if (end > len) {
 4813  4
             end = len;
 4814  
         }
 4815  19
         if (start > end) {
 4816  5
             final int temp = start;
 4817  5
             start = end;
 4818  5
             end = temp;
 4819  
         }
 4820  19
         return new StringBuilder(len + start - end + overlay.length() + 1)
 4821  
             .append(str.substring(0, start))
 4822  
             .append(overlay)
 4823  
             .append(str.substring(end))
 4824  
             .toString();
 4825  
     }
 4826  
 
 4827  
     // Chomping
 4828  
     //-----------------------------------------------------------------------
 4829  
     /**
 4830  
      * <p>Removes one newline from end of a String if it's there,
 4831  
      * otherwise leave it alone.  A newline is &quot;{@code \n}&quot;,
 4832  
      * &quot;{@code \r}&quot;, or &quot;{@code \r\n}&quot;.</p>
 4833  
      *
 4834  
      * <p>NOTE: This method changed in 2.0.
 4835  
      * It now more closely matches Perl chomp.</p>
 4836  
      *
 4837  
      * <pre>
 4838  
      * StringUtils.chomp(null)          = null
 4839  
      * StringUtils.chomp("")            = ""
 4840  
      * StringUtils.chomp("abc \r")      = "abc "
 4841  
      * StringUtils.chomp("abc\n")       = "abc"
 4842  
      * StringUtils.chomp("abc\r\n")     = "abc"
 4843  
      * StringUtils.chomp("abc\r\n\r\n") = "abc\r\n"
 4844  
      * StringUtils.chomp("abc\n\r")     = "abc\n"
 4845  
      * StringUtils.chomp("abc\n\rabc")  = "abc\n\rabc"
 4846  
      * StringUtils.chomp("\r")          = ""
 4847  
      * StringUtils.chomp("\n")          = ""
 4848  
      * StringUtils.chomp("\r\n")        = ""
 4849  
      * </pre>
 4850  
      *
 4851  
      * @param str  the String to chomp a newline from, may be null
 4852  
      * @return String without newline, {@code null} if null String input
 4853  
      */
 4854  
     public static String chomp(final String str) {
 4855  16
         if (isEmpty(str)) {
 4856  2
             return str;
 4857  
         }
 4858  
 
 4859  14
         if (str.length() == 1) {
 4860  3
             final char ch = str.charAt(0);
 4861  3
             if (ch == CharUtils.CR || ch == CharUtils.LF) {
 4862  2
                 return EMPTY;
 4863  
             }
 4864  1
             return str;
 4865  
         }
 4866  
 
 4867  11
         int lastIdx = str.length() - 1;
 4868  11
         final char last = str.charAt(lastIdx);
 4869  
 
 4870  11
         if (last == CharUtils.LF) {
 4871  5
             if (str.charAt(lastIdx - 1) == CharUtils.CR) {
 4872  3
                 lastIdx--;
 4873  
             }
 4874  6
         } else if (last != CharUtils.CR) {
 4875  3
             lastIdx++;
 4876  
         }
 4877  11
         return str.substring(0, lastIdx);
 4878  
     }
 4879  
 
 4880  
     /**
 4881  
      * <p>Removes {@code separator} from the end of
 4882  
      * {@code str} if it's there, otherwise leave it alone.</p>
 4883  
      *
 4884  
      * <p>NOTE: This method changed in version 2.0.
 4885  
      * It now more closely matches Perl chomp.
 4886  
      * For the previous behavior, use {@link #substringBeforeLast(String, String)}.
 4887  
      * This method uses {@link String#endsWith(String)}.</p>
 4888  
      *
 4889  
      * <pre>
 4890</