添加 CharAsciiFilter.java
This commit is contained in:
parent
d7fb2edf0c
commit
cc43b5ebe5
33
CharAsciiFilter.java
Normal file
33
CharAsciiFilter.java
Normal file
@ -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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user