From 25cf44496068157db723452bafb5c0f6570aedb7 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Tue, 30 May 2023 11:20:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20fillBefore=20=E5=92=8C=20f?= =?UTF-8?q?illAfter=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/xyz/zhouxy/plusone/commons/util/StrUtil.java | 10 +++++----- .../xyz/zhouxy/plusone/commons/util/StrUtilTests.java | 4 ++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/StrUtil.java b/src/main/java/xyz/zhouxy/plusone/commons/util/StrUtil.java index ca45407..b84f3a4 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/StrUtil.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/StrUtil.java @@ -9,22 +9,22 @@ public class StrUtil { return src; } char[] result = new char[minLength]; - Arrays.fill(result, c); + Arrays.fill(result, 0, minLength - src.length(), c); for (int i = 1; i <= src.length(); i++) { result[minLength - i] = src.charAt(src.length() - i); } return String.valueOf(result); } - public static String fillAfter(String src, int length, char c) { - if (src.length() >= length) { + public static String fillAfter(String src, int minLength, char c) { + if (src.length() >= minLength) { return src; } - char[] result = new char[length]; - Arrays.fill(result, c); + char[] result = new char[minLength]; for (int i = 0; i < src.length(); i++) { result[i] = src.charAt(i); } + Arrays.fill(result, src.length(), result.length, c); return String.valueOf(result); } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/util/StrUtilTests.java b/src/test/java/xyz/zhouxy/plusone/commons/util/StrUtilTests.java index 7096657..2185544 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/util/StrUtilTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/util/StrUtilTests.java @@ -11,12 +11,16 @@ class StrUtilTests { @Test void testFillZero() { char c = '='; + log.info(StrUtil.fillBefore("1", 6, c)); + log.info(StrUtil.fillBefore("12", 6, c)); log.info(StrUtil.fillBefore("123", 6, c)); log.info(StrUtil.fillBefore("1234", 6, c)); log.info(StrUtil.fillBefore("12345", 6, c)); log.info(StrUtil.fillBefore("123456", 6, c)); log.info(StrUtil.fillBefore("1234567", 6, c)); log.info(StrUtil.fillBefore("12345678", 6, c)); + log.info(StrUtil.fillAfter("1", 6, c)); + log.info(StrUtil.fillAfter("12", 6, c)); log.info(StrUtil.fillAfter("123", 6, c)); log.info(StrUtil.fillAfter("1234", 6, c)); log.info(StrUtil.fillAfter("12345", 6, c));