Class StringUtil

java.lang.Object
org.omegat.util.StringUtil

public final class StringUtil extends Object
Utilities for string processing.
  • Field Details

  • Method Details

    • isEmpty

      public static boolean isEmpty(@Nullable @Nullable String str)
      Check if string is empty, i.e. null or length==0
    • isLowerCase

      public static boolean isLowerCase(String input)
      Returns true if the input has at least one letter and all letters are lower case.
    • isUpperCase

      public static boolean isUpperCase(String input)
      Returns true if the input is upper case.
    • isMixedCase

      public static boolean isMixedCase(String input)
      Returns true if the input has both upper case and lower case letters, but is not title case.
    • isTitleCase

      public static boolean isTitleCase(String input)
      Returns true if the input is title case, meaning the first character is UpperCase or TitleCase* and the rest of the string (if present) is LowerCase.

      *There are exotic characters that are neither UpperCase nor LowerCase, but are TitleCase: e.g. LATIN CAPITAL LETTER L WITH SMALL LETTER J (U+01C8)
      These are handled correctly.

    • isTitleCase

      public static boolean isTitleCase(int codePoint)
    • isWhiteSpace

      public static boolean isWhiteSpace(String input)
      Returns true if the input consists only of whitespace characters (including non-breaking characters that are false according to Character.isWhitespace(int)).
    • isWhiteSpace

      public static boolean isWhiteSpace(int codePoint)
      Returns true if the input is a whitespace character (including non-breaking characters that are false according to Character.isWhitespace(int)).
    • isCJK

      public static boolean isCJK(String input)
    • capitalizeFirst

      public static String capitalizeFirst(String text, Locale locale)
    • replaceCase

      public static String replaceCase(@NotNull @NotNull String input, Locale locale)
      Interpret the case replacement language used in regular expressions:
      • backslash u = uppercase next letter
      • backslash l = lowercase next letter
      • backslash U = uppercase next letters until backslash E or end
      • backslash L = lowercase next letters until backslash E or end
      • backslash u + backslash L = uppercase next letter then lowercase all until backslash E
      • backslash l + backslash U = lowercase next letter then uppercase all until backslash E

      Warning: This method works directly with the input string. Perform other substitutions (e.g., variable conversion) before calling this method; otherwise, unconverted substitutions will not receive proper case handling.

    • matchCapitalization

      public static String matchCapitalization(String text, String matchTo, Locale locale)
    • toTitleCase

      public static String toTitleCase(String text, Locale locale)
      Convert text to title case according to the supplied locale.
    • nvl

      @SafeVarargs public static <T> T nvl(@Nullable @Nullable T... values)
      Returns first not null object from list, or null if all values is null.
    • nvlLong

      public static long nvlLong(long... values)
      Returns first non-zero object from list, or zero if all values is null.
    • compareToWithNulls

      @Deprecated(since="6.1") public static <T extends Comparable<T>> int compareToWithNulls(T v1, T v2)
      Deprecated.
      Compares two objects of type T that may be null.
      Type Parameters:
      T - the type of objects to be compared, which must implement Comparable
      Parameters:
      v1 - the first object to compare, which may be null
      v2 - the second object to compare, which may be null
      Returns:
      a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second
    • compareToNullable

      public static <T extends Comparable<T>> int compareToNullable(T v1, T v2)
      Compares two objects of type T that may be null.

      Compares two nullable values of a type that extends Comparable. Handles null values by considering null as less than any non-null value. If both values are null, they are considered equal.

      Type Parameters:
      T - The type of the values being compared, which must implement Comparable.
      Parameters:
      v1 - The first value to compare. May be null.
      v2 - The second value to compare. May be null.
      Returns:
      A negative integer, zero, or a positive integer if v1 is less than, equal to, or greater than v2, respectively.
    • firstN

      public static String firstN(String str, int len)
      Extracts first N codepoints from string.
    • truncate

      public static String truncate(String text, int len)
      Truncate the supplied text to a maximum of len codepoints. If truncated, the result will be the first (len - 1) codepoints plus a trailing ellipsis.
      Parameters:
      text - The text to truncate
      len - The desired length (in codepoints) of the result
      Returns:
      The truncated string
    • getFirstLetterLowercase

      public static int getFirstLetterLowercase(String s)
      Returns first letter in lowercase. Usually used for create tag shortcuts.
    • isSubstringAfter

      public static boolean isSubstringAfter(String text, int pos, String substring)
      Checks if text contains substring after specified position.
    • isSubstringBefore

      public static boolean isSubstringBefore(String text, int pos, String substring)
      Checks if text contains substring before specified position.
    • stripFromEnd

      public static String stripFromEnd(String string, String... toStrip)
    • normalizeUnicode

      public static String normalizeUnicode(CharSequence text)
      Apply Unicode NFC normalization to a string.
    • removeXMLInvalidChars

      public static String removeXMLInvalidChars(String str)
      Replace invalid XML chars by spaces.
      Parameters:
      str - input stream
      Returns:
      result stream
      See Also:
    • isValidXMLChar

      public static boolean isValidXMLChar(int codePoint)
      Determines whether the provided code point is a valid XML character.
      Parameters:
      codePoint - the code point to validate
      Returns:
      true if the code point is a valid XML character, otherwise false
    • makeValidXML

      public static String makeValidXML(String plaintext)
      Converts a stream of plaintext into valid XML. Output stream must convert stream to UTF-8 when saving to disk.
    • compressSpaces

      public static String compressSpaces(String str)
      Compresses spaces in case of non-preformatting paragraph.
    • escapeXMLChars

      public static String escapeXMLChars(int cp)
    • unescapeXMLEntities

      public static String unescapeXMLEntities(String text)
      Converts XML entities to characters.
    • equal

      public static boolean equal(@Nullable @Nullable String one, @Nullable @Nullable String two)
      Compares two strings for equality. Handles nulls: if both strings are nulls they are considered equal.
    • format

      public static String format(@NotNull @NotNull String str, Object... arguments)
      Formats UI strings.

      Note: This is only a first attempt at putting right what goes wrong in MessageFormat. Currently it only duplicates single quotes, but it doesn't even test if the string contains parameters (numbers in curly braces), and it doesn't allow for string containg already escaped quotes.

      Parameters:
      str - The string to format
      arguments - Arguments to use in formatting the string
      Returns:
      The formatted string
    • normalizeWidth

      public static String normalizeWidth(String text)
      Normalizes the width of characters in the given text to ensure consistency in character forms.

      The normalization applies specific transformations based on character types:

      • ASCII characters are converted to their halfwidth forms.
      • Katakana characters are converted to their fullwidth forms.
      • Hangul characters are converted to their fullwidth forms.
      • Letter-like symbols and squared Latin abbreviations are decomposed into their ASCII equivalents.
      The concept of halfwidth and fullwidth forms is explained in more detail in the Wikipedia article.

      This method improves modularity and efficiency by replacing the large switch-case structure used in the original implementation with a table-based look-up approach.

      Originally adapted from FullWidthConversionStep.java in the Okapi Framework licensed under GPLv2+.

      Parameters:
      text - the input text to normalize
      Returns:
      the text with normalized-width characters
    • rstrip

      public static String rstrip(@NotNull @NotNull String text)
      Strip whitespace from the end of a string. Uses Character.isWhitespace(int), so it does not strip the extra non-breaking whitespace included in isWhiteSpace(int).
      Parameters:
      text - the text to strip
      Returns:
      text with trailing whitespace removed
    • encodeBase64

      public static String encodeBase64(String string, Charset charset)
      Convert a string's charset bytes into a Base64-encoded String.
      Parameters:
      string - a string
      charset - the charset with which to obtain the bytes
      Returns:
      Base64-encoded String
    • encodeBase64

      public static String encodeBase64(char[] chars, Charset charset)
      Convert a char array's charset bytes into a Base64-encoded String. Useful for handling passwords. Intermediate buffers are cleared after use.
      Parameters:
      chars - a char array
      charset - the charset with which to obtain the bytes
      Returns:
      Base64-encoded String
    • decodeBase64

      public static String decodeBase64(String b64data, Charset charset)
      Decode the Base64-encoded charset bytes back to a String.
      Parameters:
      b64data - Base64-encoded String
      charset - charset of decoded bytes
      Returns:
      String
    • getTailSegments

      public static String getTailSegments(@NotNull @NotNull String str, int separator, int segments)
      For a string delimited by some separator, retrieve the last segments segments.
      Parameters:
      str - The string
      separator - The separator delimiting the string's segments
      segments - The number of segments to return, starting at the end
      Returns:
      The trailing segments, or, if segments is greater than the number of segments contained in str, then str itself.
    • convertToList

      public static List<String> convertToList(String str)
      For a string containing a space-separated list of items, convert that string into an ArrayList
      Parameters:
      str - The string, with items separated by whitespace
      Returns:
      An ArrayList of the items in the original space-separated list
    • wrap

      public static String wrap(@NotNull @NotNull String text, int length)
      Wrap line by length.
      Parameters:
      text - string to process.
      length - wrap length.
      Returns:
      string wrapped.
    • unescapeLinefeed

      public static String unescapeLinefeed(String s)