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