简体   繁体   English

如何在Java字符串中存储段落的每个句子?

[英]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." 要求是存储“ Hello Indians,今天对您来说是美好的一天”。 in str[0] ,"We are the champions of the world cricket." 在str [0]中,“我们是世界板球的冠军。” in str[1] and so on. 在str [1]中,依此类推。 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. 您正在寻找String.split()方法。 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.split(".") paragraph is a String , containing all your sentences. paragraph是一个String ,包含所有句子。 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 "." split(".")返回一个String[] ,我称它为sentences ,其中第一个元素( sentences[0] )包含从开始到第一个“。”的部分,第二个元素( sentence[1] )包含从第一句到第二句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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