Here's another example. Allows you to choose how you want to treat the center when there is a different number of padding characters added to the beginning and end of the string.
Uses Java 11 String::repeat
.
public class Strings {
public enum Lean {
START,
END
}
public static String center(String value, int targetLength, Lean lean) {
return center(value, targetLength, lean, ' ');
}
private static String center(String value, int targetLength, Lean lean, char pad) {
if (targetLength < 1) {
throw new IllegalArgumentException("Cannot center something into less than one space.");
}
int sourceLength = value.length();
if (sourceLength == targetLength) {
return value;
}
int paddingToAdd = targetLength - sourceLength;
int half = paddingToAdd / 2;
String spad = Character.toString(pad);
String padding = spad.repeat(half);
String startExtra = "";
String endExtra = "";
if (paddingToAdd % 2 == 1) {
if (lean == Lean.START) {
endExtra = spad;
} else {
startExtra = spad;
}
}
return padding + startExtra + value + endExtra + padding;
}
}
public class StringsTest {
@Test
public void centerAbcIn9LeanStart() {
doTest(
"abc",
9,
Strings.Lean.START,
" abc "
);
}
@Test
public void centerAbcIn9LeanEnd() {
doTest(
"abc",
9,
Strings.Lean.END,
" abc "
);
}
@Test
public void centerAbcIn10LeanStart() {
doTest(
"abc",
10,
Strings.Lean.START,
" abc "
);
}
@Test
public void centerAbcIn10LeanEnd() {
doTest(
"abc",
10,
Strings.Lean.END,
" abc "
);
}
@Test
public void centerAbcdIn9LeanStart() {
doTest(
"abcd",
9,
Strings.Lean.START,
" abcd "
);
}
@Test
public void centerAbcdIn9LeanEnd() {
doTest(
"abcd",
9,
Strings.Lean.END,
" abcd "
);
}
@Test
public void centerAbcdIn10LeanStart() {
doTest(
"abcd",
10,
Strings.Lean.START,
" abcd "
);
}
@Test
public void centerAbcdIn10LeanEnd() {
doTest(
"abcd",
10,
Strings.Lean.START,
" abcd "
);
}
@Test
public void centerAIn1LeanStart() {
doTest(
"a",
1,
Strings.Lean.START,
"a"
);
}
@Test
public void centerAIn1LeanEnd() {
doTest(
"a",
1,
Strings.Lean.END,
"a"
);
}
@Test
public void centerAIn2LeanStart() {
doTest(
"a",
2,
Strings.Lean.START,
"a "
);
}
@Test
public void centerAIn2LeanEnd() {
doTest(
"a",
2,
Strings.Lean.END,
" a"
);
}
@Test
public void centerAIn3LeanStart() {
doTest(
"a",
3,
Strings.Lean.START,
" a "
);
}
@Test
public void centerAIn3LeanEnd() {
doTest(
"a",
3,
Strings.Lean.END,
" a "
);
}
@Test
public void centerAbIn3LeanStart() {
doTest(
"ab",
3,
Strings.Lean.START,
"ab "
);
}
@Test
public void centerAbIn3LeanEnd() {
doTest(
"ab",
3,
Strings.Lean.END,
" ab"
);
}
public void doTest(String value, int targetLength, Strings.Lean lean, String expected) {
assertThat(
"Test setup error: targetLength != expected.length()",
targetLength,
equalTo(expected.length()));
assertThat(
"Test setup error: value != expected.trim()",
value,
equalTo(expected.trim()));
String actual = Strings.center(value, targetLength, lean);
assertThat(actual, equalTo(expected));
}
}