Class StringReplace

java.lang.Object
com.helger.base.string.StringReplace

@Immutable public final class StringReplace extends Object
  • Method Details

    • replaceAllSafe

      public static String replaceAllSafe(@Nullable String sInputString, @Nonnull String sSearchText, @Nullable CharSequence aReplacementText)
      Same as replaceAll(String, String, CharSequence) but allowing for a null new-value, which is than interpreted as an empty string instead.
      Parameters:
      sInputString - The input string where the text should be replace. If this parameter is null or empty, no replacement is done.
      sSearchText - The string to be replaced. May neither be null nor empty.
      aReplacementText - The string with the replacement. May be null or empty.
      Returns:
      The input string as is, if the input string is empty or if the string to be replaced is not contained.
    • replaceAll

      @Nullable public static String replaceAll(@Nullable String sInputString, @Nonnull String sSearchText, @Nonnull CharSequence aReplacementText)
      This is a fast replacement for String.replace(CharSequence, CharSequence). The problem with the mentioned String method is, that is uses internally regular expressions which use a synchronized block to compile the patterns. This method is inherently thread safe since String is immutable and we're operating on different temporary StringBuilder objects.
      Parameters:
      sInputString - The input string where the text should be replace. If this parameter is null or empty, no replacement is done.
      sSearchText - The string to be replaced. May neither be null nor empty.
      aReplacementText - The string with the replacement. May not be null but may be empty.
      Returns:
      The input string as is, if the input string is empty or if the search pattern and the replacement are equal or if the string to be replaced is not contained.
    • replaceAll

      @Nullable public static String replaceAll(@Nullable String sInputString, char cSearchChar, char cReplacementChar)
      This is a fast replacement for String.replace(char, char) for characters. The problem with the mentioned String method is, that is uses internally regular expressions which use a synchronized block to compile the patterns. This method is inherently thread safe since String is immutable and we're operating on different temporary StringBuilder objects.
      Parameters:
      sInputString - The input string where the text should be replace. If this parameter is null or empty, no replacement is done.
      cSearchChar - The character to be replaced.
      cReplacementChar - The character with the replacement.
      Returns:
      The input string as is, if the input string is empty or if the search pattern and the replacement are equal or if the string to be replaced is not contained.
    • replaceAllRepeatedly

      @Nullable public static String replaceAllRepeatedly(@Nullable String sInputString, @Nonnull String sSearchText, @Nonnull String sReplacementText)
      Just calls replaceAll as long as there are still replacements found
      Parameters:
      sInputString - The input string where the text should be replace. If this parameter is null or empty, no replacement is done.
      sSearchText - The string to be replaced. May neither be null nor empty.
      sReplacementText - The string with the replacement. May not be null but may be empty.
      Returns:
      The input string as is, if the input string is empty or if the string to be replaced is not contained.
    • getReplaceMultipleResultLength

      public static int getReplaceMultipleResultLength(@Nonnull char[] aInputString, @Nonnull @Nonempty char[] aSearchChars, @Nonnull @Nonempty char[][] aReplacementStrings)
      Get the result length (in characters) when replacing all patterns with the replacements on the passed input array.
      Parameters:
      aInputString - Input char array. May not be null.
      aSearchChars - The one-character search patterns. May not be null.
      aReplacementStrings - The replacements to be performed. May not be null. The first dimension of this array must have exactly the same amount of elements as the patterns parameter array.
      Returns:
      CGlobal.ILLEGAL_UINT if no replacement was needed, and therefore the length of the input array could be used.
    • replaceMultiple

      @Nonnull public static char[] replaceMultiple(@Nullable String sInputString, @Nonnull char[] aSearchChars, @Nonnull char[][] aReplacementStrings)
      Optimized replace method that replaces a set of characters with a set of strings. This method was created for efficient XML special character replacements!
      Parameters:
      sInputString - The input string.
      aSearchChars - The characters to replace.
      aReplacementStrings - The new strings to be inserted instead. Must have the same array length as aPatterns.
      Returns:
      The replaced version of the string or an empty char array if the input string was null.
    • replaceMultiple

      @Nonnull public static char[] replaceMultiple(@Nullable char[] aInput, @Nonnull char[] aSearchChars, @Nonnull char[][] aReplacementStrings)
      Optimized replace method that replaces a set of characters with a set of strings. This method was created for efficient XML special character replacements!
      Parameters:
      aInput - The input string.
      aSearchChars - The characters to replace.
      aReplacementStrings - The new strings to be inserted instead. Must have the same array length as aPatterns.
      Returns:
      The replaced version of the string or an empty char array if the input string was null.
    • replaceMultipleTo

      @Nonnegative public static int replaceMultipleTo(@Nullable String sInputString, @Nonnull char[] aSearchChars, @Nonnull char[][] aReplacementStrings, @Nonnull Writer aTarget) throws IOException
      Specialized version of replaceMultiple(String, char[], char[][]) where the object where the output should be appended is passed in as a parameter. This has the advantage, that not length calculation needs to take place!
      Parameters:
      sInputString - The input string.
      aSearchChars - The characters to replace.
      aReplacementStrings - The new strings to be inserted instead. Must have the same array length as aPatterns.
      aTarget - Where the replaced objects should be written to. May not be null.
      Returns:
      The number of replacements performed. Always ≥ 0.
      Throws:
      IOException - In case writing to the Writer fails
    • replaceMultipleTo

      @Nonnegative public static int replaceMultipleTo(@Nullable char[] aInput, @Nonnull char[] aSearchChars, @Nonnull char[][] aReplacementStrings, @Nonnull Writer aTarget) throws IOException
      Specialized version of replaceMultiple(String, char[], char[][]) where the object where the output should be appended is passed in as a parameter. This has the advantage, that not length calculation needs to take place!
      Parameters:
      aInput - The input char array. May not be null.
      aSearchChars - The characters to replace.
      aReplacementStrings - The new strings to be inserted instead. Must have the same array length as aPatterns.
      aTarget - Where the replaced objects should be written to. May not be null.
      Returns:
      The number of replacements performed. Always ≥ 0.
      Throws:
      IOException - In case writing to the Writer fails
    • replaceMultipleTo

      @Nonnegative public static int replaceMultipleTo(@Nullable char[] aInput, @Nonnegative int nOfs, @Nonnegative int nLen, @Nonnull char[] aSearchChars, @Nonnull char[][] aReplacementStrings, @Nonnull Writer aTarget) throws IOException
      Specialized version of replaceMultiple(String, char[], char[][]) where the object where the output should be appended is passed in as a parameter. This has the advantage, that not length calculation needs to take place!
      Parameters:
      aInput - The input char array. May be null.
      nOfs - Offset into input array. Must be ≥ 0.
      nLen - Number of characters from input array. Must be ≥ 0.
      aSearchChars - The characters to replace.
      aReplacementStrings - The new strings to be inserted instead. Must have the same array length as aPatterns.
      aTarget - Where the replaced objects should be written to. May not be null.
      Returns:
      The number of replacements performed. Always ≥ 0.
      Throws:
      IOException - In case writing to the Writer fails
    • replaceMultiple

      @Nonnull public static char[] replaceMultiple(@Nullable String sInputString, @Nonnull char[] aSearchChars, char cReplacementChar)
      Optimized replace method that replaces a set of characters with another character. This method was created for efficient unsafe character replacements!
      Parameters:
      sInputString - The input string.
      aSearchChars - The characters to replace.
      cReplacementChar - The new char to be used instead of the search chars.
      Returns:
      The replaced version of the string or an empty char array if the input string was null.
    • replaceMultipleTo

      public static void replaceMultipleTo(@Nullable String sInputString, @Nonnull char[] aSearchChars, char cReplacementChar, @Nonnull StringBuilder aTarget)
      Optimized replace method that replaces a set of characters with another character. This method was created for efficient unsafe character replacements!
      Parameters:
      sInputString - The input string.
      aSearchChars - The characters to replace.
      cReplacementChar - The new char to be used instead of the search chars.
      aTarget - The target StringBuilder to write the result to. May not be null.
    • replaceMultipleTo

      public static void replaceMultipleTo(@Nullable String sInputString, @Nonnull char[] aSearchChars, char cReplacementChar, @Nonnull Writer aTarget) throws IOException
      Optimized replace method that replaces a set of characters with another character. This method was created for efficient unsafe character replacements!
      Parameters:
      sInputString - The input string.
      aSearchChars - The characters to replace.
      cReplacementChar - The new char to be used instead of the search chars.
      aTarget - The target writer to write the result to. May not be null.
      Throws:
      IOException - in case writing to the Writer fails
      Since:
      8.6.3
    • replaceMultipleAsString

      @Nonnull public static String replaceMultipleAsString(@Nullable String sInputString, @Nonnull char[] aSearchChars, char cReplacementChar)
      Optimized replace method that replaces a set of characters with another character. This method was created for efficient unsafe character replacements!
      Parameters:
      sInputString - The input string.
      aSearchChars - The characters to replace.
      cReplacementChar - The new char to be used instead of the search chars.
      Returns:
      The replaced version of the string or an empty char array if the input string was null.
      Since:
      8.6.3
    • replaceMultiple

      @Nullable public static String replaceMultiple(@Nullable String sInputString, @Nullable Map<String,String> aTransTable)
      Perform all string replacements on the input string as defined by the passed map. All replacements are done using replaceAll(String,String,CharSequence) which is ok.
      Parameters:
      sInputString - The input string where the text should be replaced. May be null.
      aTransTable - The map with the replacements to execute. If null is passed, the input string is not altered.
      Returns:
      null if the input string was null.
    • replaceMultiple

      @Nullable public static String replaceMultiple(@Nullable String sInputString, @Nullable String[] aSearchTexts, @Nullable String[] aReplacementTexts)
      Perform all string replacements on the input string as defined by the passed map. All replacements are done using replaceAll(String,String,CharSequence) which is ok.
      Parameters:
      sInputString - The input string where the text should be replaced. May be null.
      aSearchTexts - The texts to be searched. If null is passed, the input string is not altered.
      aReplacementTexts - The texts to be used as the replacements. This array must have exactly the same number of elements than the searched texts! If null is passed, the input string is not altered.
      Returns:
      null if the input string was null. The unmodified input string if no search/replace patterns where provided.