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 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.static String
getHumanReadableByteCount(long bytes, boolean si)
Copied from https://programming.guide/java/formatting-byte-size-to-human-readable-format.htmlstatic boolean
hasText(String text)
static boolean
isEmpty(String text)
static boolean
isLocalWindowsPath(String path)
static String
iso8601LocalOffsetString(Date d)
static String
iso8601OffsetString(Date d, java.time.ZoneId zone, java.time.temporal.ChronoUnit truncateTo)
static Integer
parseDecimalInt(String value)
static Integer
parseDecimalInt(String value, boolean throwExceptions)
This is a quick and dirty parser for String representations of decimal integers.static Double
parseDouble(String value, Double defaultValue)
static Integer
parseInt(String value, Integer defaultValue)
static Long
parseLong(String value, Long defaultValue)
static String
prettyXml(String xml)
Uses the Xalan javax.transform classes to indent an XML string properly so that it is easier to read.static String
stripExtraQuotes(String string)
static String
toStringEfficiently(Date date)
NMS-9091: This method callsDate.toString()
but then callsDate.setTime(long)
so that internally, theDate.cdate
field is deallocated.static String
truncate(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.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 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
- Ifs
is null or ifdelim
already 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.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
- Ifs
is 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:
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 callsDate.setTime(long)
so that internally, theDate.cdate
field is deallocated. This saves significant heap space forDate
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, 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 from
prefix
- the prefix to remove- Returns:
- an present
Optional
containing the string without the prefix iff the string was prefixed,Optional.empty()
otherwise.
-
-