【发布时间】:2020-10-23 11:05:46
【问题描述】:
我有一个字符串包含一个重复的单词,例如 72 次,我想将文本拆分为许多子字符串,每个子字符串包含单词第一次出现和第二次出现之间的字符串,然后第二个子字符串包含第二次出现和第二次出现之间的字符串第三个,依此类推,直到最后一次出现 //我的样本数据,比如(黄色的太阳、绿色的树、大房子) //考虑是重复的单词
public class wordOutput {
public static void main(String args[]) throws Exception{
InputStream is = new FileInputStream("C:\\Users\\HP\\Documents\\output3.txt");
BufferedReader buf = new BufferedReader(new InputStreamReader(is));
String line = buf.readLine();
StringBuilder sb = new StringBuilder();
while(line != null){
sb.append(line).append("\n");
line = buf.readLine();
}
String fileAsString = sb.toString();
System.out.println("Contents : " + fileAsString);
String keyword="AGE";
int index = fileAsString.indexOf(keyword);
while (index >=0){
System.out.println("Index : "+index);
index = fileAsString.indexOf(keyword, index+keyword.length()) ;
} int last=fileAsString.length();
System.out.println("last " +last);
}
}
}
【问题讨论】:
-
您可以编辑您的帖子以使用正确的缩进代码格式吗?谢谢。
标签: java string function file file-manager