Package dev.davwheat

Class StringTools


  • public class StringTools
    extends java.lang.Object
    A collection of utilities for modifying Strings.
    • Constructor Summary

      Constructors 
      Constructor Description
      StringTools()  
    • Method Summary

      Modifier and Type Method Description
      static java.lang.String centreText​(java.lang.String text, int width)
      Takes a string as input, and a maximum length, then centres the text within that length.
      static java.util.ArrayList<java.lang.String> centreText​(java.util.List<java.lang.String> text, int width)
      Takes a list of strings as input, and a maximum length, then centres the text within that length.
      static java.lang.String concatenateListOfLines​(java.util.List<java.lang.String> lines)
      Joins an ArrayList of Strings into a single string, separated by new lines.
      static java.util.List<java.lang.String> splitStringAtWhitespace​(java.lang.String text, int width)
      Takes a string, and wraps it to the maximum length provided, while adhering to whitespace location where possible.
      • Methods inherited from class java.lang.Object

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

      • StringTools

        public StringTools()
    • Method Detail

      • centreText

        public static java.util.ArrayList<java.lang.String> centreText​(java.util.List<java.lang.String> text,
                                                                       int width)
        Takes a list of strings as input, and a maximum length, then centres the text within that length.

        If there is an odd amount of spacing required, we favour extra spacing on the left.

        Parameters:
        text - input string to be centred
        width - the maximum length per line
        Returns:
        centred, possibly multi-lined, string
      • centreText

        public static java.lang.String centreText​(java.lang.String text,
                                                  int width)
        Takes a string as input, and a maximum length, then centres the text within that length.

        If there is an odd amount of spacing required, we favour extra spacing on the left.

        If the input is longer than the width, nothing will be done.

        Parameters:
        text - input string to be centred
        width - the maximum length per line
        Returns:
        centred string
      • concatenateListOfLines

        public static java.lang.String concatenateListOfLines​(java.util.List<java.lang.String> lines)
        Joins an ArrayList of Strings into a single string, separated by new lines.

        Preserves all whitespace on each line.

        Parameters:
        lines - list of strings
        Returns:
        new-line delimited string
      • splitStringAtWhitespace

        public static java.util.List<java.lang.String> splitStringAtWhitespace​(java.lang.String text,
                                                                               int width)
        Takes a string, and wraps it to the maximum length provided, while adhering to whitespace location where possible.
        Parameters:
        text - Input
        width - Maximum length per line
        Returns:
        Wrapped string