Class StringUtils

java.lang.Object
com.tridion.util.StringUtils

public final class StringUtils extends Object
Some handy utils for string processing.
  • Field Details

    • PATH_SEPARATOR

      @Deprecated public static final String PATH_SEPARATOR
      Deprecated.
      The file (path) separator character for this platform (OS).
    • NEWLINE_CHARACTER

      public static final String NEWLINE_CHARACTER
      The line separator character (new line) for this platform (OS).
  • Method Details

    • depthBelowCurrentPath

      public static int depthBelowCurrentPath(String basePath, String comparePath, String separator)
      Compares two paths (case-insensitive), and returns whether one path is a subdirectory or not, and if so, what depth this subdirectory is at. For example, depthBelowCurrentPath("/root/", "/root/SG1/", "/") will return 1 ("/root/SG1/" is 1 directory below "/root/") For example, depthBelowCurrentPath("/root/SG1/", "/root/", "/") will return -1 ("/root/" is not below directory "/root/SG1/") Also, depthBelowCurrentPath("/root/", "/root/", "/") will return 0 ("/root/" the same as "/root/") And depthBelowCurrentPath("/root/", "/toor/", "/") will return -1 (the directories are not the same)
      Parameters:
      basePath - The path you want to compare
      comparePath - The path you want to compare against
      separator - The path separator. Normally, this will be "/"
      Returns:
      An int, representing either the depth below the current directory, or -1 if the directory is not a subdirectory, or the same directory
    • replace

      public static String replace(String someString, String matchString, String replacement)
      Replaces all occurrences of a particular substring in a string and replaces them with a different string.
      Parameters:
      someString - The String to replace the occurrences in.
      matchString - The substring you want to replace.
      replacement - The String you want to replace the matchChar character with.
      Returns:
      A String with all of the occurrences of the substring replaced.
    • replace

      public static String replace(String someString, char matchChar, String replacement)
      Replaces all occurrences of a character in a string and replaces them with a different string.
      Parameters:
      someString - The String to replace the occurrences in.
      matchChar - The character you want to replace.
      replacement - The String you want to replace the matchChar character with.
      Returns:
      A String with all of the occurrences of the character replaced.
    • joinHostStringAndPath

      public static String joinHostStringAndPath(String hostString, String path)
      Joins the host part of a URL to a path part of a URL, ensuring that we don't have too many slashes.
      Parameters:
      hostString - The host part of a URL, e.g. http://www.thiswebsite.com:8080
      path - The path part of a URL, e.g. /firstpath/
      Returns:
      The full URL, eg. http://www.thiswebsite.com:8080/firstpath/
    • getFullURLString

      public static String getFullURLString(String protocol, String domain, int port, String path)
      Takes all the component parts needed to generate a valid URL, and returns the valid URL. So the call
       String hostString = StringUtils.getFullURLString("http", "www.mywebsite.com", 8080, "/pages")
       
      returns http://www.website.com:8080/pages.
      Parameters:
      protocol - The protocol to use. If no protocol is supplied, "http" is presumed.
      domain - The domain to use, may be null.
      port - The port to use. If port is 80 and protocol is "http", port is not included in the URL.
      path - The path to use, may be null.
      Returns:
      A valid URL, or null if both domain and path are null.
    • joinMetaPaths

      public static String joinMetaPaths(String dataPath, String metaPath, String urlPath)
      Joins three strings to make something which could be the file location of a URL path.
      Parameters:
      dataPath - Location where metadata is stored
      metaPath - Metadata string you want to insert
      urlPath - URL of item you want to append
      Returns:
      A path which should represent the location of the URL metadata file for use by the findByURL methods.
    • normalizeString

      public static String normalizeString(String string)
      Normalizes the specified string by removing leading and trailing whitespace characters. A null is replaced with an empty string.
      Parameters:
      string - the string to normalize; may be null.
      Returns:
      The normalized string.
    • isEmpty

      @Deprecated public static boolean isEmpty(String str)
      Deprecated.
    • isNotEmpty

      @Deprecated public static boolean isNotEmpty(String... strings)
      Deprecated.
    • stringEquals

      public static boolean stringEquals(String str1, String str2)
    • stringEqualsIgnoreCase

      public static boolean stringEqualsIgnoreCase(String str1, String str2)
    • trimIfNotNull

      public static String trimIfNotNull(String string)
    • stringArrayContains

      public static boolean stringArrayContains(String[] array, String contains, boolean caseSensitive)
    • safeSubstring

      public static String safeSubstring(String source, int beginIndex) throws ParseException
      Throws:
      ParseException
    • safeSubstring

      public static String safeSubstring(String source, int beginIndex, int endIndex) throws ParseException
      Throws:
      ParseException
    • removeCharAt

      public static String removeCharAt(String s, int pos)
    • removeQuotations

      public static String removeQuotations(String s)
    • addQuotations

      public static String addQuotations(String s)
    • parseLong

      public static Long parseLong(String s)
    • splitString

      public static Set<String> splitString(String input, String delimiter)
      Helper method to transform single string which contains multiple strings delimited with delimiter into set of strings.
      Parameters:
      input - The String which contains multiple string values
      delimiter - The delimiter used to split the strings
      Returns:
      Resolved Set of strings