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