Class StringHelper


  • public class StringHelper
    extends java.lang.Object
    Helper methods related to Strings
    Author:
    Michel Kraemer
    • Constructor Summary

      Constructors 
      Constructor Description
      StringHelper()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String escapeJava​(java.lang.String s)
      Escapes characters in the given string according to Java rules
      static java.lang.String initializeName​(java.lang.String name, java.lang.String initializeWith)
      Parse the given name, split it into parts, and convert them to initials
      static java.lang.String initializeName​(java.lang.String name, java.lang.String initializeWith, boolean onlyNormalize)
      Parse the given name, split it into parts, and either convert them all to initials or only normalize existing initials
      static int overlap​(java.lang.CharSequence a, java.lang.CharSequence b)
      Calculates how many characters overlap between a and b, i.e.
      static java.lang.String sanitize​(java.lang.String s)
      Sanitizes a string so it can be used as an identifier
      static java.lang.String toTitleCase​(java.lang.String str)
      Converts the words in a given string to title case (according to the CSL specification)
      • Methods inherited from class java.lang.Object

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

      • StringHelper

        public StringHelper()
    • Method Detail

      • sanitize

        public static java.lang.String sanitize​(java.lang.String s)
        Sanitizes a string so it can be used as an identifier
        Parameters:
        s - the string to sanitize
        Returns:
        the sanitized string
      • escapeJava

        public static java.lang.String escapeJava​(java.lang.String s)
        Escapes characters in the given string according to Java rules
        Parameters:
        s - the string to escape
        Returns:
        the escpaped string
      • overlap

        public static int overlap​(java.lang.CharSequence a,
                                  java.lang.CharSequence b)

        Calculates how many characters overlap between a and b, i.e. how many characters at the end of a are equal to the ones at the beginning of b.

        Examples:

         overlap("abcd", "cdef")     = 2
         overlap("abcd", "xyz")      = 0
         overlap("a", "a")           = 1
         overlap("ab", "b")          = 1
         overlap("abcd", "bcdefg")   = 3
         overlap("", "a")            = 0
         overlap("a", "")            = 0
         
        Parameters:
        a - the first string
        b - the second string
        Returns:
        the number of overlapping characters
      • toTitleCase

        public static java.lang.String toTitleCase​(java.lang.String str)
        Converts the words in a given string to title case (according to the CSL specification)
        Parameters:
        str - the string to convert
        Returns:
        the converted string
      • initializeName

        public static java.lang.String initializeName​(java.lang.String name,
                                                      java.lang.String initializeWith)
        Parse the given name, split it into parts, and convert them to initials
        Parameters:
        name - the name to convert
        initializeWith - the string to append to each initial
        Returns:
        the converted name
      • initializeName

        public static java.lang.String initializeName​(java.lang.String name,
                                                      java.lang.String initializeWith,
                                                      boolean onlyNormalize)
        Parse the given name, split it into parts, and either convert them all to initials or only normalize existing initials
        Parameters:
        name - the name to convert
        initializeWith - the string to append to each initial
        onlyNormalize - true if only existing initials should be normalized and uninitialized names should be kept as is
        Returns:
        the converted name