【问题标题】:i have a String contains a repeated word like 72 times and i want to split the text into many strings that contains substrings between that word我有一个字符串包含一个重复的单词,例如 72 次,我想将文本拆分为许多字符串,其中包含该单词之间的子字符串
【发布时间】: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


【解决方案1】:

你可以简单地使用String[] arr = String.split("repeatedWordHere"),你可以忽略数组的第一个和最后一个元素

【讨论】:

    【解决方案2】:

    好吧,如果您能提供示例数据,那就太好了,但是根据您的陈述,您可以使用可用于字符串对象的 split(String regex) 方法。

     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";
              printDelimitedValues(fileAsString, keyword); // calling method to split here
             
            }
     
     public static void printDelimitedValues(String str , String delimiter) {
            
            String strArr[] = str.split(delimiter);
            for(String substr:strArr) {
                System.out.println(substr);
            }
            
        }
    

    所以如果样本数据是这样的:迅捷棕狐AGE迅捷棕狐AGE迅捷棕狐AGE 关键字是年龄 输出将是:

    快速的棕色狐狸

    快速的棕色狐狸

    快速的棕色狐狸

    【讨论】:

    • 样本数据将是白色的大房子、绿树、黄色的太阳
    • 只有重复的,我希望每个句子都在一行中
    • 改变这个 System.out.println(substr);到 System.out.print(substr);这将在 1 行中给出输出
    • 如果输入是日月星辰,那么整个动态变化既不能将输入存储在单个字符串中,也不能存储在输出中。您必须以块的形式从给定文件中读取数据,并以块的形式写入另一个文件或控制台,并具有足够的内存来维持数据作为内存中的缓冲区
    • 我的意思是每个句子在不同的行中