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); + } + +}