Class StringUtil

java.lang.Object
com.github.tadukoo.util.StringUtil

public final class StringUtil extends Object
Util functions for dealing with Strings, including building and parsing them.
Since:
Pre-Alpha
Version:
Alpha v.0.3.1
Author:
Logan Ferree (Tadukoo)
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Not allowed to create a StringUtil
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    Builds a string from the given collection of Strings where each is separated by a comma.
    static String
    Builds a string from the given collection of Strings with no separator between them.
    static String
    Builds a string from the given collection of Strings where each is on a newline.
    static String
    Builds a string from the given collection of Strings with the given separator placed between them.
    static <T> List<String>
    Converts an entire Collection of items to strings, and returns them as a List.
    static String
    Converts the given Object to a String, including proper null handling.
    static boolean
    equals(String actual, String expected)
    Checks if the two strings are equal using String.equals(), but properly handles null (if they're both null, returns true, if one is null and the other isn't, return false instead of throwing an NPE).
    static boolean
    equalsAny(String actual, String... expected)
    Checks if the given string is equal to any of the given expected strings using String.equals(), but properly handles null (if the string is null and null is in the expected strings, returns true, if the string is null and the expected strings don't have null, return false instead of throwing an NPE).
    static boolean
    equalsAnyIgnoreCase(String actual, String... expected)
    Checks if the given string is equal to any of the given expected strings using String.equalsIgnoreCase(), but properly handles null (if the string is null and null is in the expected strings, returns true, if the string is null and the expected strings don't have null, return false instead of throwing an NPE).
    static boolean
    equalsIgnoreCase(String actual, String expected)
    Checks if the two strings are equal using String.equalsIgnoreCase(), but properly handles null (if they're both null, returns true, if one is null and the other isn't, return false instead of throwing an NPE).
    static boolean
    Checks if the given string is blank (either null or the empty string).
    static boolean
    Checks if the given text is camelCase or not.
    static boolean
    Checks if the given string is NOT blank (blank = either null or the empty string).
    static boolean
    Checks if the given text is PascalCase or not.
    static boolean
    Checks if the given text is snake_case or not.
    static boolean
    notEquals(String actual, String expected)
    Checks if the two strings are NOT equal using String.equals(), but properly handles null (if they're both null, returns false, if one is null and the other isn't, return true instead of throwing an NPE).
    static boolean
    notEqualsAny(String actual, String... expected)
    Checks if the given string is NOT equal to any of the given expected strings using String.equals(), but properly handles null (if the string is null and null is in the expected strings, returns false, if the string is null and the expected strings don't have null, return true instead of throwing an NPE).
    static boolean
    notEqualsAnyIgnoreCase(String actual, String... expected)
    Checks if the given string is NOT equal to any of the given expected strings using String.equalsIgnoreCase(), but properly handles null (if the string is null and null is in the expected strings, returns false, if the string is null and the expected strings don't have null, return true instead of throwing an NPE).
    static boolean
    notEqualsIgnoreCase(String actual, String expected)
    Checks if the two strings are NOT equal using String.equalsIgnoreCase(), but properly handles null (if they're both null, returns false, if one is null and the other isn't, return true instead of throwing an NPE).
    static List<String>
    Parses a comma-separated list into a List of Strings.
    static List<String>
    parseListFromStringWithSeparator(String text, String separator, boolean trim)
    Parses the given text into a List using the given separator to split and optionally trimming to remove whitespace in the resulting strings.
    static String
    Converts the given text to camelCase.
    static String
    Converts the given text to PascalCase.
    static String
    Converts the given text to snake_case.

    Methods inherited from class java.lang.Object

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

    • StringUtil

      private StringUtil()
      Not allowed to create a StringUtil
  • Method Details

    • isBlank

      public static boolean isBlank(String text)
      Checks if the given string is blank (either null or the empty string).
      Parameters:
      text - The string to check
      Returns:
      true if the string is null or the empty string
    • isNotBlank

      public static boolean isNotBlank(String text)
      Checks if the given string is NOT blank (blank = either null or the empty string).
      Parameters:
      text - The string to check
      Returns:
      true if the string is not null and not the empty string
    • equals

      public static boolean equals(String actual, String expected)
      Checks if the two strings are equal using String.equals(), but properly handles null (if they're both null, returns true, if one is null and the other isn't, return false instead of throwing an NPE).
      Parameters:
      actual - The actual string being checked
      expected - The expected string we want
      Returns:
      true if actual.equals(expected) or they're both null
    • equalsAny

      public static boolean equalsAny(String actual, String... expected)
      Checks if the given string is equal to any of the given expected strings using String.equals(), but properly handles null (if the string is null and null is in the expected strings, returns true, if the string is null and the expected strings don't have null, return false instead of throwing an NPE).
      Parameters:
      actual - The actual string being checked
      expected - The expected strings we want
      Returns:
      true if actual.equals({any of the expected}) or they're both null
    • equalsIgnoreCase

      public static boolean equalsIgnoreCase(String actual, String expected)
      Checks if the two strings are equal using String.equalsIgnoreCase(), but properly handles null (if they're both null, returns true, if one is null and the other isn't, return false instead of throwing an NPE).
      Parameters:
      actual - The actual string being checked
      expected - The expected string we want
      Returns:
      true if actual.equalsIgnoreCase(expected) or they're both null
    • equalsAnyIgnoreCase

      public static boolean equalsAnyIgnoreCase(String actual, String... expected)
      Checks if the given string is equal to any of the given expected strings using String.equalsIgnoreCase(), but properly handles null (if the string is null and null is in the expected strings, returns true, if the string is null and the expected strings don't have null, return false instead of throwing an NPE).
      Parameters:
      actual - The actual string being checked
      expected - The expected strings we want
      Returns:
      true if actual.equalsIgnoreCase({any of the expected}) or they're both null
    • notEquals

      public static boolean notEquals(String actual, String expected)
      Checks if the two strings are NOT equal using String.equals(), but properly handles null (if they're both null, returns false, if one is null and the other isn't, return true instead of throwing an NPE).
      Parameters:
      actual - The actual string being checked
      expected - The expected string we want
      Returns:
      false if actual.equals(expected) or they're both null
    • notEqualsAny

      public static boolean notEqualsAny(String actual, String... expected)
      Checks if the given string is NOT equal to any of the given expected strings using String.equals(), but properly handles null (if the string is null and null is in the expected strings, returns false, if the string is null and the expected strings don't have null, return true instead of throwing an NPE).
      Parameters:
      actual - The actual string being checked
      expected - The expected strings we want
      Returns:
      false if actual.equals({any of the expected}) or they're both null
    • notEqualsIgnoreCase

      public static boolean notEqualsIgnoreCase(String actual, String expected)
      Checks if the two strings are NOT equal using String.equalsIgnoreCase(), but properly handles null (if they're both null, returns false, if one is null and the other isn't, return true instead of throwing an NPE).
      Parameters:
      actual - The actual string being checked
      expected - The expected string we want
      Returns:
      false if actual.equalsIgnoreCase(expected) or they're both null
    • notEqualsAnyIgnoreCase

      public static boolean notEqualsAnyIgnoreCase(String actual, String... expected)
      Checks if the given string is NOT equal to any of the given expected strings using String.equalsIgnoreCase(), but properly handles null (if the string is null and null is in the expected strings, returns false, if the string is null and the expected strings don't have null, return true instead of throwing an NPE).
      Parameters:
      actual - The actual string being checked
      expected - The expected strings we want
      Returns:
      false if actual.equalsIgnoreCase({any of the expected}) or they're both null
    • convertToString

      public static String convertToString(Object obj)
      Converts the given Object to a String, including proper null handling.
      Parameters:
      obj - The Object to convert to a String
      Returns:
      null if obj is null, or a String representing the Object
    • convertCollectionToStrings

      public static <T> List<String> convertCollectionToStrings(Collection<T> items)
      Converts an entire Collection of items to strings, and returns them as a List.
      Type Parameters:
      T - The type of the items in the collection
      Parameters:
      items - The Collection of items to convert to strings
      Returns:
      A List of the given items converted to Strings
    • buildStringWithSeparator

      public static String buildStringWithSeparator(Collection<String> items, String separator)
      Builds a string from the given collection of Strings with the given separator placed between them.

      e.g. buildStringWithSeparator(Arrays.asList("foo", "bar", "baz"), " space ") would produce:
      "foo space bar space baz"
      Parameters:
      items - The collection of strings to put together
      separator - The character to put in-between the strings
      Returns:
      A String of the given collection with the separator between each of them
    • buildString

      public static String buildString(Collection<String> items)
      Builds a string from the given collection of Strings with no separator between them.
      Parameters:
      items - The strings to combine into a single String
      Returns:
      A String of all the given items
    • buildStringWithNewLines

      public static String buildStringWithNewLines(Collection<String> lines)
      Builds a string from the given collection of Strings where each is on a newline. Uses buildStringWithSeparator(java.util.Collection<java.lang.String>, java.lang.String) to accomplish this.

      e.g. buildStringWithNewLines(Arrays.asList("foo", "bar", "baz") would produce:
      "foo
      bar
      baz"
      Parameters:
      lines - The lines to make into a single string
      Returns:
      A String of the given lines each on a newline
    • buildCommaSeparatedString

      public static String buildCommaSeparatedString(Collection<String> items)
      Builds a string from the given collection of Strings where each is separated by a comma. Uses buildStringWithSeparator(java.util.Collection<java.lang.String>, java.lang.String) to accomplish this.

      e.g. buildCommaSeparatedString(Arrays.asList("foo", "bar", "baz") would produce:
      "foo,bar,baz"
      Parameters:
      items - The items to make into a single string
      Returns:
      A String of the given items as a comma-separated string
    • parseListFromStringWithSeparator

      public static List<String> parseListFromStringWithSeparator(String text, String separator, boolean trim)
      Parses the given text into a List using the given separator to split and optionally trimming to remove whitespace in the resulting strings.
      Parameters:
      text - The text to parse into a List of Strings
      separator - The separator String to use in separating the given text
      trim - Whether to trim any whitespace off the resulting strings
      Returns:
      The resulting List of Strings
    • parseCommaSeparatedListFromString

      public static List<String> parseCommaSeparatedListFromString(String text)
      Parses a comma-separated list into a List of Strings.
      Parameters:
      text - The text to convert into a List of Strings
      Returns:
      The List of Strings produced by parsing the text
    • isPascalCase

      public static boolean isPascalCase(String text)
      Checks if the given text is PascalCase or not. PascalCase has no underscores or spaces between words and every word is capitalized, including the first word.
      Parameters:
      text - The text to be checked
      Returns:
      true if the text is PascalCase, false otherwise
    • toPascalCase

      public static String toPascalCase(String text)
      Converts the given text to PascalCase. It removes any spaces and underscores and capitalizes all words in the text.
      Parameters:
      text - The text to be converted
      Returns:
      The PascalCase version of the given text
    • isCamelCase

      public static boolean isCamelCase(String text)
      Checks if the given text is camelCase or not. camelCase has no underscores or spaces between words and every word is capitalized, excluding the first word.
      Parameters:
      text - The text to be checked
      Returns:
      true if the text is camelCase, false otherwise
    • toCamelCase

      public static String toCamelCase(String text)
      Converts the given text to camelCase. It removes any spaces and underscores and capitalizes all words in the text, except for the first word, which is made lowercase.
      Parameters:
      text - The text to be converted
      Returns:
      The camelCase version of the given text
    • isSnakeCase

      public static boolean isSnakeCase(String text)
      Checks if the given text is snake_case or not. snake_case has an underscore between words, and there are no spaces.
      Parameters:
      text - The text to be checked
      Returns:
      true is the text is snake_case, false otherwise
    • toSnakeCase

      public static String toSnakeCase(String text)
      Converts the given text to snake_case. Any spaces are replaced with underscores, underscores are placed between words, and everything is made lowercase.
      Parameters:
      text - The text to be converted
      Returns:
      The snake_case version of the given text