Package fmpp.util

Class StringUtil

java.lang.Object
fmpp.util.StringUtil

public class StringUtil extends Object
Collection of string manipulation functions.
  • Field Details

    • LINE_BREAK

      public static final String LINE_BREAK
      The default line-break string used by the methods in this class.
  • Constructor Details

    • StringUtil

      public StringUtil()
  • Method Details

    • htmlEnc

      public static String htmlEnc(String s)
      HTML encoding (does not convert line breaks). Replaces all '>' '<' '&' and '"' with entity reference
    • xmlEnc

      public static String xmlEnc(String s)
      XML Encoding. Replaces all '>' '<' '&', "'" and '"' with entity reference
    • xmlEncNQ

      public static String xmlEncNQ(String s)
      XML encoding without replacing apostrophes and quotation marks.
      See Also:
    • rtfEnc

      public static String rtfEnc(String s)
      Rich Text Format encoding (does not replace line breaks). Escapes all '\' '{' '}' and '"'
    • jQuoteOrName

      public static String jQuoteOrName(char c)
      Quotes character as Java language character, except quote characters, which are referred with name.
    • jQuote

      public static String jQuote(char c)
      Quotes string as Java language character.
    • jQuote

      public static String jQuote(String s)
      Quotes string as Java language string literal.
    • ftlStringLiteralDec

      public static String ftlStringLiteralDec(String s) throws StringUtil.ParseException
      FTL string literal decoding. \\, \", \', \n, \t, \r, \b and \f will be replaced according to Java rules. In additional, it knows \g, \l, \a and \{ which are replaced with <, >, & and { respectively. \x works as hexadecimal character code escape. The character codes are interpreted according to UCS basic plane (Unicode). "f\x006Fo", "f\x06Fo" and "f\x6Fo" will be "foo". "f\x006F123" will be "foo123" as the maximum number of digits is 4. All other \X (where X is any character not mentioned above or End-of-string) will cause a StringUtil.ParseException.
      Parameters:
      s - String literal without the surrounding quotation marks
      Returns:
      String with all escape sequences resolved
      Throws:
      StringUtil.ParseException - if there string contains illegal escapes
    • stringToPerl5Regex

      public static String stringToPerl5Regex(String text)
      Convers string to Perl 5 regular expression. This means that regular expression metacharacters will be escaped.
    • split

      public static String[] split(String s, char c)
    • split

      public static String[] split(String s, char c, boolean trim)
      Splits a string at the specified character, and optionally trims the items.
    • split

      public static String[] split(String s, String sep)
      Splits a string at the specified string.
    • splitAtWS

      public static String[] splitAtWS(String text)
      Splits a string at white-spaces. A continous sequence of one or more white-space is considered as a single separator. If the string is starting or ending with a separator, then that separator is silently ignored. Thus, the result array contains only trimmed non-0-length strings.
    • replace

      public static String replace(String text, String oldsub, String newsub)
      Replaces all occurances of a sub-string in a string.
      Parameters:
      text - The string where it will replace oldsub with newsub.
      Returns:
      String The string after the replacements.
    • expandTabs

      public static String expandTabs(String text, int tabWidth)
      Same as expandTabs(text, tabWidth, 0).
      See Also:
    • expandTabs

      public static String expandTabs(String text, int tabWidth, int startCol)
      Replaces all occurances of character tab with spaces.
      Parameters:
      tabWidth - the distance of tab stops.
      startCol - the index of the column in which the first character of the string is from the left edge of the page. The index of the first column is 0.
      Returns:
      String The string after the replacements.
    • chomp

      public static String chomp(String s)
      Removes the line-break from the end of the string.
    • chomp

      public static void chomp(StringBuffer sb)
      Removes the line-break from the end of the StringBuffer.
    • urlEnc

      public static String urlEnc(String s, String enc) throws UnsupportedEncodingException
      URL encoding (like%20this).
      Throws:
      UnsupportedEncodingException
    • urlPathEnc

      public static String urlPathEnc(String s, String enc) throws UnsupportedEncodingException
      URL encoding without escaping slashes.
      Throws:
      UnsupportedEncodingException
    • wrap

      public static String wrap(String text, int screenWidth)
      Hard-wraps flow-text. This is a convenience method that equivalent with wrap(text, screenWidth, 0, 0, LINE_BREAK, false).
      See Also:
    • wrapTrace

      public static String wrapTrace(String text, int screenWidth)
      Hard-wraps flow-text. This is a convenience method that equivalent with wrap(text, screenWidth, 0, 0, LINE_BREAK, true).
      See Also:
    • wrap

      public static String wrap(String text, int screenWidth, String lineBreak)
      Hard-wraps flow-text. This is a convenience method that equivalent with wrap(text, screenWidth, 0, 0, lineBreak, false).
      See Also:
    • wrap

      public static String wrap(String text, int screenWidth, int indent)
      Hard-wraps flow-text. This is a convenience method that equivalent with wrap(text, screenWidth, indent, indent, LINE_BREAK, false).
      See Also:
    • wrap

      public static String wrap(String text, int screenWidth, int firstIndent, int indent)
      Hard-wraps flow-text. This is a convenience method that equivalent with wrap(text, screenWidth, firstIndent, indent, LINE_BREAK, false).
      See Also:
    • wrap

      public static String wrap(String text, int screenWidth, int indent, String lineBreak)
      Hard-wraps flow-text. This is a convenience method that equivalent with wrap(text, screenWidth, indent, indent, lineBreak, false).
      See Also:
    • wrap

      public static String wrap(String text, int screenWidth, int firstIndent, int indent, String lineBreak)
      Hard-wraps flow-text. This is a convenience method that equivalent with wrap(text, screenWidth, firstIndent, indent, lineBreak, false).
      See Also:
    • wrap

      public static StringBuffer wrap(StringBuffer text, int screenWidth, int firstIndent, int indent)
      Hard-wraps flow-text. Uses StringBuffer-s instead of String-s. This is a convenience method that equivalent with wrap(text, screenWidth, firstIndent, indent, LINE_BREAK).
      See Also:
    • wrap

      public static String wrap(String text, int screenWidth, int firstIndent, int indent, String lineBreak, boolean traceMode)
      Hard-wraps flow-text.
      Parameters:
      text - The flow-text to wrap. The explicit line-breaks of the source text will be kept. All types of line-breaks (UN*X, Mac, DOS/Win) are understood.
      screenWidth - The (minimum) width of the screen. It does not utilize the screenWidth-th column of the screen to store characters, except line-breaks (because some terminals/editors do an automatic line-break when you write visible character there, and some doesn't... so it is unpredictable if an explicit line-break is needed or not.).
      firstIndent - The indentation of the first line
      indent - The indentation of all lines but the first line
      lineBreak - The String used for line-breaks
      traceMode - Set this true if the input text is a Java stack trace. In this mode, all lines starting with optional indentation + 'at' + space are treated as location lines, and will be indented and wrapped in a slightly special way.
      Throws:
      IllegalArgumentException - if the number of columns remaining for the text is less than 2.
    • wrap

      public static StringBuffer wrap(StringBuffer text, int screenWidth, int firstIndent, int indent, String lineBreak, boolean traceMode)
      Hard-wraps flow-text. Uses StringBuffer-s instead of String-s. This is the method that is internally used by all other wrap variations, so if you are working with StringBuffers anyway, it gives better performance.
      See Also:
    • createSourceCodeErrorMessage

      public static String createSourceCodeErrorMessage(String message, String srcCode, int position, String fileName, int maxQuotLength)
    • stringToBigDecimal

      public static BigDecimal stringToBigDecimal(String s) throws StringUtil.ParseException
      Converts a string to BigDecimal.
      Throws:
      StringUtil.ParseException
    • stringToBoolean

      public static boolean stringToBoolean(String s) throws StringUtil.ParseException
      Throws:
      StringUtil.ParseException
    • stringToDate

      public static freemarker.template.TemplateDateModel stringToDate(String s, TimeZone tz) throws StringUtil.ParseException
      Parses a date of format "yyyy-MM-dd" or "yyyy-MM-dd z" and returns it as TemplateDateModel.
      Throws:
      StringUtil.ParseException
    • stringToTime

      public static freemarker.template.TemplateDateModel stringToTime(String s, TimeZone tz) throws StringUtil.ParseException
      Parses a time of format "H:mm:ss" or "h:mm:ss a" or "H:mm:ss z" or "h:mm:ss a z" and returns it as TemplateDateModel.
      Throws:
      StringUtil.ParseException
    • stringToDateTime

      public static freemarker.template.TemplateDateModel stringToDateTime(String s, TimeZone tz) throws StringUtil.ParseException
      Parses a date-time of format "yyyy-MM-dd H:mm:ss" or "yyyy-MM-dd h:mm:ss a" or "yyyy-MM-dd H:mm:ss z" or "yyyy-MM-dd h:mm:ss a z" and returns it as TemplateDateModel.
      Throws:
      StringUtil.ParseException
    • normalizeLinebreaks

      public static String normalizeLinebreaks(String s)
      Converts all line-breaks to UN*X linebreaks ("\n"). The input text can contain UN*X, DOS (Windows) and Mac linebreaks mixed.
    • repeat

      public static String repeat(String s, int n)
      Since:
      0.9.15
    • capitalizeFirst

      public static String capitalizeFirst(String s)
      Since:
      0.9.15