所以我可以在我的文本文件中搜索一个字符串,但是,我想在这个ArrayList中对数据进行排序并实现一个算法。是否可以从文本文件中读取文本文件中的[Strings]值存储在String []数组中。
也可以将字符串分开吗?所以而不是我的数组:
[Alice was beginning to get very tired of sitting by her sister on the, bank, and of having nothing to do:]
是否可以将数组作为:
["Alice", "was" "beginning" "to" "get"...]
public static void main(String[]args) throws IOException
{
Scanner scan = new Scanner(System.in);
String stringSearch = scan.nextLine();
BufferedReader reader = new BufferedReader(new FileReader("File1.txt"));
List<String> words = new ArrayList<String>();
String line;
while ((line = reader.readLine()) != null) {
words.add(line);
}
for(String sLine : words)
{
if (sLine.contains(stringSearch))
{
int index = words.indexOf(sLine);
System.out.println("Got a match at line " + index);
}
}
//Collections.sort(words);
//for (String str: words)
// System.out.println(str);
int size = words.size();
System.out.println("There are " + size + " Lines of text in this text file.");
reader.close();
System.out.println(words);
}
答案 0 :(得分:4)
还可以分离字符串吗? 是的,您可以将此字符串拆分为空格。
String[] strSplit;
String str = "This is test for split";
strSplit = str.split("[\\s,;!?\"]+");
此外,您还可以逐字阅读文本文件。
Scanner scan = null;
try {
scan = new Scanner(new BufferedReader(new FileReader("Your File Path")));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while(scan.hasNext()){
System.out.println( scan.next() );
}
答案 1 :(得分:4)
要将一条线分割成一个单词数组,请使用:
String words = sentence.split("[^\\w']+");
正则表达式[^\w']
表示“不是单词char或撇号”
这将捕获带有“can can”等嵌入式撇号的单词,并跳过所有标点符号。
评论提出了将'this'
引用的单词解析为this
的边缘情况。
这是解决方案 - 您必须首先删除包装引号:
String[] words = input.replaceAll("(^|\\s)'([\\w']+)'(\\s|$)", "$1$2$3").split("[^\\w']+");
以下是一些边缘和角落情况的测试代码:
public static void main(String[] args) throws Exception {
String input = "'I', ie \"me\", can't extract 'can't' or 'can't'";
String[] words = input.replaceAll("(^|[^\\w'])'([\\w']+)'([^\\w']|$)", "$1$2$3").split("[^\\w']+");
System.out.println(Arrays.toString(words));
}
输出:
[I, ie, me, can't, extract, can't, or, can't]