java.lang.Object
com.aoapps.lang.Strings
- Author:
- AO Industries, Inc.
-
Method Summary
Modifier and TypeMethodDescriptionstatic int
Compares two strings in a case insensitive manner.static boolean
containsIgnoreCase
(String line, String word) static byte[]
convertByteArrayFromHex
(char[] hex) Deprecated.static int
convertIntArrayFromHex
(char[] hex) Deprecated.Please useHex
static long
convertLongArrayFromHex
(char[] hex) Deprecated.Please useHex
static String
convertToHex
(byte[] bytes) Deprecated.Please useHex
static void
convertToHex
(byte[] bytes, Appendable out) Deprecated.Please useHex
static String
convertToHex
(int value) Deprecated.Please useHex
static void
convertToHex
(int value, Appendable out) Deprecated.Please useHex
static String
convertToHex
(long value) Deprecated.Please useHex
static void
convertToHex
(long value, Appendable out) Deprecated.Please useHex
static int
countOccurrences
(byte[] buff, int len, String word) Counts how many times a word appears in a line.static int
countOccurrences
(byte[] buff, String word) Counts how many times a word appears in a line.static int
countOccurrences
(String line, String word) Counts how many times a word appears in a line.static String
firstLineOnly
(String value, int maxCharacters) Returns the first line only, and only up to the maximum number of characters.static String
getApproximateBitRate
(long bitRate) Gets the approximate bit rate (where k=1000).static String
getApproximateSize
(long size) Gets the approximate size (where k=1024) of a file.static String
getDecimalTimeLengthString
(long time) static String
getDecimalTimeLengthString
(long time, boolean alwaysShowMillis) static int
getHex
(char ch) Deprecated.Please useHex
static char
getHexChar
(int v) Deprecated.Please useHex
static String
getMonth
(int month) Deprecated.This method is not locale-aware, is no longer used, and will be removed.static String
getTimeLengthString
(long time) static int
Finds the first occurrence of any of the supplied characters.static int
Finds the first occurrence of any of the supplied characters starting at the specified index.static int
Finds the next of a substring like regular String.indexOf, but stops at a certain maximum index.static int
Finds the first occurrence of any of the supplied characters.static int
Finds the first occurrence of any of the supplied characters starting at the specified index.static boolean
isWhitespace
(char ch) Determines if a character is whitespace.static boolean
isWhitespace
(int codePoint) Determines if a code point is whitespace.static String
Joins the string representation of objects on the provided delimiter.static <A extends Appendable>
AJoins the string representation of objects on the provided delimiter.static String
Joins the string representation of objects on the provided delimiter.static <A extends Appendable>
AJoins the string representation of objects on the provided delimiter.static String
nullIfEmpty
(String value) Returns null if the string is null or empty.static void
replace
(StringBuffer sb, String find, String replacement) Replaces all occurrences of a String with a String.static void
replace
(StringBuilder sb, String find, String replacement) Replaces all occurrences of a String with a String.static String
Replaces all occurrences of a character with a String.static void
replace
(String string, char find, String replacement, Appendable out) Replaces all occurrences of a character with a String, appends the replacement toout
.static void
replace
(String string, char find, String replacement, Appendable out, Encoder encoder) Replaces all occurrences of a character with a String, appends the replacement toout
.static String
Replaces all occurrences of a String with a String.static void
replace
(String string, String find, String replacement, Appendable out) Replaces all occurrences of a String with a String, appends the replacement toout
.static void
Replaces all occurrences of a String with a String, appends the replacement toout
.static String[]
Splits aString
into aString[]
.Splits a string on the given delimiter.static int
Deprecated.It is highly unlikely this method is still usedstatic <C extends Collection<String>>
CSplits a string on the given delimiter.Splits a string on the given delimiter over the given range.static <C extends Collection<String>>
CSplits a string on the given delimiter over the given range.static int
Deprecated.It is highly unlikely this method is still usedsplitCommaSpace
(String line) Splits a string into multiple words on either whitespace or commas.splitLines
(String s) Splits a String into lines on any '\n' characters.static CharSequence
trim
(CharSequence value) Trims a value, as per rules ofisWhitespace(int)
.static String
Trims a value, as per rules ofisWhitespace(int)
.static CharSequence
trimNullIfEmpty
(CharSequence value) Trims a value, as per rules ofisWhitespace(int)
, returningnull
if empty after trimming.static String
trimNullIfEmpty
(String value) Trims a value, as per rules ofisWhitespace(int)
, returningnull
if empty after trimming.static String
Deprecated.Use new version with Appendable for higher performancestatic void
wordWrap
(String string, int width, Appendable out) Word wraps aString
to be no longer than the provided number of characters wide.
-
Method Details
-
getMonth
Deprecated.This method is not locale-aware, is no longer used, and will be removed. -
join
public static String join(Iterable<?> objects, String delimiter) throws ConcurrentModificationException Joins the string representation of objects on the provided delimiter. The iteration will be performed twice. Once to compute the total length of the resulting string, and the second to build the result.- Throws:
ConcurrentModificationException
- if iteration is not consistent between passes- See Also:
-
join
public static <A extends Appendable> A join(Iterable<?> objects, String delimiter, A out) throws IOException Joins the string representation of objects on the provided delimiter.- Throws:
IOException
- See Also:
-
join
public static String join(Object[] objects, String delimiter) throws ConcurrentModificationException Joins the string representation of objects on the provided delimiter. The iteration will be performed twice. Once to compute the total length of the resulting string, and the second to build the result.- Throws:
ConcurrentModificationException
- if iteration is not consistent between passes- See Also:
-
join
public static <A extends Appendable> A join(Object[] objects, String delimiter, A out) throws IOException Joins the string representation of objects on the provided delimiter.- Throws:
IOException
- See Also:
-
containsIgnoreCase
-
countOccurrences
Counts how many times a word appears in a line. Case insensitive matching. -
countOccurrences
Counts how many times a word appears in a line. Case insensitive matching. -
countOccurrences
Counts how many times a word appears in a line. Case insensitive matching. -
getTimeLengthString
-
getDecimalTimeLengthString
-
getDecimalTimeLengthString
-
indexOf
Finds the first occurrence of any of the supplied characters.- Parameters:
s
- theString
to searchchars
- the characters to look for- Returns:
- the index of the first occurrence of
-1
if none found
-
indexOf
Finds the first occurrence of any of the supplied characters starting at the specified index.- Parameters:
s
- theString
to searchchars
- the characters to look forstart
- the starting index.- Returns:
- the index of the first occurrence of
-1
if none found
-
indexOf
Finds the first occurrence of any of the supplied characters.- Parameters:
s
- theString
to searchchars
- the characters to look for- Returns:
- the index of the first occurrence of
-1
if none found
-
indexOf
Finds the first occurrence of any of the supplied characters starting at the specified index.- Parameters:
s
- theString
to searchchars
- the characters to look forstart
- the starting index.- Returns:
- the index of the first occurrence of
-1
if none found
-
replace
Replaces all occurrences of a character with a String. Please consider the variant with theAppendable
for higher performance. -
replace
Replaces all occurrences of a String with a String. Please consider the variant with theAppendable
for higher performance. -
replace
public static void replace(String string, char find, String replacement, Appendable out) throws IOException Replaces all occurrences of a character with a String, appends the replacement toout
.- Throws:
IOException
-
replace
public static void replace(String string, String find, String replacement, Appendable out) throws IOException Replaces all occurrences of a String with a String, appends the replacement toout
.- Throws:
IOException
-
replace
public static void replace(String string, char find, String replacement, Appendable out, Encoder encoder) throws IOException Replaces all occurrences of a character with a String, appends the replacement toout
.- Throws:
IOException
-
replace
public static void replace(String string, String find, String replacement, Appendable out, Encoder encoder) throws IOException Replaces all occurrences of a String with a String, appends the replacement toout
.- Throws:
IOException
-
replace
Replaces all occurrences of a String with a String. -
replace
Replaces all occurrences of a String with a String. -
splitLines
Splits a String into lines on any '\n' characters. Also removes any ending '\r' characters if present -
split
Splits aString
into aString[]
.- See Also:
-
split
Deprecated.It is highly unlikely this method is still usedSplits aString
into aString[]
.- See Also:
-
split
Deprecated.It is highly unlikely this method is still usedSplits aString
into aString[]
.- See Also:
-
split
Splits a string on the given delimiter. Does include all empty elements on the split.- Returns:
- the modifiable list from the split
-
split
Splits a string on the given delimiter. Does include all empty elements on the split.- Parameters:
words
- the words will be added to this collection.- Returns:
- the collection provided in words parameter
-
split
Splits a string on the given delimiter over the given range. Does include all empty elements on the split.- Returns:
- the modifiable list from the split
-
split
public static <C extends Collection<String>> C split(String line, int begin, int end, char delim, C words) Splits a string on the given delimiter over the given range. Does include all empty elements on the split.- Parameters:
words
- the words will be added to this collection.- Returns:
- the collection provided in words parameter
-
split
-
splitCommaSpace
Splits a string into multiple words on either whitespace or commas.- Returns:
- The list of non-empty strings.
- See Also:
-
wordWrap
Deprecated.Use new version with Appendable for higher performanceWord wraps aString
to be no longer than the provided number of characters wide. -
wordWrap
Word wraps aString
to be no longer than the provided number of characters wide.TODO: Make this more efficient by eliminating the internal use of substring.
- Throws:
IOException
-
getHexChar
Deprecated.Please useHex
Gets the hexadecimal character for the low-order four bits of the provided int. -
getHex
Deprecated.Please useHex
Converts one hex digit to an integer.- Throws:
IllegalArgumentException
-
convertToHex
Deprecated.Please useHex
- Throws:
IOException
-
convertToHex
Deprecated.Please useHex
-
convertByteArrayFromHex
Deprecated.Please useHex
-
convertToHex
Deprecated.Please useHex
Converts an int to a full 8-character hex code.- Throws:
IOException
-
convertToHex
Deprecated.Please useHex
Converts an int to a full 8-character hex code. -
convertIntArrayFromHex
Deprecated.Please useHex
-
convertToHex
Deprecated.Please useHex
Converts a long integer to a full 16-character hex code.- Throws:
IOException
-
convertToHex
Deprecated.Please useHex
Converts a long integer to a full 16-character hex code. -
convertLongArrayFromHex
Deprecated.Please useHex
-
getApproximateSize
Gets the approximate size (where k=1024) of a file. In this format:x byte(s) xx bytes xxx bytes x.x k xx.x k xxx k x.x M xx.x M xxx M x.x G xx.x G xxx G x.x T xx.x T xxx T xxx... T
-
getApproximateBitRate
Gets the approximate bit rate (where k=1000). In this format:x xx xxx x.x k xx.x k xxx k x.x M xx.x M xxx M x.x G xx.x G xxx G x.x T xx.x T xxx T xxx... T
-
compareToIgnoreCaseCarefulEquals
Compares two strings in a case insensitive manner. However, if they are considered equals in the case-insensitive manner, the case sensitive comparison is done. -
indexOf
Finds the next of a substring like regular String.indexOf, but stops at a certain maximum index. Like substring, will look up to the character one before toIndex. -
firstLineOnly
Returns the first line only, and only up to the maximum number of characters. If the value is modified, will append a horizontal ellipsis (Unicode 0x2026). -
nullIfEmpty
Returns null if the string is null or empty. -
isWhitespace
public static boolean isWhitespace(char ch) Determines if a character is whitespace. A character is considered whitespace if it is either<= ' '
(for compatibility withString.trim()
) or matchesCharacter.isWhitespace(char)
. -
isWhitespace
public static boolean isWhitespace(int codePoint) Determines if a code point is whitespace. A code point is considered whitespace if it is either<= ' '
(for compatibility withString.trim()
) or matchesCharacter.isWhitespace(int)
. -
trim
Trims a value, as per rules ofisWhitespace(int)
.- Returns:
- The value trimmed or
null
when wasnull
- See Also:
-
trim
Trims a value, as per rules ofisWhitespace(int)
.- Returns:
- The value trimmed or
null
when wasnull
- See Also:
-
trimNullIfEmpty
Trims a value, as per rules ofisWhitespace(int)
, returningnull
if empty after trimming. -
trimNullIfEmpty
Trims a value, as per rules ofisWhitespace(int)
, returningnull
if empty after trimming.
-
Hex