String Utilities - rodrigoo-r/Harmony GitHub Wiki

Explanation

Harmony come with a String Utility and validation class. You can easily use it to validate strings.


Usage

The StringUtil class can be found at net.brydget.harmony.util.StringUtil. This class come with some methods.


Methods

  • static int distance(String s1, String s2) - Calculates the Levenshtein distance between two strings
  • static int difference(String s1, String s2) - Calculates the Levenshtein distance between two strings
  • static String capitalize(String str) - Capitalizes the first letter of the input string
  • static String unCapitalize(String str) - Returns a new string with the first letter of the input string converted to lowercase.
  • static String lowerCase - Converts the input string to lowercase
  • static String upperCase(String str) - Returns the uppercase version of the input string
  • static String trim(String str) - Trims the input string
  • static boolean strictEquals(String s1, String s2) - Check if two strings are strictly equal (Exactly the same)
  • static boolean strictMatches(String s1, String s2) - Checks if the given pattern matches the given string and the match is exactly the same as the given string
  • static boolean matches(String s1, String s2) - Checks if the given pattern has almost one match in the given string
  • and more

Example

Full Example:

public final class GreenShield extends BackendPlugin {

    @Override
    public void whenInitialize() {
        final PluginLogger logger = getOwnLogger();

        logger.info(
                StringUtil.capitalize("hello world"), // Hello world
                StringUtil.difference("hello world", "hi world"), // Will find the difference percentage
                StringUtil.distance("hello world", "hi world"), // Will find the difference percentage
        
                StringUtil.matches("hello world", "(?i)Hello"), // true
                StringUtil.strictMatches("hello world", "(?i)Hello"), // false
                // The string matches the regex, but the match isn't equal to the string
                
                StringUtil.strictEquals("hello world", "Hello world"), // false
                // The string aren't exactly equal
                
                StringUtil.trim("         hello world         "), // hello world
                StringUtil.upperCase("hello"), // HELLO
                StringUtil.lowerCase("HELLO"), // hello
                StringUtil.unCapitalize("HELLO") // hELLO
        );
    }

    @Override
    public void whenUnloaded() {
        // Plugin shutdown logic
    }
}