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