Class Strings

java.lang.Object
com.aoapps.lang.Strings

public final class Strings extends Object
Author:
AO Industries, Inc.
  • Method Details

    • 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

      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

      public static boolean containsIgnoreCase(String line, String word)
    • 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 - the String to search
      chars - the characters to look for
      Returns:
      the index of the first occurrence of -1 if 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 - the String to search
      chars - the characters to look for
      start - the starting index.
      Returns:
      the index of the first occurrence of -1 if none found
    • indexOf

      public static int indexOf(String s, BitSet chars)
      Finds the first occurrence of any of the supplied characters.
      Parameters:
      s - the String to search
      chars - the characters to look for
      Returns:
      the index of the first occurrence of -1 if 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 - the String to search
      chars - the characters to look for
      start - the starting index.
      Returns:
      the index of the first occurrence of -1 if 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 the Appendable for 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 the Appendable 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 to out.
      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 to out.
      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 to out.
      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 to out.
      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 a String into a String[].
      See Also:
    • split

      @Deprecated public static int split(String line, char[][][] buff)
      Deprecated.
      It is highly unlikely this method is still used
      Splits a String into a String[].
      See Also:
    • split

      @Deprecated public static int split(String line, String[][] buff)
      Deprecated.
      It is highly unlikely this method is still used
      Splits a String into a String[].
      See Also:
    • 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
    • split

      public static List<String> split(String line, String delim)
    • 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:
    • wordWrap

      @Deprecated public static String wordWrap(String string, int width)
      Deprecated.
      Use new version with Appendable for higher performance
      Word wraps a String to 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 a String 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 public static char getHexChar(int v)
      Deprecated.
      Please use Hex
      Gets 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 use Hex
      Converts one hex digit to an integer.
      Throws:
      IllegalArgumentException
    • convertToHex

      @Deprecated public static void convertToHex(byte[] bytes, Appendable out) throws IOException
      Deprecated.
      Please use Hex
      Throws:
      IOException
    • convertToHex

      @Deprecated public static String convertToHex(byte[] bytes)
      Deprecated.
      Please use Hex
    • convertByteArrayFromHex

      @Deprecated public static byte[] convertByteArrayFromHex(char[] hex)
      Deprecated.
      Please use Hex
    • convertToHex

      @Deprecated public static void convertToHex(int value, Appendable out) throws IOException
      Deprecated.
      Please use Hex
      Converts an int to a full 8-character hex code.
      Throws:
      IOException
    • convertToHex

      @Deprecated public static String convertToHex(int value)
      Deprecated.
      Please use Hex
      Converts an int to a full 8-character hex code.
    • convertIntArrayFromHex

      @Deprecated public static int convertIntArrayFromHex(char[] hex)
      Deprecated.
      Please use Hex
    • convertToHex

      @Deprecated public static void convertToHex(long value, Appendable out) throws IOException
      Deprecated.
      Please use Hex
      Converts a long integer to a full 16-character hex code.
      Throws:
      IOException
    • convertToHex

      @Deprecated public static String convertToHex(long value)
      Deprecated.
      Please use Hex
      Converts a long integer to a full 16-character hex code.
    • convertLongArrayFromHex

      @Deprecated public static long convertLongArrayFromHex(char[] hex)
      Deprecated.
      Please use Hex
    • 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 with String.trim()) or matches Character.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 with String.trim()) or matches Character.isWhitespace(int).
    • trim

      public static String trim(String value)
      Trims a value, as per rules of isWhitespace(int).
      Returns:
      The value trimmed or null when was null
      See Also:
    • trim

      public static CharSequence trim(CharSequence value)
      Trims a value, as per rules of isWhitespace(int).
      Returns:
      The value trimmed or null when was null
      See Also:
    • trimNullIfEmpty

      public static String trimNullIfEmpty(String value)
      Trims a value, as per rules of isWhitespace(int), returning null if empty after trimming.
    • trimNullIfEmpty

      public static CharSequence trimNullIfEmpty(CharSequence value)
      Trims a value, as per rules of isWhitespace(int), returning null if empty after trimming.