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