从Java中的String中获取句子

时间:2014-10-09 10:58:10

标签: java string search

我有一个字符串。它的一些内容可以更改,一些内容是固定的。

就像:

String mycontent = "here is the sentence 1 . here is the sentence 2 . hereisthesomesentence3.here is the sentence 4. "

sentence 1 content : changeable  ,
sentence 2 content : fixed       ,
sentence 3 content : changeable  ,
sentence 4 content : changeable .

我想接受句子3的内容,比如

String sentence3 = "hereisthesomesentence3"

注意:由于部分字符串可以更改,我不知道有多少句子。我最终可能会有10或20个句子。

我的字符串内容如下:

some paragraphs in here.// i do not know what is it writing .  After this changeable contents 
fixed content :"url" //  fixed content not change . But url can be change **i want to get url** 
some other paragraphs in here  // here some other contents. 

示例代码3 :(我想得到我的网址;我们仍然有一些部分可以更改,某些部分已修复)

Some Other Paragraphs
FIXED TEXT    

<span class="subcat2"><a href="myurl">
    <span style="display: inline-block; float: left; color: #ccc !important;"></span>  Hello World!!!!!!!!!!!!!!!!!!!!!!!!!! 
</a></span>

Some Other Paragraphs

4 个答案:

答案 0 :(得分:1)

所以每个句子都用逗号分隔。然后你只需要拆分:

 public class HelloWorld{

   public static void main(String []args){
     String myContent ="here is the sentence 1 . here is the sentence 2 . hereisthesomesentence3.here is the sentence 4. ";

     String[] parts = myContent.split("\\.");
     System.out.println("Amount of parts = " + parts.length);
     System.out.println("Sentence 3 = " + parts[2].trim()); // trim() removes leading and trailing whitespaces
   }
 }

答案 1 :(得分:1)

这样的事情会起作用:

public static void main(String[] args) {
    String mycontent = "here is the sentence 1 . here is the sentence 2 . hereisthesomesentence3.here is the sentence 4. ";
    System.out.println(mycontent.split("\\.")[2].trim());
}

output : 
hereisthesomesentence3

答案 2 :(得分:0)

<强>码

String mycontent =
    "here is the sentence 1 . here is the sentence 2 . here isthesomesentence3.here is the sentence 4. "
String[] totalSentance = mycontent.split("\\."); 
System.out.println("Sentence No. 3 = " + totalSentance[2]);

答案 3 :(得分:0)

这样的事情,改编自getSentenceInstance and whitespace

    Scanner input = new Scanner(new File("some/path/to/sampleinput.txt"));

    ArrayList theSentences = new ArrayList<String>();
    String myText = String.valueOf(input); //the text is produced through a text box
    BreakIterator boundary = BreakIterator.getSentenceInstance();
    boundary.setText(myText);
    int start = boundary.first();
    for (int end = boundary.next(); end != BreakIterator.DONE; start = end, end = boundary.next())
    {
        String temp = myText.substring(start,end);
        theSentences.add(temp.trim());
    }