【发布时间】:2013-03-26 17:56:16
【问题描述】:
我有一个读取文件的方法,将每个单词放入字符串数组中,然后将每个单词添加到树中。我想修改它,以便如果它包含非英文字符(例如西班牙语等),则不会将单词添加到树中。我虽然关于“包含”方法,但它不适用于字符串类型的数组。我该怎么做?
public void parse(File f) throws Exception {
Node root = new Node('+'); //create a root node
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
String line;
while((line = br.readLine())!=null){
String[] words = line.toLowerCase().split(" ");
for(int i = 0; i < words.length; i++){
addToTree(words[i], root);
}
}//end of while
【问题讨论】:
-
你不能对你试图添加到树的字符串(words[i])使用 contains 方法吗?
-
您可以使用正则表达式,它只接受带有 -;!,' 的 a 到 Z。
-
stackoverflow.com/questions/2774320/… 这应该可以解决您的问题。
-
除非您准确定义“英文字符”是什么,否则这个问题毫无意义。例如,英语和西班牙语都基于罗马字母表。您是在谈论排除变音符号之类的东西吗?
标签: java arrays string contains