Class StringUtils

java.lang.Object
org.kingdoms.utils.string.StringUtils

public final class StringUtils extends Object
A string util class extending Apache's org.apache.commons.lang.StringUtils
  • Field Details

  • Constructor Details

    • StringUtils

      public StringUtils()
  • Method Details

    • random

      public static String random(int minLength, int maxLength, String characters)
    • findCapitalized

      public static String findCapitalized(String str)
    • parseInt

      public static Integer parseInt(org.bukkit.entity.Player player, String str)
    • parseDouble

      public static Double parseDouble(org.bukkit.entity.Player player, String str)
    • capitalize

      @Nullable public static String capitalize(@Nullable String str)
      Capitalizes the first letter of a string and lowercase the other letters. Apache's org.apache.commons.lang.StringUtils#capitalize(String) doesn't lowercase the other half.
      Parameters:
      str - the string to capitalize.
      Returns:
      a capitalized word.
      Since:
      1.0.0
    • contains

      public static boolean contains(String str, char searchChar)

      Checks if String contains a search character, handling null. This method uses String.indexOf(int).

      A null or empty ("") String will return false.

       StringUtils.contains(null, *)    = false
       StringUtils.contains("", *)      = false
       StringUtils.contains("abc", 'a') = true
       StringUtils.contains("abc", 'z') = false
       
      Parameters:
      str - the String to check, may be null
      searchChar - the character to find
      Returns:
      true if the String contains the search character, false if not or null string input
      Since:
      2.0
    • splitByLength

      public static List<String> splitByLength(List<String> strings, int length)
    • configOption

      public static String configOption(@Nullable Enum<?> enumeral)
    • configOptionToEnum

      public static String configOptionToEnum(@Nullable String str)
    • configOption

      public static String configOption(@Nullable String str)
    • logComponents

      public static void logComponents(net.md_5.bungee.api.chat.BaseComponent... components)
    • findSimilar

      public static String findSimilar(String name, Collection<String> names)
    • areElementsEmpty

      public static boolean areElementsEmpty(Collection<String> strings)
    • repeat

      public static String repeat(String str, int times)
    • repeat

      public static String repeat(char ch, int times)
    • containsWhitespace

      public static boolean containsWhitespace(String str)
    • toLatinLowerCase

      @Nullable public static String toLatinLowerCase(@Nullable String str)
    • replaceOnce

      public static String replaceOnce(String text, char search, String replacement)
    • replace

      public static CharSequence replace(String text, char search, String replacement)
    • replace

      public static CharSequence replace(String text, char search, char replacement)
    • generatedToString

      public static String generatedToString(Object instance)
    • toLatinUpperCase

      @Nullable public static String toLatinUpperCase(@Nullable String str)
    • upperCaseReplaceChar

      public static String upperCaseReplaceChar(@Nullable String str, char variable, char replacement)
    • join

      public static String join(Object[] array, String separator)

      Joins the elements of the provided array into a single String containing the provided list of elements.

      No delimiter is added before or after the list. A null separator is the same as an empty String (""). Null objects or empty strings within the array are represented by empty strings.

       StringUtils.join(null, *)                = null
       StringUtils.join([], *)                  = ""
       StringUtils.join([null], *)              = ""
       StringUtils.join(["a", "b", "c"], "--")  = "a--b--c"
       StringUtils.join(["a", "b", "c"], null)  = "abc"
       StringUtils.join(["a", "b", "c"], "")    = "abc"
       StringUtils.join([null, "", "a"], ',')   = ",,a"
       
      Parameters:
      array - the array of values to join together, may be null
      separator - the separator character to use, null treated as ""
      Returns:
      the joined String, null if null array input
    • join

      public static String join(Object[] array, String separator, int startIndex, int endIndex)

      Joins the elements of the provided array into a single String containing the provided list of elements.

      No delimiter is added before or after the list. A null separator is the same as an empty String (""). Null objects or empty strings within the array are represented by empty strings.

       StringUtils.join(null, *)                = null
       StringUtils.join([], *)                  = ""
       StringUtils.join([null], *)              = ""
       StringUtils.join(["a", "b", "c"], "--")  = "a--b--c"
       StringUtils.join(["a", "b", "c"], null)  = "abc"
       StringUtils.join(["a", "b", "c"], "")    = "abc"
       StringUtils.join([null, "", "a"], ',')   = ",,a"
       
      Parameters:
      array - the array of values to join together, may be null
      separator - the separator character to use, null treated as ""
      startIndex - the first index to start joining from. It is an error to pass in an end index past the end of the array
      endIndex - the index to stop joining from (exclusive). It is an error to pass in an end index past the end of the array
      Returns:
      the joined String, null if null array input
    • lowerCaseReplaceChar

      public static String lowerCaseReplaceChar(@Nullable String str, char variable, char replacement)
    • isEnglish

      public static boolean isEnglish(@Nullable CharSequence str)
      Checks if every single character in a string is English. English characters consists of whitespaces, alphabets and numbers Characters such as !@#$%^&*()_+| will not be accepted.
      Parameters:
      str - the string to check.
      Returns:
      true if the whole string is in English, otherwise false.
      Since:
      1.0.0
      See Also:
    • validateURL

      public static URL validateURL(String str)
    • hasSymbol

      public static boolean hasSymbol(@Nullable CharSequence str)
    • isEnglishLetter

      public static boolean isEnglishLetter(char ch)
    • isEnglishDigit

      public static boolean isEnglishDigit(char ch)
    • cleanSplit

      @Nonnull public static List<String> cleanSplit(@Nonnull String str, char separator)
    • cleanSplitManaged

      @Nonnull public static List<String> cleanSplitManaged(@Nonnull String str, char separator)
    • deleteWhitespace

      @Nullable public static String deleteWhitespace(@Nullable String str)
    • join

      public static CharSequence join(char delimiter, CharSequence... elements)
    • join

      public static String join(String delimiter, Collection<String> elements, com.google.common.base.Function<String,String> decorator)
    • join

      public static String join(String delimiter, Collection<String> elements, com.google.common.base.Function<String,String> decorator, String empty)
    • toOrdinalNumeral

      public static String toOrdinalNumeral(int num)
    • split

      @Nonnull public static List<String> split(@Nonnull String str, char separatorChar, boolean preserveAllTokens)
    • advancedSplit

      @Nonnull public static List<StringUtils.SplitInfo> advancedSplit(@Nonnull String str, char separatorChar, boolean preserveAllTokens)
    • splitLocation

      public static String[] splitLocation(@Nonnull String str, int size)
    • isEnglishLetterOrDigit

      public static boolean isEnglishLetterOrDigit(char ch)
    • stacktrace

      public static String stacktrace()
    • printStackTrace

      public static void printStackTrace()
    • printStackTrace

      public static void printStackTrace(StackTraceElement[] stackTraceElements, int skip)
    • isCalledFromClass

      public static boolean isCalledFromClass(Class<?> clazz)
    • fakeBool

      public static boolean fakeBool(String e)
    • containsNumber

      public static boolean containsNumber(@Nullable CharSequence str)
      Checks if the given string contains a number.
      Parameters:
      str - the string to check the numbers.
      Returns:
      true if the string contains any number, otherwise false.
      Since:
      1.0.0
      See Also:
    • containsAnyLangNumber

      public static boolean containsAnyLangNumber(@Nullable CharSequence str)
    • getImageSize

      @Nullable public static Dimension getImageSize(@Nonnull URL url)
    • isNumeric

      public static boolean isNumeric(@Nullable String str)
    • associatedArrayMap

      public static <K, V> StringBuilder associatedArrayMap(Map<K,V> map)
    • spaces

      public static String spaces(int times)
    • isPureNumber

      public static boolean isPureNumber(@Nullable String str)
    • indexOfAny

      public static int indexOfAny(String str, String[] searchStrs)

      Find the first index of any of a set of potential substrings.

      A null String will return -1. A null or zero length search array will return -1. A null search array entry will be ignored, but a search array containing "" will return 0 if str is not null. This method uses String.indexOf(String).

       StringUtils.indexOfAny(null, *)                     = -1
       StringUtils.indexOfAny(*, null)                     = -1
       StringUtils.indexOfAny(*, [])                       = -1
       StringUtils.indexOfAny("zzabyycdxx", ["ab","cd"])   = 2
       StringUtils.indexOfAny("zzabyycdxx", ["cd","ab"])   = 2
       StringUtils.indexOfAny("zzabyycdxx", ["mn","op"])   = -1
       StringUtils.indexOfAny("zzabyycdxx", ["zab","aby"]) = 1
       StringUtils.indexOfAny("zzabyycdxx", [""])          = 0
       StringUtils.indexOfAny("", [""])                    = 0
       StringUtils.indexOfAny("", ["a"])                   = -1
       
      Parameters:
      str - the String to check, may be null
      searchStrs - the Strings to search for, may be null
      Returns:
      the first index of any of the searchStrs in str, -1 if no match
    • getGroupedOption

      @Nonnull public static String getGroupedOption(@Nonnull String option, int... grouped)
      Translates an enum to a config option with grouped key sections using dots.

      Examples

               getGroupedOption("HELLO_WORLD");
               // Output: hello-world
      
               getGroupedOption("WHATS_UP_WORLD", 1);
               // Output: whats: up-world:
      
               getGroupedOption("GOODBYE_CRUEL_WORLD", 1, 2);
               // Output: goodbye: cruel: world:
           
      Parameters:
      grouped - the groups index (will be replaced with "_").
      Returns:
      the config option.
      Since:
      1.0.0
    • splitArray

      public static String[] splitArray(String str, char separatorChar)
    • splitArray

      public static String[] splitArray(String str, char separatorChar, boolean preserveAllTokens)
    • reverse

      public static String reverse(String input)
    • isOneOf

      public static boolean isOneOf(@Nullable String str, @Nonnull String... strings)
      Checks if the given string is equal to any of the other strings.
      Parameters:
      str - the original string.
      strings - the other strings to check.
      Returns:
      true if the original string is equal to one of the other strings, otherwise false.
      Since:
      1.0.0
    • toFancyNumber

      @Nonnull public static String toFancyNumber(double number)
    • printConfig

      public static void printConfig(org.bukkit.configuration.ConfigurationSection section)
    • printConfig

      public static void printConfig(org.bukkit.configuration.ConfigurationSection section, String nestLevel)
    • printConfig

      public static void printConfig(ConfigSection section)
    • remove

      @Nullable public static String remove(@Nullable String str, char remove)
    • remove

      public static String remove(String str, String remove)

      Removes all occurrences of a substring from within the source string.

      A null source string will return null. An empty ("") source string will return the empty string. A null remove string will return the source string. An empty ("") remove string will return the source string.

       StringUtils.remove(null, *)        = null
       StringUtils.remove("", *)          = ""
       StringUtils.remove(*, null)        = *
       StringUtils.remove(*, "")          = *
       StringUtils.remove("queued", "ue") = "qd"
       StringUtils.remove("queued", "zz") = "queued"
       
      Parameters:
      str - the source String to search, may be null
      remove - the String to search for and remove, may be null
      Returns:
      the substring with the string removed if found, null if null String input
      Since:
      2.1
    • replaceOnce

      public static String replaceOnce(String text, String searchString, String replacement)

      Replaces a String with another String inside a larger String, once.

      A null reference passed to this method is a no-op.

       StringUtils.replaceOnce(null, *, *)        = null
       StringUtils.replaceOnce("", *, *)          = ""
       StringUtils.replaceOnce("any", null, *)    = "any"
       StringUtils.replaceOnce("any", *, null)    = "any"
       StringUtils.replaceOnce("any", "", *)      = "any"
       StringUtils.replaceOnce("aba", "a", null)  = "aba"
       StringUtils.replaceOnce("aba", "a", "")    = "ba"
       StringUtils.replaceOnce("aba", "a", "z")   = "zba"
       
      Parameters:
      text - text to search and replace in, may be null
      searchString - the String to search for, may be null
      replacement - the String to replace with, may be null
      Returns:
      the text with any replacements processed, null if null String input
      See Also:
    • replace

      public static String replace(String text, String searchString, String replacement)

      Replaces all occurrences of a String within another String.

      A null reference passed to this method is a no-op.

       StringUtils.replace(null, *, *)        = null
       StringUtils.replace("", *, *)          = ""
       StringUtils.replace("any", null, *)    = "any"
       StringUtils.replace("any", *, null)    = "any"
       StringUtils.replace("any", "", *)      = "any"
       StringUtils.replace("aba", "a", null)  = "aba"
       StringUtils.replace("aba", "a", "")    = "b"
       StringUtils.replace("aba", "a", "z")   = "zbz"
       
      Parameters:
      text - text to search and replace in, may be null
      searchString - the String to search for, may be null
      replacement - the String to replace it with, may be null
      Returns:
      the text with any replacements processed, null if null String input
      See Also:
    • replace

      public static String replace(String text, String searchString, String replacement, int max)

      Replaces a String with another String inside a larger String, for the first max values of the search String.

      A null reference passed to this method is a no-op.

       StringUtils.replace(null, *, *, *)         = null
       StringUtils.replace("", *, *, *)           = ""
       StringUtils.replace("any", null, *, *)     = "any"
       StringUtils.replace("any", *, null, *)     = "any"
       StringUtils.replace("any", "", *, *)       = "any"
       StringUtils.replace("any", *, *, 0)        = "any"
       StringUtils.replace("abaa", "a", null, -1) = "abaa"
       StringUtils.replace("abaa", "a", "", -1)   = "b"
       StringUtils.replace("abaa", "a", "z", 0)   = "abaa"
       StringUtils.replace("abaa", "a", "z", 1)   = "zbaa"
       StringUtils.replace("abaa", "a", "z", 2)   = "zbza"
       StringUtils.replace("abaa", "a", "z", -1)  = "zbzz"
       
      Parameters:
      text - text to search and replace in, may be null
      searchString - the String to search for, may be null
      replacement - the String to replace it with, may be null
      max - maximum number of values to replace, or -1 if no maximum
      Returns:
      the text with any replacements processed, null if null String input
    • containsAny

      public static boolean containsAny(@Nullable String str, @Nonnull String... strings)
      Checks if the given string contains any of the other strings.
      Parameters:
      str - the original string
      strings - the other strings to check
      Returns:
      true if the original string contains one of the other strings, otherwise false.
      Since:
      1.0.0
    • buildArguments

      public static String buildArguments(@Nonnull String[] args, @Nonnull String joinStr, int from)
      Converts all the arguments to a single string from the start index.

      Examples

               String args = {"hello", "hi", "goodbye"};
               buildArguments(args, ", ", 0);
               // Output: hello, hi, goodbye
      
               buildArguments(args, "", 1);
               // Output: higoodbye
           
      Parameters:
      args - the arguments to convert to string.
      joinStr - the string to put between each argument when converting to string.
      from - the index where it should start converting from.
      Returns:
      a converted string.
      Since:
      1.0.0
    • performCommands

      public static void performCommands(org.bukkit.OfflinePlayer offlinePlayer, Collection<String> commands)
    • buildArguments

      public static String buildArguments(@Nonnull String[] args, @Nonnull String joinStr)
    • buildArguments

      public static String buildArguments(@Nonnull String[] args, int from)
    • buildArguments

      public static String buildArguments(@Nonnull String[] args)