public abstract class StringUtils extends Object
Constructor and Description |
---|
StringUtils() |
Modifier and Type | Method and 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.html
|
static 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 calls
Date.toString() but then calls
Date.setTime(long) so that internally, the Date.cdate
field is deallocated. |
static String |
truncate(String name,
int length)
truncate
|
static Optional<String> |
truncatePrefix(String input,
String prefix)
Removes a prefix from the input but returns the input only if the prefix was present.
|
public static String[] createCommandArray(String s, char delim)
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.
s
- the string to splitdelim
- a char that does not already exist in s
IllegalArgumentException
- If s
is null or if delim
already exists in s
.public static String[] createCommandArray(String s)
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.
s
- the string to splitIllegalArgumentException
- If s
is null.public static boolean isLocalWindowsPath(String path)
public static String prettyXml(String xml) throws TransformerException
TransformerException
public static String iso8601OffsetString(Date d, java.time.ZoneId zone, java.time.temporal.ChronoUnit truncateTo)
public static boolean equalsTrimmed(String a, String b)
a
- string to trim before comparingb
- string to comparetrue
if A equals B, after A is trimmedpublic static boolean isEmpty(String text)
public static boolean hasText(String text)
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.
date
- public static Integer parseDecimalInt(String value, boolean throwExceptions)
Integer.parseInt(String)
.value
- Positive or negative decimal string valuepublic static String getHumanReadableByteCount(long bytes, boolean si)
public static Optional<String> truncatePrefix(String input, String prefix)
input
- the to remove the prefix from
prefix
- the prefix to removeOptional
containing the string without the prefix iff the string was prefixed,
Optional.empty()
otherwise.Copyright © 2021. All rights reserved.