How can we split a string by sentence as a delimiter in Java?


The split() method of the String class accepts a String value representing the delimiter and splits into an array of tokens (words), treating the string between the occurrence of two delimiters as one token.

For example, if you pass single space “ ” as a delimiter to this method and try to split a String. This method considers the word between two spaces as one token and returns an array of words (between spaces) in the current String.

If the String does not contain the specified delimiter this method returns an array containing the whole string as an element.

Example

 Live Demo

public class SplitExample {
   public static void main(String[] args) {
      String str = "Hi how are you welcome to Tutorialspoint";
      String words[] = str.split(" ");
      for(String token : words) {
         System.out.println(token);
      }
   }
}

Output

Hi
how
are
you
welcome
to
Tutorialspoint

Splitting a string by sentence as a delimiter

You can also split a sentence by passing a sentence as a delimiter if you do so each time the specified sentence occurs the String is divided as a separate token.

Example

Following Java program reads the contents of a file into a Sting and splits it using the split() method with a sentence as a delimiter.

public class StringOccurrence {
   public static String fileToString(String filePath){
      Scanner sc = null;
      String input = null;
      StringBuffer sb = null;
      try {
         sc = new Scanner(new File(filePath));
         sb = new StringBuffer();
         while (sc.hasNextLine()) {
            input = sc.nextLine();
            sb.append(" "+input);
         }
      }
      catch(Exception ex) {
         ex.toString();
      }
      return sb.toString();
   }
   public static void main(String args[]) throws FileNotFoundException {
      String filePath = "D://sampleData.txt";
      String text = fileToString(filePath);
      String array[] = text.split("We don’t force our readers to sign up with us or submit their details either");
      for(String token : array) {
         System.out.println(" ");
         System.out.println(token);
      }
   }
}

Output

Tutorials Point originated from the idea that there exists a class of readers 
who respond better to online content and prefer to learn new skills at their 
own pace from the comforts of their drawing rooms. The journey commenced with 
a single tutorial on HTML in 2006 and elated by the response it generated, 
we worked our way to adding fresh tutorials to our repository which now 
proudly flaunts a wealth of tutorials and allied articles on topics ranging 
from programming languages to web designing to academics and much more. 
40 million readers read 100 million pages every month. Our content and 
resources are freely available and we prefer to keep it that way to 
encourage our readers acquire as many skills as they would like to.
No preconditions and no impediments. Simply Easy Learning!

Updated on: 10-Sep-2019

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements