Class StringUtils

java.lang.Object
com.inductiveautomation.snap.util.StringUtils

public class StringUtils extends Object
This class is a collection of convenient static String utils.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    add(String s1, String s2)
    Adds the two strings together (treats nulls as empty strings).
    static String
    add(String s1, String s2, String aSeparator)
    Adds the two strings together with separator (treats nulls as empty strings, omitting sep if either is null).
    static int
    addWords(String aString, int aStart, int anEnd, StringBuffer aBuffer, Matcher aMatcher)
    Adds words from given string and string start/end to given string buffer.
    static boolean
    containsIC(CharSequence aString1, CharSequence aString2)
    Returns whether a given string contains a given string, ignoring case.
    static boolean
    containsIC(String[] theStrings, String aString)
    Returns whether the given string array contains the given string, ignoring case.
    static String
    delete(String aString, String aSearch)
    Returns the result of deleting from the given string any occurrence of the search string.
    static String
    deleteIC(String aString, String aSearch)
    Returns the result of deleting from the given string any occurrence of the search string (ignores case).
    static double
    Returns an double value by parsing the given string.
    static double
    doubleValue(String aString, int aStart)
    Returns an double value by parsing the given string starting at the given index.
    static boolean
    Returns whether s1 ends with s2, ignoring case.
    static boolean
    endsWithIC(String s1, String... strings)
    Returns whether s1 ends with any of the given strings, ignoring case.
    static boolean
    Returns whether s1 equals s2, ignoring case.
    static boolean
    equalsIC(String s1, String... strings)
    Returns whether s1 equals any of the given strings, ignoring case.
    static String
    Returns the given string with the first char demoted to lowercase.
    static String
    Returns the given string with the first char promoted to uppercase.
    static float
    floatValue(String aString)
    Returns an float value by parsing the given string.
    static String
    Returns a spaced string from a camel case string.
    static byte[]
    getBytes(String aString)
    Returns the ASCII bytes of the given string (ISO-Latin).
    static byte[]
    getBytes(String aString, String enc)
    Returns the bytes of the given string in the requested char encoding.
    static String
    getISOLatinString(byte[] bytes)
    Returns a string from the given ASCII bytes.
    static String
    getISOLatinString(byte[] bytes, int offset, int length)
    Returns a string from the given ASCII bytes (from offset to offset+length).
    static String
    getPathChild(String aPath, String aChildPath)
    Returns a path with a filename or relative path added.
    static String
    Returns the extension of the given string path (everything after last '.').
    static String
    Returns the file name component of the given string path (everything after last file separator).
    static int
    Returns the index to the first character of the filename component of the given path.
    static String
    Returns the simple file name for a given path (just the file name without extension).
    static String
    Returns the given string path minus the file name component (everything after last file separator).
    static String
    getPathPeer(String aPath, String aName)
    Returns a peer path for given path and new filename by stripping filename from given path and using given name.
    static String
    Returns the given string path minus any extension.
    static String
    getPathSister(String aPath, String anExtension)
    Returns a sister path for given path and new extension by stripping extension from path and using given extension.
    static String
    Returns the path in a standard, normal format (strips any trailing file separators).
    static String
    Returns a stack trace string for given exception.
    static String
    getString(byte[] theBytes)
    Returns a string for given bytes.
    static String
    getString(byte[] theBytes, String anEncoding)
    Returns a string for given bytes.
    static String
    Returns a quoted string.
    static String
    Returns a single quoted string.
    static String
    getStringSurrounded(String aString, String aSurroundString, String anEscapeString)
    Returns a string surrounded by given string.
    static int
    Returns the index of search string s2 in given string s1, ignores case.
    static int
    indexOfIC(CharSequence s1, CharSequence s2, int start)
    Returns the index of search string s2 in given string s1, ignores case and starts at start char index.
    static int
    indexOfIC(String[] theStrings, String aString)
    Returns the index of a given string in the given array, ignoring case.
    static int
    intValue(String aString)
    Returns an int value by parsing the given string.
    static boolean
    isEmpty(String aString)
    Returns whether string is null or empty.
    static String
    join(Object[] parts, String separator)
    Allows you to take an array of strings and concatenate them together with a separator
    static String
    join(Object[] parts, String separator, int aStart, int anEnd)
    Allows you to take an array of strings and concatenate them together with a separator
    static int
    Returns the length of given string (supports null).
    static long
    longValue(String aString)
    Returns an int value by parsing the given string.
    static long
    longValue(String aString, int aStart)
    Returns an double value by parsing the given string starting at the given index.
    static String
    String promotion - returns either the given string or empty string (if given string is null).
    static String
    String demotion - returns either the given string or null (if given string length is zero).
    static String
    nextInSequence(String source, String suffix)
    Turns a string like "myFile" into "myFile-1", and a string like "myFile-2" to "myFile-3", given suffix like "-".
    static String
    or(String s1, String s2)
    Returns the first non-null string of the two given strings (or null).
    static String
    replace(String s, int start, int end, String withString)
    Returns the result of replacing in the given string the char range with the with-string.
    static String
    replace(String aString, String search, char replace)
    Returns the result of replacing in the given string any occurrence of the search string with the replace-char.
    static String
    replace(String aString, String search, String replace)
    Returns the result of replacing in the given string any occurrence of the search string with the replace-string.
    static String
    replaceIC(String aString, String search, String replace)
    Returns the result of replacing in given string any occurrence of search string with replace-string (ignore case).
    static List<String>
    separate(String aString, String aSeparator)
    Returns a list of parts of given string separated by the given delimiter.
    static List<String>
    separate(String aString, String aSeparator, boolean doTrim)
    Returns a list of parts of given string separated by the given delimiter, with option to trim space.
    static boolean
    Returns whether s1 starts with s2, ignoring case.
    static boolean
    startsWithIC(String s1, String... strings)
    Returns whether s1 starts with any of the given strings, ignoring case.
    static String
    Returns a camel cased string from a string with non-alphanumeric chars.
    static String
    toString(double aValue)
    Returns a string representation of the given float to (at most) 3 significant digits.
    static String
    trimEnd(String aString)
    Trims the end of a string.
    static String
    wrap(String aString, int aLimit)
    Returns a string wrapped on word boundaries.
    static String
    wrap(String aString, int aLimit, String aSeperator, String aJoiner)
    Returns a string wrapped on word boundaries as defined by regex string (eg., whitespace="\\s*", dot="\\.", etc.).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • StringUtils

      public StringUtils()
  • Method Details

    • isEmpty

      public static boolean isEmpty(String aString)
      Returns whether string is null or empty.
    • length

      public static int length(CharSequence aString)
      Returns the length of given string (supports null).
    • add

      public static String add(String s1, String s2)
      Adds the two strings together (treats nulls as empty strings).
    • add

      public static String add(String s1, String s2, String aSeparator)
      Adds the two strings together with separator (treats nulls as empty strings, omitting sep if either is null).
    • min

      public static String min(String s)
      String demotion - returns either the given string or null (if given string length is zero).
    • max

      public static String max(String s)
      String promotion - returns either the given string or empty string (if given string is null).
    • or

      public static String or(String s1, String s2)
      Returns the first non-null string of the two given strings (or null).
    • toString

      public static String toString(double aValue)
      Returns a string representation of the given float to (at most) 3 significant digits.
    • getPathFileName

      public static String getPathFileName(String aPath)
      Returns the file name component of the given string path (everything after last file separator).
    • getPathFileNameSimple

      public static String getPathFileNameSimple(String aPath)
      Returns the simple file name for a given path (just the file name without extension).
    • getPathFileNameIndex

      public static int getPathFileNameIndex(String aPath)
      Returns the index to the first character of the filename component of the given path.
    • getPathExtension

      public static String getPathExtension(String aPath)
      Returns the extension of the given string path (everything after last '.').
    • getPathSimple

      public static String getPathSimple(String aPath)
      Returns the given string path minus any extension.
    • getPathParent

      public static String getPathParent(String aPath)
      Returns the given string path minus the file name component (everything after last file separator).
    • getPathChild

      public static String getPathChild(String aPath, String aChildPath)
      Returns a path with a filename or relative path added.
    • getPathPeer

      public static String getPathPeer(String aPath, String aName)
      Returns a peer path for given path and new filename by stripping filename from given path and using given name.
    • getPathSister

      public static String getPathSister(String aPath, String anExtension)
      Returns a sister path for given path and new extension by stripping extension from path and using given extension.
    • getPathStandardized

      public static String getPathStandardized(String aPath)
      Returns the path in a standard, normal format (strips any trailing file separators).
    • trimEnd

      public static String trimEnd(String aString)
      Trims the end of a string.
    • delete

      public static String delete(String aString, String aSearch)
      Returns the result of deleting from the given string any occurrence of the search string.
    • deleteIC

      public static String deleteIC(String aString, String aSearch)
      Returns the result of deleting from the given string any occurrence of the search string (ignores case).
    • replace

      public static String replace(String s, int start, int end, String withString)
      Returns the result of replacing in the given string the char range with the with-string.
    • replace

      public static String replace(String aString, String search, char replace)
      Returns the result of replacing in the given string any occurrence of the search string with the replace-char.
    • replace

      public static String replace(String aString, String search, String replace)
      Returns the result of replacing in the given string any occurrence of the search string with the replace-string.
    • replaceIC

      public static String replaceIC(String aString, String search, String replace)
      Returns the result of replacing in given string any occurrence of search string with replace-string (ignore case).
    • separate

      public static List<String> separate(String aString, String aSeparator)
      Returns a list of parts of given string separated by the given delimiter.
    • separate

      public static List<String> separate(String aString, String aSeparator, boolean doTrim)
      Returns a list of parts of given string separated by the given delimiter, with option to trim space.
    • intValue

      public static int intValue(String aString)
      Returns an int value by parsing the given string.
    • longValue

      public static long longValue(String aString)
      Returns an int value by parsing the given string.
    • longValue

      public static long longValue(String aString, int aStart)
      Returns an double value by parsing the given string starting at the given index.
    • floatValue

      public static float floatValue(String aString)
      Returns an float value by parsing the given string.
    • doubleValue

      public static double doubleValue(String aString)
      Returns an double value by parsing the given string.
    • doubleValue

      public static double doubleValue(String aString, int aStart)
      Returns an double value by parsing the given string starting at the given index.
    • getBytes

      public static byte[] getBytes(String aString)
      Returns the ASCII bytes of the given string (ISO-Latin).
    • getBytes

      public static byte[] getBytes(String aString, String enc)
      Returns the bytes of the given string in the requested char encoding.
    • getString

      public static String getString(byte[] theBytes)
      Returns a string for given bytes.
    • getString

      public static String getString(byte[] theBytes, String anEncoding)
      Returns a string for given bytes.
    • getISOLatinString

      public static String getISOLatinString(byte[] bytes)
      Returns a string from the given ASCII bytes.
    • getISOLatinString

      public static String getISOLatinString(byte[] bytes, int offset, int length)
      Returns a string from the given ASCII bytes (from offset to offset+length).
    • indexOfIC

      public static int indexOfIC(CharSequence s1, CharSequence s2)
      Returns the index of search string s2 in given string s1, ignores case.
    • indexOfIC

      public static int indexOfIC(CharSequence s1, CharSequence s2, int start)
      Returns the index of search string s2 in given string s1, ignores case and starts at start char index.
    • containsIC

      public static boolean containsIC(CharSequence aString1, CharSequence aString2)
      Returns whether a given string contains a given string, ignoring case.
    • equalsIC

      public static boolean equalsIC(String s1, String s2)
      Returns whether s1 equals s2, ignoring case.
    • equalsIC

      public static boolean equalsIC(String s1, String... strings)
      Returns whether s1 equals any of the given strings, ignoring case.
    • endsWithIC

      public static boolean endsWithIC(String s1, String s2)
      Returns whether s1 ends with s2, ignoring case.
    • endsWithIC

      public static boolean endsWithIC(String s1, String... strings)
      Returns whether s1 ends with any of the given strings, ignoring case.
    • startsWithIC

      public static boolean startsWithIC(String s1, String s2)
      Returns whether s1 starts with s2, ignoring case.
    • startsWithIC

      public static boolean startsWithIC(String s1, String... strings)
      Returns whether s1 starts with any of the given strings, ignoring case.
    • firstCharUpperCase

      public static String firstCharUpperCase(String aString)
      Returns the given string with the first char promoted to uppercase.
    • firstCharLowerCase

      public static String firstCharLowerCase(String aString)
      Returns the given string with the first char demoted to lowercase.
    • fromCamelCase

      public static String fromCamelCase(String aString)
      Returns a spaced string from a camel case string.
    • toCamelCase

      public static String toCamelCase(String aString)
      Returns a camel cased string from a string with non-alphanumeric chars.
    • wrap

      public static String wrap(String aString, int aLimit)
      Returns a string wrapped on word boundaries.
    • wrap

      public static String wrap(String aString, int aLimit, String aSeperator, String aJoiner)
      Returns a string wrapped on word boundaries as defined by regex string (eg., whitespace="\\s*", dot="\\.", etc.).
    • addWords

      public static int addWords(String aString, int aStart, int anEnd, StringBuffer aBuffer, Matcher aMatcher)
      Adds words from given string and string start/end to given string buffer.
    • join

      public static String join(Object[] parts, String separator)
      Allows you to take an array of strings and concatenate them together with a separator
      Parameters:
      parts - an array of Strings
      separator - a string to place between elements
    • join

      public static String join(Object[] parts, String separator, int aStart, int anEnd)
      Allows you to take an array of strings and concatenate them together with a separator
      Parameters:
      parts - an array of Strings
      separator - a string to place between elements
    • nextInSequence

      public static String nextInSequence(String source, String suffix)
      Turns a string like "myFile" into "myFile-1", and a string like "myFile-2" to "myFile-3", given suffix like "-".
    • indexOfIC

      public static int indexOfIC(String[] theStrings, String aString)
      Returns the index of a given string in the given array, ignoring case.
    • containsIC

      public static boolean containsIC(String[] theStrings, String aString)
      Returns whether the given string array contains the given string, ignoring case.
    • getStringQuoted

      public static String getStringQuoted(String aString)
      Returns a quoted string.
    • getStringSingleQuoted

      public static String getStringSingleQuoted(String aString)
      Returns a single quoted string.
    • getStringSurrounded

      public static String getStringSurrounded(String aString, String aSurroundString, String anEscapeString)
      Returns a string surrounded by given string.
    • getStackTraceString

      public static String getStackTraceString(Throwable aThrowable)
      Returns a stack trace string for given exception.