String test = "ab564gh31cd中国abcd345.45ei上海d*/-+ce是的啊frw"; String[] result = test.split("[^\u4e00-\u9fa5]+"); int num = 0; for (int i = 0; i < result.length; i++) { if (result[i].length()!=0) { num++; } } System.out.println(num); } }
避而不谈
2024-11-28 08:15:04
public static void main(String args[]) { String req = "[\u4e00-\u9faf]+";
public class Hanzi { public static void main(String[] args) { Hanzi test = new Hanzi(); String input = "abcd中国abcdei上海dce是的啊frw"; ArrayList<String> list = new ArrayList<String>(); char[] chars = input.toCharArray(); for (int i = 0; i < chars.length; i++) { if (test.isCharacter(chars[i])) { StringBuffer sb = new StringBuffer(); sb.append(chars[i]); while (test.isCharacter(chars[i + 1])) { sb.append(chars[i + 1]); i++; } list.add(sb.toString()); } }
for (Iterator<String> it = list.iterator(); it.hasNext();) { String tmp = it.next(); System.out.println(tmp); } }
private boolean isCharacter(char c) { if (c >= '\u4e00' && c <= '\u9fbf') { return true; } else { return false; } } }