Strings { public static String justifyCenter(String str, final int width, char padWithChar) { str = str != null ? str.trim() : ""; int addChars = width - str.length(); if (addChars < 0) { return str.subSequence(0, width).toString(); } int prependNumber = addChars / 2; int appendNumber = prependNumber; if ((prependNumber + appendNumber) != addChars) { ++prependNumber; } final StringBuilder sb = new StringBuilder(); while (prependNumber > 0) { sb.append(padWithChar); --prependNumber; } sb.append(str); while (appendNumber > 0) { sb.append(padWithChar); --appendNumber; } return sb.toString(); } private  Strings(); static Set<T> setOf(String input, Function<String, String[]> splitter, Function<String, T> factory); static List<T> listOf(String input, Function<String, String[]> splitter, Function<String, T> factory); static Set<T> setOf(String input, char delimiter, Function<String, T> factory); static Set<T> setOf(String input, Function<String, T> factory); static Set<Pattern> setOfRegex(String input, int regexFlags); static Set<Pattern> setOfRegex(String input); static List<Pattern> listOfRegex(String input, int regexFlags); static List<String> splitLines(final String content); static int compareTo(CharSequence str1, CharSequence str2); static boolean equalsIgnoreCase(String str1, String str2); static String join(CharSequence delimiter, int[] values); static String join(CharSequence delimiter, Iterable<T> values); static String join(CharSequence delimiter, Iterable<T> values, Function<T, String> conversion); static String trim(String str); static String trim(String str, CharacterPredicate predicate); static String createString(final char charToRepeat,
                                      int numberOfRepeats); static String pad(String original,
                             int length,
                             char padChar); static String setLength(String original,
                                   int length,
                                   char padChar); static String justify(Justify justify,
                                 String str,
                                 final int width,
                                 char padWithChar); static String justifyRight(String str,
                                      final int width,
                                      char padWithChar); static String justifyLeft(String str,
                                     final int width,
                                     char padWithChar); static String justifyCenter(String str,
                                       final int width,
                                       char padWithChar); static String getStackTrace(Throwable throwable); static Number asNumber(String value); static Number asNumber(String value, Supplier<Number> defaultValueProvider); static int asInt(String value, int defaultValue); static long asLong(String value, long defaultValue); static double asDouble(String value, double defaultValue); static boolean asBoolean(String value, boolean defaultValue); static Duration asDuration(String timeString); static String duration(long durationInMillis); static Function<String, String> replaceVariablesWith(Function<String, String> replacementsByVariableName); static String replaceVariables(String value, Function<String, String> replacementsByVariableName); static boolean isUuid(String str); static boolean isNullOrEmpty(String str); static boolean isNumeric(String str); static String unquoteIdentifierPart(String identifierPart); static byte[] hexStringToByteArray(String hexString); static boolean startsWithIgnoreCase(String str, String prefix); static String getBegin(String str, int length);  }
@Test public void justifyCenterShouldTruncateStringsThatAreTooLong() { assertEquals("This is the st", Strings.justifyCenter("This is the string", 14, ' ')); }
@Test public void justifyCenterShouldPrependAndAppendSameNumberOfCharacterForStringsThatAreTooShortButOfAnEvenLength() { assertEquals(" This ", Strings.justifyCenter("This", 10, ' ')); }
@Test public void justifyCenterShouldPrependOneMoreCharacterThanAppendingForStringsThatAreTooShortButOfAnOddLength() { assertEquals(" Thing ", Strings.justifyCenter("Thing", 10, ' ')); }
@Test public void justifyCenterShouldPrependCharacterForEmptyStrings() { assertEquals(" ", Strings.justifyCenter("", 10, ' ')); }
@Test public void justifyCenterShouldPrependCharacterForNullStrings() { assertEquals(" ", Strings.justifyCenter(null, 10, ' ')); }
@Test public void justifyCenterShouldReturnStringsThatAreTheDesiredLength() { assertEquals("This is the string", Strings.justifyCenter("This is the string", 18, ' ')); }