基本上,我创建了一个代码,导入一个包含单词的文本文件,当我在代码中输入一个单词时,程序会发现这个单词是否在我导入的文本文件中。
我最后想做的是找到索引号(在找到的行内)和找到单词的行号。通过执行以下操作,我可以将代码从总文本中打印出索引号,但不能在找到的行中打印
String indexnum = s1;
System.out.println("Matches at index : " + indexnum.lastIndexOf(s2));
上面印的是
'索引处的匹配:2949232'
我希望它打印的内容类似于。。。
'在第4441行的索引26处匹配'
你知道我该怎么做吗。。老实说,我已经没有主意了。
1条答案
按热度按时间t3irkdon1#
你在用什么
Files.readString()
? 我建议使用扫描仪或类似设备逐行扫描:但是,如果您需要将其存储为一大块文本,也许您可以利用这个版本的indexof,它为搜索提供一个起始索引。