Class StringUtils
- java.lang.Object
-
- org.opennms.core.utils.StringUtils
-
public abstract class StringUtils extends java.lang.Object
-
-
Constructor Summary
Constructors Constructor Description StringUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static java.lang.String[]
createCommandArray(java.lang.String s)
Convenience method for creating arrays of strings suitable for use as command-line parameters when executing an external process.static java.lang.String[]
createCommandArray(java.lang.String s, char delim)
Deprecated.Use createCommandArray(String s) instead.static boolean
equalsTrimmed(java.lang.String a, java.lang.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 java.lang.String
getHumanReadableByteCount(long bytes, boolean si)
Copied from https://programming.guide/java/formatting-byte-size-to-human-readable-format.htmlstatic boolean
hasText(java.lang.String text)
static boolean
isEmpty(java.lang.String text)
static boolean
isLocalWindowsPath(java.lang.String path)
static java.lang.String
iso8601LocalOffsetString(java.util.Date d)
static java.lang.String
iso8601OffsetString(java.util.Date d, java.time.ZoneId zone, java.time.temporal.ChronoUnit truncateTo)
static java.lang.Integer
parseDecimalInt(java.lang.String value)
static java.lang.Integer
parseDecimalInt(java.lang.String value, boolean throwExceptions)
This is a quick and dirty parser for String representations of decimal integers.static java.lang.Double
parseDouble(java.lang.String value, java.lang.Double defaultValue)
static java.lang.Integer
parseInt(java.lang.String value, java.lang.Integer defaultValue)
static java.lang.Long
parseLong(java.lang.String value, java.lang.Long defaultValue)
static java.lang.String
prettyXml(java.lang.String xml)
Uses the Xalan javax.transform classes to indent an XML string properly so that it is easier to read.static java.lang.String
stripExtraQuotes(java.lang.String string)
static java.lang.String
toStringEfficiently(java.util.Date date)
NMS-9091: This method callsDate.toString()
but then callsDate.setTime(long)
so that internally, theDate.cdate
field is deallocated.static java.lang.String
truncate(java.lang.String name, int length)
truncatestatic java.util.Optional<java.lang.String>
truncatePrefix(java.lang.String input, java.lang.String prefix)
Removes a prefix from the input but returns the input only if the prefix was present.
-
-
-
Method Detail
-
createCommandArray
public static java.lang.String[] createCommandArray(java.lang.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:
java.lang.IllegalArgumentException
- Ifs
is null or ifdelim
already exists ins
.
-
createCommandArray
public static java.lang.String[] createCommandArray(java.lang.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:
java.lang.IllegalArgumentException
- Ifs
is null.
-
truncate
public static java.lang.String truncate(java.lang.String name, int length)
truncate
- Parameters:
name
- aString
object.length
- a int.- Returns:
- a
String
object.
-
isLocalWindowsPath
public static boolean isLocalWindowsPath(java.lang.String path)
-
prettyXml
public static java.lang.String prettyXml(java.lang.String xml) throws javax.xml.transform.TransformerException
Uses the Xalan javax.transform classes to indent an XML string properly so that it is easier to read.- Throws:
javax.xml.transform.TransformerException
-
iso8601LocalOffsetString
public static java.lang.String iso8601LocalOffsetString(java.util.Date d)
-
iso8601OffsetString
public static java.lang.String iso8601OffsetString(java.util.Date d, java.time.ZoneId zone, java.time.temporal.ChronoUnit truncateTo)
-
stripExtraQuotes
public static java.lang.String stripExtraQuotes(java.lang.String string)
-
equalsTrimmed
public static boolean equalsTrimmed(java.lang.String a, java.lang.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(java.lang.String text)
-
hasText
public static boolean hasText(java.lang.String text)
-
toStringEfficiently
public static java.lang.String toStringEfficiently(java.util.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 java.lang.Integer parseDecimalInt(java.lang.String value)
-
parseDecimalInt
public static java.lang.Integer parseDecimalInt(java.lang.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 java.lang.String getHumanReadableByteCount(long bytes, boolean si)
Copied from https://programming.guide/java/formatting-byte-size-to-human-readable-format.html
-
truncatePrefix
public static java.util.Optional<java.lang.String> truncatePrefix(java.lang.String input, java.lang.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.
-
parseInt
public static java.lang.Integer parseInt(java.lang.String value, java.lang.Integer defaultValue)
-
parseLong
public static java.lang.Long parseLong(java.lang.String value, java.lang.Long defaultValue)
-
parseDouble
public static java.lang.Double parseDouble(java.lang.String value, java.lang.Double defaultValue)
-
-