java/CharAsciiFilter.java
2025-01-10 11:15:07 +08:00

34 lines
841 B
Java

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