Class StringUtils
- java.lang.Object
-
- org.opennms.core.utils.StringUtils
-
public abstract class StringUtils extends Object
-
-
Constructor Summary
Constructors Constructor Description StringUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static String[]createCommandArray(String s)Convenience method for creating arrays of strings suitable for use as command-line parameters when executing an external process.static String[]createCommandArray(String s, char delim)Deprecated.Use createCommandArray(String s) instead.static booleanequalsTrimmed(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.static StringgetHumanReadableByteCount(long bytes, boolean si)Copied from https://programming.guide/java/formatting-byte-size-to-human-readable-format.htmlstatic booleanhasText(String text)static booleanisEmpty(String text)static booleanisLocalWindowsPath(String path)static Stringiso8601LocalOffsetString(Date d)static Stringiso8601OffsetString(Date d, java.time.ZoneId zone, java.time.temporal.ChronoUnit truncateTo)static IntegerparseDecimalInt(String value)static IntegerparseDecimalInt(String value, boolean throwExceptions)This is a quick and dirty parser for String representations of decimal integers.static DoubleparseDouble(String value, Double defaultValue)static IntegerparseInt(String value, Integer defaultValue)static LongparseLong(String value, Long defaultValue)static StringprettyXml(String xml)Uses the Xalan javax.transform classes to indent an XML string properly so that it is easier to read.static StringstripExtraQuotes(String string)static StringtoStringEfficiently(Date date)NMS-9091: This method callsDate.toString()but then callsDate.setTime(long)so that internally, theDate.cdatefield is deallocated.static Stringtruncate(String name, int length)truncatestatic Optional<String>truncatePrefix(String input, String prefix)Removes a prefix from the input but returns the input only if the prefix was present.
-
-
-
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 splitdelim- a char that does not already exist ins- Returns:
- An array of strings split by spaces outside of quotes.
- Throws:
IllegalArgumentException- Ifsis null or ifdelimalready exists ins.
-
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.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 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- Ifsis null.
-
isLocalWindowsPath
public static boolean isLocalWindowsPath(String path)
-
prettyXml
public static String prettyXml(String xml) throws TransformerException
Uses the Xalan javax.transform classes to indent an XML string properly so that it is easier to read.- Throws:
TransformerException
-
iso8601OffsetString
public static String iso8601OffsetString(Date d, java.time.ZoneId zone, java.time.temporal.ChronoUnit truncateTo)
-
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 comparingb- string to compare- Returns:
trueif 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 callsDate.setTime(long)so that internally, theDate.cdatefield is deallocated. This saves significant heap space forDateinstances 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, boolean throwExceptions)
This is a quick and dirty parser for String representations of decimal integers. It should be up to 2X faster thanInteger.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- theto remove the prefix fromprefix- the prefix to remove- Returns:
- an present
Optionalcontaining the string without the prefix iff the string was prefixed,Optional.empty()otherwise.
-
-