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