Class StringUtils

java.lang.Object
org.deltava.util.StringUtils

public final class StringUtils extends Object
A String utility class.
Since:
1.0
Version:
10.3
Author:
Luke
  • Method Details

    • isEmpty

      public static boolean isEmpty(CharSequence s)
      A null-safe mechanism to check if a String is null or empty.
      Parameters:
      s - the String to check
      Returns:
      TRUE if s is null or empty, otherwise FALSE
    • removeCSVQuotes

      public static String removeCSVQuotes(String s)
      Removes leading and trailing double-quotes from a CSV string etntity. This will only remove a trailing quote if a leading quote is present.
      Parameters:
      s - the string
      Returns:
      the string minus any leading/trailing quotes
    • escapeQuotes

      public static String escapeQuotes(String s)
      Escapes single and double quotes in a string with a leading backslash.
      Parameters:
      s - the string
      Returns:
      the escaped string
      Throws:
      NullPointerException - if s is null
    • properCase

      public static String properCase(String s)
      Converts a string to "proper case".
      Parameters:
      s - the string to convert
      Returns:
      the converted string
      Throws:
      NullPointerException - if the string is null
    • filter

      public static String filter(String s, Predicate<Character> p)
      Filters a string by passing in a character filtering Predicate.
      Parameters:
      s - the string
      p - the Predicate
      Returns:
      the filtered string, or null
    • stripInlineHTML

      public static String stripInlineHTML(String s)
      Strips out inline HTML in a string by replacing < and > with < and >.
      Parameters:
      s - the string to Convert
      Returns:
      the formatted string, null if s is null
    • listConcat

      public static String listConcat(Collection<?> values, String delim)
      Concatenates a collection of Strings into a single delimited String.
      Parameters:
      values - the List of values
      delim - the value delimiter
      Returns:
      the delimited value stirng
    • getPropertyMethod

      public static String getPropertyMethod(String pName)
      Returns the method name used to retrieve a particular property value. This conforms to the Java Bean specification by prepending the string "get" to the property name, and converting the first character of the property name to uppercase.
      Parameters:
      pName - the property name
      Returns:
      the name of the get method for this property
    • parseHex

      public static int parseHex(String hexValue)
      Parses a string that may be in hexadecimal. If the string starts with "0x", it is treated as a hex string, otherwise it is parsed as a regular number.
      Parameters:
      hexValue - the number to parse
      Returns:
      the value
      Throws:
      NumberFormatException - if hexValue cannot be converted to a number
    • parse

      public static int parse(String value, int defaultValue)
      Parses a string and converts into a 32-bit integer.
      Parameters:
      value - the value to convert
      defaultValue - the default value
      Returns:
      the value parsed into a number, or the default value if conversion fails
    • parse

      public static long parse(String value, long defaultValue, boolean isUnsigned)
      Parses a string and converts into a 64-bit integer.
      Parameters:
      value - the value to convert
      defaultValue - the default value
      isUnsigned - TRUE to treat as an unsigned integer, otherwise FALSE
      Returns:
      the value parsed into a number, or the default value if conversion fails
    • parse

      public static double parse(String value, double defaultValue)
      Parses a string and converts into a floating-point number.
      Parameters:
      value - the value to convert
      defaultValue - the default value
      Returns:
      the value parsed into a number, or the default value if conversion fails
    • parseInstant

      public static Instant parseInstant(String dt, String fmt)
      Parses a date using a given format pattern.
      Parameters:
      dt - the date to parse
      fmt - the format pattern
      Returns:
      the parsed Date
      See Also:
    • parseLocal

      public static ZonedDateTime parseLocal(String dt, String fmt, ZoneId tz)
      Parses a date using a given format pattern.
      Parameters:
      dt - the date to parse
      fmt - the format pattern
      tz - the ZoneId
      Returns:
      the parsed Date
      See Also:
    • parseEpoch

      public static Instant parseEpoch(String dt)
      Parses an epoch Date.
      Parameters:
      dt - the date in milliseconds since Epoch
      Returns:
      a Date, or null
    • parseRFC3339Date

      public static Instant parseRFC3339Date(String dt)
      Helper method to parse RFC3339 date strings.
      Parameters:
      dt - the date/time string
      Returns:
      a Date
      Throws:
      IllegalArgumentException - if the date/time is not in RFC3339 format
    • formatHex

      public static String formatHex(long value)
      Converts a number to a hexadecimal value by prepending "0x" and converting to hexadecimal.
      Parameters:
      value - the number to convert
      Returns:
      the hexadecimal value
      See Also:
    • arrayIndexOf

      public static int arrayIndexOf(String[] values, String value)
      Returns the index of a value within an array of Strings.
      Parameters:
      values - the array of Strings
      value - the String to search for
      Returns:
      the offset of the value in the array, or -1 if not found
    • arrayIndexOf

      public static int arrayIndexOf(String[] values, String value, int defaultValue)
      Returns the index of a value within an array of Strings.
      Parameters:
      values - the array of Strings
      value - the String to search for
      defaultValue - the default value to return if not found
      Returns:
      the offset of the value in the array, or defaultValue if not found
    • split

      public static List<String> split(String s, String delim)
      Splits a delimited String into a List of elements.
      Parameters:
      s - the String
      delim - the delimiter
      Returns:
      a List of String
      See Also:
    • format

      public static String format(GeoLocation loc, boolean asHTML, int formatMask)
      Formats a Latitude/Longitude pair.
      Parameters:
      loc - the location
      asHTML - TRUE if HTML formatting to be used, otherwise FALSE
      formatMask - bitmask specifying the portion to format
      Returns:
      a formatted latitude/longitude
      See Also:
    • format

      public static String format(long value, String fmtPattern)
      Formats an integer into a string using a particular pattern.
      Parameters:
      value - the number to format
      fmtPattern - the formatter pattern
      Returns:
      the formatted number
      See Also:
    • format

      public static String format(double value, String fmtPattern)
      Formats a floating-point number into a string using a particular pattern.
      Parameters:
      value - the number to format
      fmtPattern - the formatter pattern
      Returns:
      the formatted number
      See Also:
    • format

      public static String format(Instant dt, String fmtPattern)
      Formats a date/time into a string using a particular pattern.
      Parameters:
      dt - the date/time
      fmtPattern - the formatter pattern
      Returns:
      the formatted date/time
    • format

      public static String format(LocalDate d, String fmtPattern)
      Formats a date into a string using a particular pattern.
      Parameters:
      d - the date
      fmtPattern - the formatter pattern
      Returns:
      the formatted date
    • format

      public static String format(ZonedDateTime dt, String fmtPattern)
      Formats a date/time into a string using a particular pattern.
      Parameters:
      dt - the date/time
      fmtPattern - the formatter pattern
      Returns:
      the formatted date/time
    • strip

      public static String strip(CharSequence s, String chars)
      Strips out specific charaters from a string.
      Parameters:
      s - the input string
      chars - the characters to remove
      Returns:
      the string with no characters specified in chars
    • nullTrim

      public static List<String> nullTrim(Collection<String> s)
      Trims a Collection of Strings, removing elements if the string is empty.
      Parameters:
      s - the Collection of Strings to trim
      Returns:
      a List of Strings
    • getURLParameters

      public static Map<String,String> getURLParameters(String url)
      Returns the query parameters on a URL.
      Parameters:
      url - the URL
      Returns:
      a Map of parameters and values
    • parseCSV

      public static CSVTokens parseCSV(String data)
      Parses a line of CSV-fomratted text.
      Parameters:
      data - the text
      Returns:
      a CSVTokens object