Class StringUtils

java.lang.Object
io.github.connellite.util.StringUtils

public class StringUtils extends Object
String helpers
  • Constructor Details

    • StringUtils

      public StringUtils()
  • Method Details

    • toString

      public static String toString(Object value)
      Returns a readable string for value, including array contents.

      null becomes the literal "null". Non-array values use String.valueOf(Object). Primitive arrays use the matching Arrays.toString overload; reference-type arrays use Arrays.deepToString(Object[]) so nested arrays are expanded instead of default object references like [I@….

      Parameters:
      value - any object or array, or null
      Returns:
      text suitable for logging or default formatting
    • join

      public static <T> String join(Iterable<T> items, String separator)
      Joins elements with String.valueOf(Object) for non-null items; null elements are represented as the four characters null, same as String.join(separator, elements) for an Iterable of CharSequence.
      Parameters:
      items - sequence to join; not null (may be empty)
      separator - placed between elements; not null (may be empty)
    • join

      public static <T> String join(Iterable<T> items, String separator, Function<? super T,? extends CharSequence> toString)
      Joins elements using toString for non-null items; null elements are not passed to toString — they are joined like String.join(CharSequence, Iterable) (the literal null). If toString returns null, that is passed to StringJoiner.add(CharSequence) and becomes the same four characters.
      Parameters:
      items - sequence to join; not null (may be empty)
      separator - placed between elements; not null (may be empty)
      toString - converts each non-null element; not null
    • join

      public static String join(Object obj, String separator)
      Joins multiple values into one string, dispatching on the runtime shape of obj.

      If obj is an Iterable, each element is appended with String.valueOf(Object). If it is any array (including primitives), elements are indexed with Array.get(java.lang.Object, int). If it is an Iterator, remaining elements are consumed and appended (the iterator is advanced). If it is a Map, each Map.Entry becomes key=value via string concatenation. Otherwise a single segment String.valueOf(Object) is used.

      Parameters:
      obj - non-null container, array, iterator, map, or scalar
      separator - placed between segments; not null (may be empty)
    • splitLines

      public static List<String> splitLines(String input)
      Splits input on line breaks using \n or \r\n as separators (String.split(String) with \r?\n).
      Parameters:
      input - the text; must not be null
      Returns:
      a list of lines (may contain a single element for text without newlines; behavior matches String.split(java.lang.String, int))
      Throws:
      NullPointerException - if input is null
    • splitAndTrim

      public static List<String> splitAndTrim(String string, String regex)
      Splits string with String.split(String, int) using limit 0 (trailing empty segments discarded per String.split(String)), trims each segment with String.trim(), and omits segments that are blank after trimming.
      Parameters:
      string - text to split; may be null (returns an empty list)
      regex - delimiter pattern passed to String.split(String, int); must not be null
      Returns:
      a new list of non-blank trimmed parts (never null; may be empty)
      Throws:
      NullPointerException - if regex is null
      See Also:
    • splitAndTrim

      public static List<String> splitAndTrim(String string, String regex, int limit)
      Same as splitAndTrim(String, String) but forwards limit to String.split(String, int) so callers can cap splits or preserve trailing empties when limit is negative or greater than zero, per String.split(String, int).
      Parameters:
      string - text to split; may be null (returns an empty list)
      regex - delimiter pattern; must not be null
      limit - maximum number of parts; same semantics as String.split(String, int)
      Returns:
      a new list of non-blank trimmed parts (never null; may be empty)
      Throws:
      NullPointerException - if regex is null
      See Also:
    • removeLineBreaks

      public static String removeLineBreaks(String text, boolean removeSoftHyphens)
      Normalizes line breaks in text for a single-line or flowed paragraph style.

      Applied in order:

      1. Strips leading spaces and tabs on each line (after a newline or at the start) and trailing spaces and tabs before a newline or end of string.
      2. Removes a newline immediately after a hyphen-like character (ASCII hyphen, Unicode dashes, or soft hyphen U+00AD), keeping the hyphen (typical line-break hyphenation in plain text).
      3. Replaces remaining newlines that sit between two non-newline characters with a single space (joins wrapped lines). Newlines adjacent to other line breaks are left as-is because the lookahead/lookbehind use ., which does not match line terminators.

      If removeSoftHyphens is true, all soft hyphens (U+00AD) are removed from the result.

      Parameters:
      text - source text; must not be null
      removeSoftHyphens - when true, strips every U+00AD from the result
      Returns:
      normalized text with the rules above applied
      Throws:
      NullPointerException - if text is null
    • isNumeric

      public static boolean isNumeric(CharSequence input)
      Returns whether input looks like a signed decimal integer: an optional leading ASCII hyphen ('-') followed only by characters for which Character.isDigit(char) is true.

      null and the empty sequence return false. A lone "-" is not numeric (there must be at least one digit after an optional sign).

      Parameters:
      input - text to inspect; may be null
      Returns:
      true if non-empty and every character passes the rules above
    • isDigits

      public static boolean isDigits(CharSequence input)
      Returns whether input is an optional leading '-' followed only by ASCII digits '0'–'9'.

      null, the empty sequence, and a lone "-" return false.

      Parameters:
      input - text to inspect; may be null
      Returns:
      true if non-empty and every character passes the rules above
    • compileMatchPattern

      public static Pattern compileMatchPattern(String match)
      Builds a Pattern from a simple match string with shell-style wildcards, then compiles it with Pattern.CASE_INSENSITIVE and Pattern.DOTALL.

      Transformations apply only to characters that are not escaped by a preceding backslash (per-step (?<!\\\\) rules in the implementation):

      • Runs of paired backslashes are replaced by a single *.
      • Redundant unescaped ? / * clusters (e.g. ?**) collapse to one *.
      • Remaining regex metacharacters in |[]{}(),.^$+- are escaped for literal matching.
      • Unescaped ? becomes (.?) (zero or one of any character, including newlines).
      • Unescaped * becomes (.*) (any length, including newlines).
      Parameters:
      match - user-facing pattern; must not be null
      Returns:
      compiled pattern ready for Matcher.matches(), Matcher.find(), etc.
      Throws:
      NullPointerException - if match is null
      PatternSyntaxException - if the transformed string is not a valid regex
    • isUpperCase

      public static boolean isUpperCase(String input)
      Returns whether input equals its String.toUpperCase() form using the default locale.
      Parameters:
      input - the string; must not be null
      Returns:
      true if unchanged by toUpperCase()
      Throws:
      NullPointerException - if input is null
    • isLowerCase

      public static boolean isLowerCase(String input)
      Returns whether input equals its String.toLowerCase() form using the default locale.
      Parameters:
      input - the string; must not be null
      Returns:
      true if unchanged by toLowerCase()
      Throws:
      NullPointerException - if input is null
    • convertInputStreamToString

      public static String convertInputStreamToString(InputStream in) throws IOException
      Reads all bytes from in until end-of-stream and decodes them as UTF-8.

      Does not close in.

      Parameters:
      in - the stream; must not be null
      Returns:
      the full decoded string
      Throws:
      IOException - if reading fails
    • convertInputStreamToString

      public static String convertInputStreamToString(InputStream in, Charset charset) throws IOException
      Reads all bytes from in until end-of-stream and decodes them with charset.

      Does not close in.

      Parameters:
      in - the stream; must not be null
      charset - character set for decoding; must not be null
      Returns:
      the full decoded string
      Throws:
      IOException - if reading fails
    • toCamelCase

      public static String toCamelCase(String input)
      Converts a phrase or identifier to lower camelCase: first token lowercased, rest title-cased and concatenated.

      Uses CASE_CONVENTION_TOKEN_PATTERN to segment the input.

      Parameters:
      input - the source string; must not be null
      Returns:
      camel-cased text
      Throws:
      NullPointerException - if input is null
      StringIndexOutOfBoundsException - if no tokens match (e.g. input is empty)
    • toKebabCase

      public static String toKebabCase(String input)
      Converts a phrase or identifier to kebab-case: tokens lowercased and joined with '-'.

      Uses CASE_CONVENTION_TOKEN_PATTERN to segment the input.

      Parameters:
      input - the source string; must not be null
      Returns:
      kebab-cased text (empty if input is empty)
      Throws:
      NullPointerException - if input is null
    • toSnakeCase

      public static String toSnakeCase(String input)
      Converts a phrase or identifier to snake_case: tokens lowercased and joined with '_'.

      Uses CASE_CONVENTION_TOKEN_PATTERN to segment the input.

      Parameters:
      input - the source string; must not be null
      Returns:
      snake-cased text (empty if input is empty)
      Throws:
      NullPointerException - if input is null