Class StringUtils


  • public abstract class StringUtils
    extends Object
    • Constructor Detail

      • StringUtils

        public StringUtils()
    • Method Detail

      • createCommandArray

        public static String[] createCommandArray​(String s,
                                                  char delim)
        Deprecated.
        Use createCommandArray(String s) instead.
        Convenience method for creating arrays of strings suitable for use as command-line parameters when executing an external process.

        The default Runtime.execmethod will split a single string based on spaces, but it does not respect spaces within quotation marks, and it will leave the quotation marks in the resulting substrings. This method solves those problems by replacing all in-quote spaces with the given delimiter, removes the quotes, and then splits the resulting string by the remaining out-of-quote spaces. It then goes through each substring and replaces the delimiters with spaces.

        Caveat: This method does not respect escaped quotes! It will simply remove them and leave the stray escape characters.

        Parameters:
        s - the string to split
        delim - a char that does not already exist in s
        Returns:
        An array of strings split by spaces outside of quotes.
        Throws:
        IllegalArgumentException - If s is null or if delim already exists in s.
      • createCommandArray

        public static String[] createCommandArray​(String s)
        Convenience method for creating arrays of strings suitable for use as command-line parameters when executing an external process.

        The default Runtime.exec method will split a single string based on spaces, but it does not respect spaces within quotation marks, and it will leave the quotation marks in the resulting substrings. This method solves those problems by preserving all in-quote spaces.

        Caveat: This method does not respect escaped quotes! It will simply remove them and leave the stray escape characters.

        Parameters:
        s - the string to split
        Returns:
        An array of strings split by spaces outside of quotes.
        Throws:
        IllegalArgumentException - If s is null.
      • truncate

        public static String truncate​(String name,
                                      int length)

        truncate

        Parameters:
        name - a String object.
        length - a int.
        Returns:
        a String object.
      • isLocalWindowsPath

        public static boolean isLocalWindowsPath​(String path)
      • iso8601LocalOffsetString

        public static String iso8601LocalOffsetString​(Date d)
      • iso8601OffsetString

        public static String iso8601OffsetString​(Date d,
                                                 java.time.ZoneId zone,
                                                 java.time.temporal.ChronoUnit truncateTo)
      • stripExtraQuotes

        public static String stripExtraQuotes​(String string)
      • equalsTrimmed

        public static boolean equalsTrimmed​(String a,
                                            String b)
        This is an optimized version of: return a != null && a.trim().equals(b) that avoids creating a trimmed substring of A before comparison. Instead A and B are compared in place.
        Parameters:
        a - string to trim before comparing
        b - string to compare
        Returns:
        true if A equals B, after A is trimmed
      • isEmpty

        public static boolean isEmpty​(String text)
      • hasText

        public static boolean hasText​(String text)
      • toStringEfficiently

        public static String toStringEfficiently​(Date date)

        NMS-9091: This method calls Date.toString() but then calls Date.setTime(long) so that internally, the Date.cdate field is deallocated. This saves significant heap space for Date instances that are stored in long-lived collections.

        • java.util.Date with only fastTime: 24 bytes
        • java.util.Date with fastTime and cdate: 120 bytes
        Parameters:
        date -
        Returns:
        Value of date.toString()
      • parseDecimalInt

        public static Integer parseDecimalInt​(String value)
      • parseDecimalInt

        public static Integer parseDecimalInt​(String value,
                                              boolean throwExceptions)
        This is a quick and dirty parser for String representations of decimal integers. It should be up to 2X faster than Integer.parseInt(String).
        Parameters:
        value - Positive or negative decimal string value
        Returns:
        Integer representing the string value
      • getHumanReadableByteCount

        public static String getHumanReadableByteCount​(long bytes,
                                                       boolean si)
        Copied from https://programming.guide/java/formatting-byte-size-to-human-readable-format.html
      • truncatePrefix

        public static Optional<String> truncatePrefix​(String input,
                                                      String prefix)
        Removes a prefix from the input but returns the input only if the prefix was present.
        Parameters:
        input - the to remove the prefix from
        prefix - the prefix to remove
        Returns:
        an present Optional containing the string without the prefix iff the string was prefixed, Optional.empty() otherwise.
      • parseLong

        public static Long parseLong​(String value,
                                     Long defaultValue)