From cc43b5ebe58e1a760eea7d953451f28d9eb61d66 Mon Sep 17 00:00:00 2001 From: 8ga Date: Fri, 10 Jan 2025 11:15:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20CharAsciiFilter.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CharAsciiFilter.java | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 CharAsciiFilter.java diff --git a/CharAsciiFilter.java b/CharAsciiFilter.java new file mode 100644 index 0000000..9c455dc --- /dev/null +++ b/CharAsciiFilter.java @@ -0,0 +1,33 @@ +public class CharAsciiFilter { + + // 用最少的内存过滤出小写字母 + static final String str = "Aa1b2c3d4e5f6f7"; + + // 内存开销更低 + static void good() { + char[] chars = str.toCharArray(); + int x = 0; + for (int i = 0; i < chars.length; i++) { + char c = chars[i]; + if (c >= 97 && c <= 122) { + chars[x] = c; + x = x + 1; + } + } + System.out.println(new String(chars, 0, x)); + } + + // 常规解法 + static void general() { + char[] chars = str.toCharArray(); + StringBuilder sb = new StringBuilder(); + for (char c : chars) { + // Character.isLetter(c) + if (c >= 97 && c <= 122) { + sb.append(c); + } + } + System.out.println(sb); + } + +}