Class StringUtils

java.lang.Object
io.karatelabs.common.StringUtils

public class StringUtils extends Object
  • Field Details

  • Method Details

    • looksLikeJson

      public static boolean looksLikeJson(String s)
    • isXml

      public static boolean isXml(String s)
    • truncate

      public static String truncate(String s, int length, boolean addDots)
    • trimToEmpty

      public static String trimToEmpty(String s)
    • trimToNull

      public static String trimToNull(String s)
    • repeat

      public static String repeat(char c, int count)
    • join

      public static String join(Object[] a, char delimiter)
    • join

      public static String join(Collection<String> c, String delimiter)
    • split

      public static List<String> split(String s, char delimiter, boolean skipBackSlash)
    • isBlank

      public static boolean isBlank(String s)
    • toIdString

      public static String toIdString(String name)
    • splitByFirstLineFeed

      public static Pair<String> splitByFirstLineFeed(String text)
    • toStringLines

      public static List<String> toStringLines(String text)
    • countLineFeeds

      public static int countLineFeeds(String text)
    • wrappedLinesEstimate

      public static int wrappedLinesEstimate(String text, int colWidth)
    • containsIgnoreCase

      public static boolean containsIgnoreCase(List<String> list, String str)
    • getIgnoreKeyCase

      public static <T> T getIgnoreKeyCase(Map<String,T> map, String name)
    • removeIgnoreKeyCase

      public static void removeIgnoreKeyCase(Map<String,?> map, String name)
    • simplify

      public static Map<String,Object> simplify(Map<String,List<String>> map, boolean always)
    • throwableToString

      public static String throwableToString(Throwable t)
    • tokenizeCliCommand

      public static String[] tokenizeCliCommand(String command)
      Tokenizes a CLI command string into individual arguments. Handles both single-quoted and double-quoted strings.
      Parameters:
      command - the command string to tokenize
      Returns:
      array of arguments
    • shellEscape

      public static String shellEscape(String value)
      Escapes a string for safe use as a shell argument. Uses OS-appropriate escaping (single quotes for Unix, double quotes for Windows).
      Parameters:
      value - the string to escape
      Returns:
      escaped string suitable for shell command
    • shellEscapeUnix

      public static String shellEscapeUnix(String value)
      Escapes a string for Unix/Mac/Linux shell (bash, sh, zsh). Uses single quotes and escapes embedded single quotes as '\''
      Parameters:
      value - the string to escape
      Returns:
      escaped string wrapped in single quotes
    • shellEscapeWindows

      public static String shellEscapeWindows(String value)
      Escapes a string for Windows cmd.exe. Uses double quotes and escapes embedded double quotes and special chars.
      Parameters:
      value - the string to escape
      Returns:
      escaped string wrapped in double quotes
    • shellEscapePowerShell

      public static String shellEscapePowerShell(String value)
      Escapes a string for PowerShell. Uses single quotes and escapes embedded single quotes by doubling them.
      Parameters:
      value - the string to escape
      Returns:
      escaped string wrapped in single quotes
    • shellEscapeForPlatform

      public static String shellEscapeForPlatform(String value, String platform)
      Escapes a string for the specified shell platform.
      Parameters:
      value - the string to escape
      platform - "sh" for Unix/Linux/macOS, "cmd" for Windows CMD, "ps" for PowerShell
      Returns:
      escaped string suitable for the target platform
    • getLineContinuation

      public static String getLineContinuation(String platform)
      Returns the line continuation character for the specified shell platform.
      Parameters:
      platform - "sh" for Unix/Linux/macOS, "cmd" for Windows CMD, "ps" for PowerShell
      Returns:
      line continuation string
    • buildShellCommand

      public static String buildShellCommand(String command, String... args)
      Builds a complete shell command from command name and arguments. Properly escapes each argument for the current OS.
      Parameters:
      command - the command name
      args - the arguments
      Returns:
      complete shell command string
    • buildShellCommand

      public static String buildShellCommand(String command, List<String> args)
      Builds a complete shell command from command name and arguments. Properly escapes each argument for the current OS.
      Parameters:
      command - the command name
      args - the arguments
      Returns:
      complete shell command string
    • randomAlphaNumeric

      public static String randomAlphaNumeric(int len)
    • formatFileSize

      public static String formatFileSize(int bytes)
    • formatJson

      public static String formatJson(Object o)
    • formatJson

      public static String formatJson(Object o, boolean pretty, boolean lenient, boolean sort)
    • formatJson

      public static String formatJson(Object o, boolean pretty, boolean lenient, boolean sort, String indent)