Class UtlString

java.lang.Object
io.github.darvil.utils.UtlString

public final class UtlString extends Object
  • Field Details

    • ESCAPE_CHAR

      public static final char ESCAPE_CHAR
      Escape character used for terminal formatting
      See Also:
  • Method Details

    • getLongestLine

      @NotNull public static @NotNull String getLongestLine(@NotNull @NotNull String str)
      Get the longest line from the contents of a string. Lines are separated by newlines and the length is calculated without counting the escape sequences.
    • wrap

      @NotNull public static @NotNull String wrap(@NotNull @NotNull String str, int maxWidth)
      Wraps a string into multiple lines in order to fit in the given maximum width. Wrapping respects words and indentation, so no word will be split in two lines and indentation will be preserved.
      Parameters:
      str - The text to wrap.
      maxWidth - The maximum width that the text should never exceed.
      Returns:
      The wrapped text.
    • indent

      @NotNull public static @NotNull String indent(@NotNull @NotNull String str, int padCount, char padChar)
      Adds padCount characters (specified with padChar) at the left of the string. If the string has multiple lines, the padding is added on all of them.
      Parameters:
      str - The string to pad.
      padCount - The number of characters to add.
      padChar - The character to use for padding.
      Returns:
      The padded string.
    • indent

      @NotNull public static @NotNull String indent(@NotNull @NotNull String str, int padCount)
      Adds padCount space characters at the left of the string. If the string has multiple lines, the padding is added on all of them.
      Parameters:
      str - The string to pad.
      padCount - The amount of spaces to add.
      Returns:
      The padded string.
    • center

      @NotNull public static @NotNull String center(@NotNull @NotNull String str, int width, char padChar)
      Centers a string in a given width. padChar is used to fill the remaining space. The padding is equal on both sides of the string, even if the width is odd.

      If the string is longer than the width, it is returned as is.

      Parameters:
      str - The string to center.
      width - The width to center the string in.
      padChar - The character to use for padding.
      Returns:
      The centered string.
    • center

      @NotNull public static @NotNull String center(@NotNull @NotNull String str, int width)
      Centers a string in a given width. '-' is used to fill the remaining space. The padding is equal on both sides of the string, even if the width is odd.
      Parameters:
      str - The string to center.
      width - The width to center the string in.
      Returns:
      The centered string.
      See Also:
    • removeSequences

      @NotNull public static @NotNull String removeSequences(@NotNull @NotNull String str)
      Remove all formatting colors or format from the string
    • getLengthIgnoreSequences

      public static int getLengthIgnoreSequences(@NotNull @NotNull String str)
      Returns the length of the string, ignoring any formatting sequences.
    • plural

      @NotNull public static @NotNull String plural(@NotNull @NotNull String str, int count)
      Returns the count given appended to the string given. An 's' will be appended at the end if the count is not 1.
      Parameters:
      str - the string to append to
      count - the count
      Returns:
      "count str" or "count strs" depending on the count
    • isNullOrEmpty

      public static boolean isNullOrEmpty(@Nullable @Nullable String str)
      Returns true if the string given is null or empty.
      Parameters:
      str - the string to check
      Returns:
      true if the string is null or empty
    • isNullOrBlank

      public static boolean isNullOrBlank(@Nullable @Nullable String str)
      Returns true if the string given is null or blank.
      Parameters:
      str - the string to check
      Returns:
      true if the string is null or blank
    • split

      @NotNull public static @NotNull String @NotNull [] split(@NotNull @NotNull String str, @NotNull @NotNull String splitter, int max)
      Split a string by the given splitter. This is similar to String.split(String) but it will also ignore spaces around the splitter.
      Parameters:
      str - the string to split
      splitter - the splitter
      max - the maximum amount of splits
      Returns:
      the split string
    • split

      @NotNull public static @NotNull String @NotNull [] split(@NotNull @NotNull String str, char splitter, int max)
      Split a string by the given splitter. This is similar to String.split(String) but it will also ignore spaces around the splitter.
      Parameters:
      str - the string to split
      splitter - the splitter
      Returns:
      the split string
    • split

      @NotNull public static @NotNull String @NotNull [] split(@NotNull @NotNull String str, char splitter)
      split(String, char, int) with max set to -1. (Default of String.split(String))
      See Also:
    • splitAtLeadingWhitespace

      @NotNull public static @NotNull Pair<@NotNull String,@NotNull String> splitAtLeadingWhitespace(@NotNull @NotNull String str)
      Returns a pair of strings. The first string is the leading whitespace of the string given, and the second string is the string given without the leading whitespace.
      Parameters:
      str - the string to split
      Returns:
      a pair of strings
    • escapeQuotes

      @NotNull public static @NotNull String escapeQuotes(@NotNull @NotNull String str)
      Replaces all quotes in the given string with escaped quotes. For example, "hello' becomes \"testing\'.
      Parameters:
      str - the string to escape
      Returns:
      the escaped string