简体   繁体   中英

How to store each sentence of a paragraph in a string in java?

I am doing a project on encryption using a key less approach. I am struck while storing a textual paragraph containing a series of sentences into a string.My requirement is to store each sentence of the paragraph into string separately.
For example: If the paragraph contains, "Hello Indians,today is good day for you. We are the champions of the world cricket.Here is the time for celebration.Enjoy the moment." The requirement is to store "Hello Indians,today is good day for you." in str[0] ,"We are the champions of the world cricket." in str[1] and so on. Can anyone please help me as early as possible to tackle this problem.

  • Keerthi JNNCE

The String.split() method is what you are in search of. Try this:

public static void main(String[] args) {
    for (String sentence : "Hello Indians,today is good day for you. We are the champions of the world cricket.Here is the time for celebration.Enjoy the moment.".split("[.]")) {
        System.out.println(sentence);
    }
}
......
String yourinput = "Hello Indians,today is good day for you. We are the champions of the world cricket.Here is the time for celebration.Enjoy the moment.";
String[] str = yourinput.split(".");
......

This should be what you're looking for

Use paragraph.split(".") , if you are sure, that each sentence ends with ".". paragraph is a String , containing all your sentences. split(".") returns a String[] , i call it sentences , where the first element ( sentences[0] ) contains the part from begining to the first ".", the second element ( sentence[1] ) contains the sentence from first to second "."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM