mirror of
https://github.com/danieldemus/openhab-core.git
synced 2025-01-25 11:45:49 +01:00
Extend Stringutils with padRight (#4279)
* Add padRight * Fix edge cases Signed-off-by: Leo Siepel <leosiepel@gmail.com>
This commit is contained in:
parent
f150e8dd50
commit
93b53e7847
@ -15,6 +15,7 @@ package org.openhab.core.util;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
@ -207,8 +208,29 @@ public class StringUtils {
|
||||
* @return the padded String
|
||||
*/
|
||||
public static String padLeft(@Nullable String str, int minSize, String padString) {
|
||||
String paddedString = str == null ? "" : str;
|
||||
return String.format("%" + minSize + "s", paddedString).replace(" ", padString);
|
||||
String paddedString = Objects.requireNonNullElse(str, "");
|
||||
return paddedString.length() >= minSize ? paddedString
|
||||
: padString.repeat(minSize - paddedString.length()) + paddedString;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pads the string from the right
|
||||
*
|
||||
* <pre>
|
||||
* padRight("9", 4, "0") => "9000"
|
||||
* padRight("3112", 12, "*") => "3112********"
|
||||
* padRight("openHAB", 4, "*") => "openHAB"
|
||||
* </pre>
|
||||
*
|
||||
* @param str the String to pad, may be null
|
||||
* @param minSize the minimum String size to return
|
||||
* @param padString the String to add when padding
|
||||
* @return the padded String
|
||||
*/
|
||||
public static String padRight(@Nullable String str, int minSize, String padString) {
|
||||
String paddedString = Objects.requireNonNullElse(str, "");
|
||||
return (paddedString.length() >= minSize) ? paddedString
|
||||
: paddedString + padString.repeat(minSize - paddedString.length());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -102,6 +102,18 @@ public class StringUtilsTest {
|
||||
assertEquals("AAAAAAp3RF@CT", StringUtils.padLeft("p3RF@CT", 13, "A"));
|
||||
assertEquals("nopaddingshouldhappen", StringUtils.padLeft("nopaddingshouldhappen", 21, "x"));
|
||||
assertEquals("LongerStringThenMinSize", StringUtils.padLeft("LongerStringThenMinSize", 10, "x"));
|
||||
assertEquals("xxxhas space", StringUtils.padLeft("has space", 12, "x"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void padRight() {
|
||||
assertEquals("000000", StringUtils.padRight("", 6, "0"));
|
||||
assertEquals("000000", StringUtils.padRight(null, 6, "0"));
|
||||
assertEquals("teststr000", StringUtils.padRight("teststr", 10, "0"));
|
||||
assertEquals("p3RF@CTAAAAAA", StringUtils.padRight("p3RF@CT", 13, "A"));
|
||||
assertEquals("nopaddingshouldhappen", StringUtils.padRight("nopaddingshouldhappen", 21, "x"));
|
||||
assertEquals("LongerStringThenMinSize", StringUtils.padRight("LongerStringThenMinSize", 10, "x"));
|
||||
assertEquals("has spacexxx", StringUtils.padRight("has space", 12, "x"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
Reference in New Issue
Block a user