- java.lang.Object
-
- com.aoapps.lang.Strings
-
public final class Strings extends Object
- Author:
- AO Industries, Inc.
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static intcompareToIgnoreCaseCarefulEquals(String s1, String s2)Compares two strings in a case insensitive manner.static booleancontainsIgnoreCase(String line, String word)static byte[]convertByteArrayFromHex(char[] hex)Deprecated.Please useHexstatic intconvertIntArrayFromHex(char[] hex)Deprecated.Please useHexstatic longconvertLongArrayFromHex(char[] hex)Deprecated.Please useHexstatic StringconvertToHex(byte[] bytes)Deprecated.Please useHexstatic voidconvertToHex(byte[] bytes, Appendable out)Deprecated.Please useHexstatic StringconvertToHex(int value)Deprecated.Please useHexstatic voidconvertToHex(int value, Appendable out)Deprecated.Please useHexstatic StringconvertToHex(long value)Deprecated.Please useHexstatic voidconvertToHex(long value, Appendable out)Deprecated.Please useHexstatic intcountOccurrences(byte[] buff, int len, String word)Counts how many times a word appears in a line.static intcountOccurrences(byte[] buff, String word)Counts how many times a word appears in a line.static intcountOccurrences(String line, String word)Counts how many times a word appears in a line.static StringfirstLineOnly(String value, int maxCharacters)Returns the first line only, and only up to the maximum number of characters.static StringgetApproximateBitRate(long bitRate)Gets the approximate bit rate (where k=1000).static StringgetApproximateSize(long size)Gets the approximate size (where k=1024) of a file.static StringgetDecimalTimeLengthString(long time)static StringgetDecimalTimeLengthString(long time, boolean alwaysShowMillis)static intgetHex(char ch)Deprecated.Please useHexstatic chargetHexChar(int v)Deprecated.Please useHexstatic StringgetMonth(int month)Deprecated.This method is not locale-aware, is no longer used, and will be removed.static StringgetTimeLengthString(long time)static intindexOf(String s, char[] chars)Finds the first occurrence of any of the supplied characters.static intindexOf(String s, char[] chars, int start)Finds the first occurrence of any of the supplied characters starting at the specified index.static intindexOf(String source, String target, int fromIndex, int toIndex)Finds the next of a substring like regular String.indexOf, but stops at a certain maximum index.static intindexOf(String s, BitSet chars)Finds the first occurrence of any of the supplied characters.static intindexOf(String s, BitSet chars, int start)Finds the first occurrence of any of the supplied characters starting at the specified index.static booleanisWhitespace(char ch)Determines if a character is whitespace.static booleanisWhitespace(int codePoint)Determines if a code point is whitespace.static Stringjoin(Iterable<?> objects, String delimiter)Joins the string representation of objects on the provided delimiter.static <A extends Appendable>
Ajoin(Iterable<?> objects, String delimiter, A out)Joins the string representation of objects on the provided delimiter.static Stringjoin(Object[] objects, String delimiter)Joins the string representation of objects on the provided delimiter.static <A extends Appendable>
Ajoin(Object[] objects, String delimiter, A out)Joins the string representation of objects on the provided delimiter.static StringnullIfEmpty(String value)Returns null if the string is null or empty.static voidreplace(StringBuffer sb, String find, String replacement)Replaces all occurrences of a String with a String.static voidreplace(StringBuilder sb, String find, String replacement)Replaces all occurrences of a String with a String.static Stringreplace(String string, char ch, String replacement)Replaces all occurrences of a character with a String.static voidreplace(String string, char find, String replacement, Appendable out)Replaces all occurrences of a character with a String, appends the replacement toout.static voidreplace(String string, char find, String replacement, Appendable out, Encoder encoder)Replaces all occurrences of a character with a String, appends the replacement toout.static Stringreplace(String string, String find, String replacement)Replaces all occurrences of a String with a String.static voidreplace(String string, String find, String replacement, Appendable out)Replaces all occurrences of a String with a String, appends the replacement toout.static voidreplace(String string, String find, String replacement, Appendable out, Encoder encoder)Replaces all occurrences of a String with a String, appends the replacement toout.static String[]split(String line)Splits aStringinto aString[].static List<String>split(String line, char delim)Splits a string on the given delimiter.static intsplit(String line, char[][][] buff)Deprecated.It is highly unlikely this method is still usedstatic <C extends Collection<String>>
Csplit(String line, char delim, C words)Splits a string on the given delimiter.static List<String>split(String line, int begin, int end, char delim)Splits a string on the given delimiter over the given range.static <C extends Collection<String>>
Csplit(String line, int begin, int end, char delim, C words)Splits a string on the given delimiter over the given range.static List<String>split(String line, String delim)static intsplit(String line, String[][] buff)Deprecated.It is highly unlikely this method is still usedstatic List<String>splitCommaSpace(String line)Splits a string into multiple words on either whitespace or commas.static List<String>splitLines(String s)Splits a String into lines on any '\n' characters.static CharSequencetrim(CharSequence value)Trims a value, as per rules ofisWhitespace(int).static Stringtrim(String value)Trims a value, as per rules ofisWhitespace(int).static CharSequencetrimNullIfEmpty(CharSequence value)Trims a value, as per rules ofisWhitespace(int), returningnullif empty after trimming.static StringtrimNullIfEmpty(String value)Trims a value, as per rules ofisWhitespace(int), returningnullif empty after trimming.static StringwordWrap(String string, int width)Deprecated.Use new version with Appendable for higher performancestatic voidwordWrap(String string, int width, Appendable out)Word wraps aStringto be no longer than the provided number of characters wide.
-
-
-
Method Detail
-
getMonth
@Deprecated public static String getMonth(int month)
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(java.lang.Iterable, java.lang.String, java.lang.Appendable),join(java.lang.Object[], java.lang.String)
-
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.
-
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(java.lang.Object[], java.lang.String, java.lang.Appendable),join(java.lang.Iterable, java.lang.String)
-
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.
-
countOccurrences
public static int countOccurrences(byte[] buff, int len, String word)Counts how many times a word appears in a line. Case insensitive matching.
-
countOccurrences
public static int countOccurrences(byte[] buff, String word)Counts how many times a word appears in a line. Case insensitive matching.
-
countOccurrences
public static int countOccurrences(String line, String word)
Counts how many times a word appears in a line. Case insensitive matching.
-
getTimeLengthString
public static String getTimeLengthString(long time)
-
getDecimalTimeLengthString
public static String getDecimalTimeLengthString(long time)
-
getDecimalTimeLengthString
public static String getDecimalTimeLengthString(long time, boolean alwaysShowMillis)
-
indexOf
public static int indexOf(String s, char[] chars)
Finds the first occurrence of any of the supplied characters.- Parameters:
s- theStringto searchchars- the characters to look for- Returns:
- the index of the first occurrence of
-1if none found
-
indexOf
public static int indexOf(String s, char[] chars, int start)
Finds the first occurrence of any of the supplied characters starting at the specified index.- Parameters:
s- theStringto searchchars- the characters to look forstart- the starting index.- Returns:
- the index of the first occurrence of
-1if none found
-
indexOf
public static int indexOf(String s, BitSet chars)
Finds the first occurrence of any of the supplied characters.- Parameters:
s- theStringto searchchars- the characters to look for- Returns:
- the index of the first occurrence of
-1if none found
-
indexOf
public static int indexOf(String s, BitSet chars, int start)
Finds the first occurrence of any of the supplied characters starting at the specified index.- Parameters:
s- theStringto searchchars- the characters to look forstart- the starting index.- Returns:
- the index of the first occurrence of
-1if none found
-
replace
public static String replace(String string, char ch, String replacement)
Replaces all occurrences of a character with a String. Please consider the variant with theAppendablefor higher performance.
-
replace
public static String replace(String string, String find, String replacement)
Replaces all occurrences of a String with a String. Please consider the variant with theAppendablefor 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
public static void replace(StringBuffer sb, String find, String replacement)
Replaces all occurrences of a String with a String.
-
replace
public static void replace(StringBuilder sb, String find, String replacement)
Replaces all occurrences of a String with a String.
-
splitLines
public static List<String> splitLines(String s)
Splits a String into lines on any '\n' characters. Also removes any ending '\r' characters if present
-
split
public static String[] split(String line)
Splits aStringinto aString[].- See Also:
isWhitespace(int)
-
split
@Deprecated public static int split(String line, char[][][] buff)
Deprecated.It is highly unlikely this method is still usedSplits aStringinto aString[].- See Also:
isWhitespace(int)
-
split
@Deprecated public static int split(String line, String[][] buff)
Deprecated.It is highly unlikely this method is still usedSplits aStringinto aString[].- See Also:
isWhitespace(int)
-
split
public static List<String> split(String line, char delim)
Splits a string on the given delimiter. 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, char delim, C words)
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
public static List<String> split(String line, int begin, int end, char delim)
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
-
splitCommaSpace
public static List<String> splitCommaSpace(String line)
Splits a string into multiple words on either whitespace or commas.- Returns:
- The list of non-empty strings.
- See Also:
isWhitespace(int)
-
wordWrap
@Deprecated public static String wordWrap(String string, int width)
Deprecated.Use new version with Appendable for higher performanceWord wraps aStringto be no longer than the provided number of characters wide.
-
wordWrap
public static void wordWrap(String string, int width, Appendable out) throws IOException
Word wraps aStringto 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 public static char getHexChar(int v)
Deprecated.Please useHexGets the hexadecimal character for the low-order four bits of the provided int.
-
getHex
@Deprecated public static int getHex(char ch) throws IllegalArgumentException
Deprecated.Please useHexConverts one hex digit to an integer.- Throws:
IllegalArgumentException
-
convertToHex
@Deprecated public static void convertToHex(byte[] bytes, Appendable out) throws IOException
Deprecated.Please useHex- Throws:
IOException
-
convertToHex
@Deprecated public static String convertToHex(byte[] bytes)
Deprecated.Please useHex
-
convertByteArrayFromHex
@Deprecated public static byte[] convertByteArrayFromHex(char[] hex)
Deprecated.Please useHex
-
convertToHex
@Deprecated public static void convertToHex(int value, Appendable out) throws IOException
Deprecated.Please useHexConverts an int to a full 8-character hex code.- Throws:
IOException
-
convertToHex
@Deprecated public static String convertToHex(int value)
Deprecated.Please useHexConverts an int to a full 8-character hex code.
-
convertIntArrayFromHex
@Deprecated public static int convertIntArrayFromHex(char[] hex)
Deprecated.Please useHex
-
convertToHex
@Deprecated public static void convertToHex(long value, Appendable out) throws IOException
Deprecated.Please useHexConverts a long integer to a full 16-character hex code.- Throws:
IOException
-
convertToHex
@Deprecated public static String convertToHex(long value)
Deprecated.Please useHexConverts a long integer to a full 16-character hex code.
-
convertLongArrayFromHex
@Deprecated public static long convertLongArrayFromHex(char[] hex)
Deprecated.Please useHex
-
getApproximateSize
public static String getApproximateSize(long size)
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
public static String getApproximateBitRate(long bitRate)
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
public static int compareToIgnoreCaseCarefulEquals(String s1, String s2)
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
public static int indexOf(String source, String target, int fromIndex, int toIndex)
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
public static String firstLineOnly(String value, int maxCharacters)
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
public static String nullIfEmpty(String value)
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
public static String trim(String value)
Trims a value, as per rules ofisWhitespace(int).- Returns:
- The value trimmed or
nullwhen wasnull - See Also:
String.substring(int, int)
-
trim
public static CharSequence trim(CharSequence value)
Trims a value, as per rules ofisWhitespace(int).- Returns:
- The value trimmed or
nullwhen wasnull - See Also:
CharSequence.subSequence(int, int)
-
trimNullIfEmpty
public static String trimNullIfEmpty(String value)
Trims a value, as per rules ofisWhitespace(int), returningnullif empty after trimming.
-
trimNullIfEmpty
public static CharSequence trimNullIfEmpty(CharSequence value)
Trims a value, as per rules ofisWhitespace(int), returningnullif empty after trimming.
-
-