我提供一种方法:
- class StringDemo
- {
- public static void main(String[] args)
- {
- String s ="asd!1wWE$.";
- //将字符串变成字符数组
- char[] ch =s.toCharArray();
-
- StringBuilder sb =new StringBuilder();
- //遍历字符数组
- for (int i =0;i<ch.length ;i++ )
- {
- //如果是字目就加到容器中
- if((ch[i]>='a'&&ch[i]<='z') || (ch[i]>='A'&&ch[i]<='Z'))
- sb.append(ch[i]);
- }
- System.out.println(sb.toString());
- }
- }
复制代码 |