# Class StringBuilders

Object
Static
StringBuilders

public final class StringBuilders extends Static
Static methods working on `StringBuilder` instances. Some methods defined in this class duplicate the functionalities provided in the `CharSequences` class, but modify directly the content of the provided `StringBuilder` instead of creating new objects.

## Unicode support

Every methods defined in this class work on code points instead of characters when appropriate. Consequently, those methods should behave correctly with characters outside the Basic Multilingual Plane (BMP).

Since:
  0.3
See Also:
  - `CharSequences`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static void`
  `remove(StringBuilder buffer, String toSearch)`
  Removes every occurrences of the given string in the given buffer.
  `static void`
  `repeat(StringBuilder buffer, char c, int count)`
  Deprecated, for removal: This API element is subject to removal in a future version.
  Replaced by `StringBuilder​.repeat(int, int)` since Java 21.
  `static void`
  `repeat(StringBuilder buffer, int offset, char c, int count)`
  Inserts the given character n times at the given position.
  `static void`
  `replace(StringBuilder buffer, char toSearch, char replaceBy)`
  Replaces every occurrences of the given character in the given buffer.
  `static void`
  `replace(StringBuilder buffer, int start, int end, char[] chars)`
  Replaces the characters in a substring of the buffer with characters in the specified array.
  `static void`
  `replace(StringBuilder buffer, String toSearch, String replaceBy)`
  Replaces every occurrences of the given string in the given buffer.
  `static void`
  `toASCII(StringBuilder buffer)`
  Replaces some Unicode characters by ASCII characters on a "best effort basis".
  `static void`
  `trimFractionalPart(StringBuilder buffer)`
  Trims the fractional part of the given formatted number, provided that it doesn't change the value.
  `static int`
  `trimWhitespaces(StringBuilder buffer, int lower, int upper)`
  Removes leading and trailing whitespace characters in a subregion of the specified buffer.

  ### Methods inherited from class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### trimWhitespaces

    public static int trimWhitespaces(StringBuilder buffer, int lower, int upper)
    Removes leading and trailing whitespace characters in a subregion of the specified buffer. Space characters are identified by the `Character​.isWhitespace(int)` method.
    Parameters:
      `buffer` - the buffer where to remove leading and trailing white spaces.
      `lower` - index of the first character of the subregion where to remove leading spaces.
      `upper` - index after the last character of the subregion where to remove trailing spaces.
    Returns:
      number of characters removed.
    Since:
      1.5
    See Also:
      - `CharSequences​.trimWhitespaces(CharSequence, int, int)`

  - ### replace

    public static void replace(StringBuilder buffer, char toSearch, char replaceBy)
    Replaces every occurrences of the given character in the given buffer.
    Parameters:
      `buffer` - the string in which to perform the replacements.
      `toSearch` - the character to replace.
      `replaceBy` - the replacement for the searched character.
    Throws:
      `NullPointerException` - if the `buffer` arguments is null.
    See Also:
      - `String​.replace(char, char)`

  - ### replace

    public static void replace(StringBuilder buffer, String toSearch, String replaceBy)
    Replaces every occurrences of the given string in the given buffer. This method invokes `StringBuilder​.replace(int, int, String)` for each occurrence of `search` found in the buffer.
    Parameters:
      `buffer` - the string in which to perform the replacements.
      `toSearch` - the string to replace.
      `replaceBy` - the replacement for the searched string.
    Throws:
      `NullPointerException` - if any of the arguments is null.
      `IllegalArgumentException` - if the `toSearch` argument is empty.
    See Also:
      - `String​.replace(char, char)`
      - `CharSequences​.replace(CharSequence, CharSequence, CharSequence)`
      - `StringBuilder​.replace(int, int, String)`

  - ### replace

    public static void replace(StringBuilder buffer, int start, int end, char[] chars)
    Replaces the characters in a substring of the buffer with characters in the specified array. The substring to be replaced begins at the specified `start` and extends to the character at index `end - 1`.
    Parameters:
      `buffer` - the buffer in which to perform the replacement.
      `start` - the beginning index in the `buffer`, inclusive.
      `end` - the ending index in the `buffer`, exclusive.
      `chars` - the array that will replace previous contents.
    Throws:
      `NullPointerException` - if the `buffer` or `chars` argument is null.
    See Also:
      - `StringBuilder​.replace(int, int, String)`

  - ### remove

    public static void remove(StringBuilder buffer, String toSearch)
    Removes every occurrences of the given string in the given buffer. This method invokes `StringBuilder​.delete(int, int)` for each occurrence of `search` found in the buffer.
    Parameters:
      `buffer` - the string in which to perform the removals.
      `toSearch` - the string to remove.
    Throws:
      `NullPointerException` - if any of the arguments is null.
      `IllegalArgumentException` - if the `toSearch` argument is empty.
    See Also:
      - `StringBuilder​.delete(int, int)`

  - ### repeat

    @Deprecated(since="1.5", forRemoval=true) public static void repeat(StringBuilder buffer, char c, int count)
    Deprecated, for removal: This API element is subject to removal in a future version.
    Replaced by `StringBuilder​.repeat(int, int)` since Java 21.
    Appends the given character n times. This method does nothing if the given `count` is zero.
    Parameters:
      `buffer` - the buffer where to append the character.
      `c` - the character to repeat.
      `count` - number of times to repeat the given character.
    Throws:
      `NullPointerException` - if the given buffer is null.
      `IllegalArgumentException` - if the given count is negative.
    Since:
      1.0

  - ### repeat

    public static void repeat(StringBuilder buffer, int offset, char c, int count)
    Inserts the given character n times at the given position. This method does nothing if the given `count` is zero.
    Parameters:
      `buffer` - the buffer where to insert the character.
      `offset` - position where to insert the characters.
      `c` - the character to repeat.
      `count` - number of times to repeat the given character.
    Throws:
      `NullPointerException` - if the given buffer is null.
      `IndexOutOfBoundsException` - if the given index is invalid.
      `IllegalArgumentException` - if the given count is negative.
    Since:
      0.8

  - ### trimFractionalPart

    public static void trimFractionalPart(StringBuilder buffer)
    Trims the fractional part of the given formatted number, provided that it doesn't change the value. This method assumes that the number is formatted in the US locale, typically by the `Double​.toString(double)` method.

    More specifically if the given buffer ends with a `'.'` character followed by a sequence of `'0'` characters, then those characters are removed. Otherwise this method does nothing. This is a all or nothing method: either the fractional part is completely removed, or either it is left unchanged.

    #### Use case

    This method is useful after a double value has been appended to the buffer, in order to make it appears like an integer when possible.
    Parameters:
      `buffer` - the buffer to trim if possible.
    Throws:
      `NullPointerException` - if the given `buffer` is null.
    See Also:
      - `CharSequences​.trimFractionalPart(CharSequence)`

  - ### toASCII

    public static void toASCII(StringBuilder buffer)
    Replaces some Unicode characters by ASCII characters on a "best effort basis". For example, the “ é ” character is replaced by “ e ” (without accent), the “ ″ ” symbol for minutes of angle is replaced by straight double quotes “ " ”, and combined characters like ㎏, ㎎, ㎝, ㎞, ㎢, ㎦, ㎖, ㎧, ㎩, ㎐, etc. are replaced by the corresponding sequences of characters.
    Parameters:
      `buffer` - the text to scan for Unicode characters to replace by ASCII characters.
    Throws:
      `NullPointerException` - if the given `buffer` is null.
    See Also:
      - `CharSequences​.toASCII(CharSequence)`
      - `Normalizer​.normalize(CharSequence, Normalizer​.Form)`