From 32d51fdd6148e4ccc1955d7cfebb0cf7f461cee0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=B9=E9=B9=8F=E9=A3=9E?= Date: Fri, 8 Aug 2025 14:03:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/nflg/wms/admin/SapMetaPrintTest.java | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/nflg-wms-admin/src/test/java/com/nflg/wms/admin/SapMetaPrintTest.java b/nflg-wms-admin/src/test/java/com/nflg/wms/admin/SapMetaPrintTest.java index 4555c3ed..dbba94dc 100644 --- a/nflg-wms-admin/src/test/java/com/nflg/wms/admin/SapMetaPrintTest.java +++ b/nflg-wms-admin/src/test/java/com/nflg/wms/admin/SapMetaPrintTest.java @@ -117,12 +117,12 @@ public class SapMetaPrintTest { // 计算每列最大宽度 int[] maxWidths = new int[headers.size()]; for (int i = 0; i < headers.size(); i++) { - maxWidths[i] = headers.get(i).length(); + maxWidths[i] = getWeightedLength(headers.get(i)); } for (String[] row : rows) { for (int i = 0; i < row.length && i < maxWidths.length; i++) { - maxWidths[i] = Math.max(maxWidths[i], row[i].length()); + maxWidths[i] = Math.max(maxWidths[i], getWeightedLength(row[i])); } } @@ -152,9 +152,29 @@ public class SapMetaPrintTest { System.out.print("|"); for (int i = 0; i < widths.length; i++) { String value = i < row.length ? row[i] : ""; - System.out.printf(" %-" + widths[i] + "s |", value); + System.out.printf(" %-" + (widths[i] - getChineseNum(value) / 2) + "s |", value); } System.out.println(); } + + private static final String CHINESE_REGEX = "[\\u4e00-\\u9fff]"; + + public static int getWeightedLength(String str) { + if (str == null || str.isEmpty()) { + return 0; + } + return str.chars() + .map(c -> String.valueOf((char) c).matches(CHINESE_REGEX) ? 2 : 1) + .sum(); + } + + public static int getChineseNum(String str) { + if (str == null || str.isEmpty()) { + return 0; + } + return str.chars() + .map(c -> String.valueOf((char) c).matches(CHINESE_REGEX) ? 1 : 0) + .sum(); + } } }