Package com.helger.base.string
Class StringReplace
java.lang.Object
com.helger.base.string.StringReplace
-
Method Summary
Modifier and TypeMethodDescriptionstatic intgetReplaceMultipleResultLength(char[] aInputString, char[] aSearchChars, char[][] aReplacementStrings) Get the result length (in characters) when replacing all patterns with the replacements on the passed input array.static StringreplaceAll(String sInputString, char cSearchChar, char cReplacementChar) This is a fast replacement forString.replace(char, char)for characters.static StringreplaceAll(String sInputString, String sSearchText, CharSequence aReplacementText) This is a fast replacement forString.replace(CharSequence, CharSequence).static StringreplaceAllRepeatedly(String sInputString, String sSearchText, String sReplacementText) Just callsreplaceAllas long as there are still replacements foundstatic StringreplaceAllSafe(String sInputString, String sSearchText, CharSequence aReplacementText) Same asreplaceAll(String, String, CharSequence)but allowing for anullnew-value, which is than interpreted as an empty string instead.static char[]replaceMultiple(char[] aInput, char[] aSearchChars, char[][] aReplacementStrings) Optimized replace method that replaces a set of characters with a set of strings.static char[]replaceMultiple(String sInputString, char[] aSearchChars, char cReplacementChar) Optimized replace method that replaces a set of characters with another character.static char[]replaceMultiple(String sInputString, char[] aSearchChars, char[][] aReplacementStrings) Optimized replace method that replaces a set of characters with a set of strings.static StringreplaceMultiple(String sInputString, String[] aSearchTexts, String[] aReplacementTexts) Perform all string replacements on the input string as defined by the passed map.static StringreplaceMultiple(String sInputString, Map<String, String> aTransTable) Perform all string replacements on the input string as defined by the passed map.static StringreplaceMultipleAsString(String sInputString, char[] aSearchChars, char cReplacementChar) Optimized replace method that replaces a set of characters with another character.static intreplaceMultipleTo(char[] aInput, char[] aSearchChars, char[][] aReplacementStrings, 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[] aInput, int nOfs, int nLen, char[] aSearchChars, char[][] aReplacementStrings, 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(String sInputString, char[] aSearchChars, char[][] aReplacementStrings, 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(String sInputString, char[] aSearchChars, char cReplacementChar, Writer aTarget) Optimized replace method that replaces a set of characters with another character.static voidreplaceMultipleTo(String sInputString, char[] aSearchChars, char cReplacementChar, 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
@Nullable public static 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
@Nullable public static 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
@Nullable public static 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(@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 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
@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 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(@Nullable char[] aInput, @Nonnull char[] aSearchChars, @Nonnull char[][] 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(@Nullable char[] aInput, @Nonnegative int nOfs, @Nonnegative int nLen, @Nonnull char[] aSearchChars, @Nonnull char[][] 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
@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 benull.
-
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 benull.- 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 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
@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 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.
-