Class StringUtils

java.lang.Object
io.openems.common.utils.StringUtils

public class StringUtils extends Object
Provides static helper functions for string manipulation.
  • Constructor Details

    • StringUtils

      public StringUtils()
  • Method Details

    • toShortString

      public static String toShortString(String s, int length)
      Shortens a string to a given length.

      Example: converts a string "hello world" to "hello w..."

      Parameters:
      s - the string
      length - the target string length
      Returns:
      the shortened string
    • toShortString

      public static String toShortString(com.google.gson.JsonElement j, int length)
      Shortens a JsonElement string representation to a given length.

      Example: converts a "{ 'foo': 'bar' }" to "{ 'foo': '..."

      Parameters:
      j - the JsonElement
      length - the target string length
      Returns:
      the shortened string
    • capitalizeFirstLetter

      public static String capitalizeFirstLetter(String s)
      Convert the first letter of a string to Upper-Case.

      Example: converts "hello world" to "Hello world"

      Parameters:
      s - the string
      Returns:
      the converted string
    • matchWildcard

      public static int matchWildcard(String source, String pattern)
      Match two Strings, considering wildcards.
      • if Object.equals(Object) is true -> return 0
      • if 'pattern' matches 'source' -> return value > 1; bigger values represent a better match
      • if both Strings do not match -> return -1

      Implementation note: only one wildcard is considered. Either the entire string is "*" or the wildcard is at the beginning or at the end of the pattern String. The the JUnit test for details.

      Parameters:
      source - the String to be evaluated
      pattern - the pattern String, i.e. "meter*"
      Returns:
      an integer value representing the degree of matching
    • matchesIntegerPattern

      public static boolean matchesIntegerPattern(String string)
      Checks if the given string matches an Integer pattern, i.e. if could be parsed to Integer/Long.
      Parameters:
      string - a string
      Returns:
      true if it matches Integer
    • matchesFloatPattern

      public static boolean matchesFloatPattern(String string)
      Checks if the given string matches an Float pattern, i.e. if could be parsed to Float/Double.
      Parameters:
      string - a string
      Returns:
      true if it matches Float
    • reverse

      public static String reverse(String string)
      Causes this character sequence to be replaced by the reverse of the sequence.
      Parameters:
      string - to be reversed.
      Returns:
      reversed String.
    • containsWithNullCheck

      public static boolean containsWithNullCheck(String string, String value)
      If the given string is null return false, otherwise result of String.contains(CharSequence) is returned.
      Parameters:
      string - the string to check
      value - the sequence to search for
      Returns:
      true if string is not null and string contains value, otherwise false
    • definedOrElse

      public static String definedOrElse(String original, String alternative)
      Returns the 'alternative' if 'original' is null or blank.
      Parameters:
      original - the original value, can be null, empty or filled with white-space only
      alternative - the alternative value
      Returns:
      either the 'defined' value (not null, not empty, not only white-space), alternatively the 'orElse' value
    • parseNumberFromName

      public static OptionalInt parseNumberFromName(String name)
      Parses the number of an Edge from its name string.

      e.g. translates "edge0" to "0".

      Parameters:
      name - the edge name
      Returns:
      the number or empty optional if there is no number in the name or if the name is null