Package com.helger.base.string
Class StringReplace
java.lang.Object
com.helger.base.string.StringReplace
Helper class for replacing characters and substrings within strings, with
support for multiple replacements and streaming output.
- Author:
- Philip Helger
-
Method Summary
Modifier and TypeMethodDescriptionstatic intgetReplaceMultipleResultLength(char @NonNull [] aInputString, @com.helger.annotation.Nonempty char @NonNull [] aSearchChars, @com.helger.annotation.Nonempty char[] @NonNull [] aReplacementStrings) Get the result length (in characters) when replacing all patterns with the replacements on the passed input array.static @Nullable StringreplaceAll(@Nullable String sInputString, char cSearchChar, char cReplacementChar) This is a fast replacement forString.replace(char, char)for characters.static @Nullable StringreplaceAll(@Nullable String sInputString, @NonNull String sSearchText, @NonNull CharSequence aReplacementText) This is a fast replacement forString.replace(CharSequence, CharSequence).static @Nullable StringreplaceAllRepeatedly(@Nullable String sInputString, @NonNull String sSearchText, @NonNull String sReplacementText) Just callsreplaceAllas long as there are still replacements foundstatic StringreplaceAllSafe(@Nullable String sInputString, @NonNull String sSearchText, @Nullable CharSequence aReplacementText) Same asreplaceAll(String, String, CharSequence)but allowing for anullnew-value, which is than interpreted as an empty string instead.static char @NonNull []replaceMultiple(char @Nullable [] aInput, char @NonNull [] aSearchChars, char[] @NonNull [] aReplacementStrings) Optimized replace method that replaces a set of characters with a set of strings.static char @NonNull []replaceMultiple(@Nullable String sInputString, char @NonNull [] aSearchChars, char cReplacementChar) Optimized replace method that replaces a set of characters with another character.static char @NonNull []replaceMultiple(@Nullable String sInputString, char @NonNull [] aSearchChars, char[] @NonNull [] aReplacementStrings) Optimized replace method that replaces a set of characters with a set of strings.static @Nullable StringreplaceMultiple(@Nullable String sInputString, @Nullable String[] aSearchTexts, @Nullable String[] aReplacementTexts) Perform all string replacements on the input string as defined by the passed map.static @Nullable StringreplaceMultiple(@Nullable String sInputString, @Nullable Map<String, String> aTransTable) Perform all string replacements on the input string as defined by the passed map.static @NonNull StringreplaceMultipleAsString(@Nullable String sInputString, char @NonNull [] aSearchChars, char cReplacementChar) Optimized replace method that replaces a set of characters with another character.static intreplaceMultipleTo(char @Nullable [] aInput, char @NonNull [] aSearchChars, char[] @NonNull [] aReplacementStrings, @NonNull Writer aTarget) Specialized version ofreplaceMultiple(String, char[], char[][])where the object where the output should be appended is passed in as a parameter.static intreplaceMultipleTo(char @Nullable [] aInput, int nOfs, int nLen, char @NonNull [] aSearchChars, char[] @NonNull [] aReplacementStrings, @NonNull Writer aTarget) Specialized version ofreplaceMultiple(String, char[], char[][])where the object where the output should be appended is passed in as a parameter.static intreplaceMultipleTo(@Nullable String sInputString, char @NonNull [] aSearchChars, char[] @NonNull [] aReplacementStrings, @NonNull Writer aTarget) Specialized version ofreplaceMultiple(String, char[], char[][])where the object where the output should be appended is passed in as a parameter.static voidreplaceMultipleTo(@Nullable String sInputString, char @NonNull [] aSearchChars, char cReplacementChar, @NonNull Writer aTarget) Optimized replace method that replaces a set of characters with another character.static voidreplaceMultipleTo(@Nullable String sInputString, char @NonNull [] aSearchChars, char cReplacementChar, @NonNull StringBuilder aTarget) Optimized replace method that replaces a set of characters with another character.
-
Method Details
-
replaceAllSafe
public static String replaceAllSafe(@Nullable String sInputString, @NonNull String sSearchText, @Nullable CharSequence aReplacementText) Same asreplaceAll(String, String, CharSequence)but allowing for anullnew-value, which is than interpreted as an empty string instead.- Parameters:
sInputString- The input string where the text should be replace. If this parameter isnullor empty, no replacement is done.sSearchText- The string to be replaced. May neither benullnor empty.aReplacementText- The string with the replacement. May benullor empty.- Returns:
- The input string as is, if the input string is empty or if the string to be replaced is not contained.
-
replaceAll
public static @Nullable String replaceAll(@Nullable String sInputString, @NonNull String sSearchText, @NonNull CharSequence aReplacementText) This is a fast replacement forString.replace(CharSequence, CharSequence). The problem with the mentionedStringmethod is, that is uses internally regular expressions which use a synchronized block to compile the patterns. This method is inherently thread safe sinceStringis immutable and we're operating on different temporaryStringBuilderobjects.- Parameters:
sInputString- The input string where the text should be replace. If this parameter isnullor empty, no replacement is done.sSearchText- The string to be replaced. May neither benullnor empty.aReplacementText- The string with the replacement. May not benullbut 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
public static @Nullable String replaceAll(@Nullable String sInputString, char cSearchChar, char cReplacementChar) This is a fast replacement forString.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 sinceStringis immutable and we're operating on different temporaryStringBuilderobjects.- Parameters:
sInputString- The input string where the text should be replace. If this parameter isnullor 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
public static @Nullable String replaceAllRepeatedly(@Nullable String sInputString, @NonNull String sSearchText, @NonNull String sReplacementText) Just callsreplaceAllas long as there are still replacements found- Parameters:
sInputString- The input string where the text should be replace. If this parameter isnullor empty, no replacement is done.sSearchText- The string to be replaced. May neither benullnor empty.sReplacementText- The string with the replacement. May not benullbut 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(char @NonNull [] aInputString, @Nonempty @com.helger.annotation.Nonempty char @NonNull [] aSearchChars, @Nonempty @com.helger.annotation.Nonempty char[] @NonNull [] 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 benull.aSearchChars- The one-character search patterns. May not benull.aReplacementStrings- The replacements to be performed. May not benull. The first dimension of this array must have exactly the same amount of elements as the patterns parameter array.- Returns:
CGlobal.ILLEGAL_UINTif no replacement was needed, and therefore the length of the input array could be used.
-
replaceMultiple
public static char @NonNull [] replaceMultiple(@Nullable String sInputString, char @NonNull [] aSearchChars, char[] @NonNull [] 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
public static char @NonNull [] replaceMultiple(char @Nullable [] aInput, char @NonNull [] aSearchChars, char[] @NonNull [] 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, char @NonNull [] aSearchChars, char[] @NonNull [] aReplacementStrings, @NonNull Writer aTarget) throws IOException Specialized version ofreplaceMultiple(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 benull.- Returns:
- The number of replacements performed. Always ≥ 0.
- Throws:
IOException- In case writing to the Writer fails
-
replaceMultipleTo
@Nonnegative public static int replaceMultipleTo(char @Nullable [] aInput, char @NonNull [] aSearchChars, char[] @NonNull [] aReplacementStrings, @NonNull Writer aTarget) throws IOException Specialized version ofreplaceMultiple(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 benull.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 benull.- Returns:
- The number of replacements performed. Always ≥ 0.
- Throws:
IOException- In case writing to the Writer fails
-
replaceMultipleTo
@Nonnegative public static int replaceMultipleTo(char @Nullable [] aInput, @Nonnegative int nOfs, @Nonnegative int nLen, char @NonNull [] aSearchChars, char[] @NonNull [] aReplacementStrings, @NonNull Writer aTarget) throws IOException Specialized version ofreplaceMultiple(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 benull.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 benull.- Returns:
- The number of replacements performed. Always ≥ 0.
- Throws:
IOException- In case writing to the Writer fails
-
replaceMultiple
public static char @NonNull [] replaceMultiple(@Nullable String sInputString, char @NonNull [] 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, char @NonNull [] 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 benull.
-
replaceMultipleTo
public static void replaceMultipleTo(@Nullable String sInputString, char @NonNull [] 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 benull.- Throws:
IOException- in case writing to the Writer fails- Since:
- 8.6.3
-
replaceMultipleAsString
public static @NonNull String replaceMultipleAsString(@Nullable String sInputString, char @NonNull [] 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
public static @Nullable 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 usingreplaceAll(String,String,CharSequence)which is ok.- Parameters:
sInputString- The input string where the text should be replaced. May benull.aTransTable- The map with the replacements to execute. Ifnullis passed, the input string is not altered.- Returns:
nullif the input string wasnull.
-
replaceMultiple
public static @Nullable 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 usingreplaceAll(String,String,CharSequence)which is ok.- Parameters:
sInputString- The input string where the text should be replaced. May benull.aSearchTexts- The texts to be searched. Ifnullis 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! Ifnullis passed, the input string is not altered.- Returns:
nullif the input string wasnull. The unmodified input string if no search/replace patterns where provided.
-