【问题标题】:Get sentence from a String in Java从Java中的字符串中获取句子
【发布时间】:2014-10-09 10:58:09
【问题描述】:

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

是这样的:

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:(我想获取我的 url;我们仍然有一些部分是可更改的,一些部分是固定的)

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

【问题讨论】:

  • 只需使用System.out.println("changeable"),因为内容是“可变的”... 飞走:D
  • 那么,这个字符串的结构看起来如何?
  • 你在这里对sentence的定义是什么?你的意思是每次有一个句号,就开始一个新的句子?会不会出现像 “M. Smith 在 35.7 秒内洗完澡。”这样的句子?
  • 这个字符串,我不知道它是什么内容。我只知道有些东西是固定的,它是恒定的。我想在恒定的单词之后获得内容。可能有

标签: java string search


【解决方案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

【讨论】:

  • 我不知道里面有多少点
  • @user3632921 - 你是什么意思?你能举例说明你想说什么吗?
  • 句子 1 中可能有 10 个句子。因为它是可变的。所以我应该使用句子 2。因为在那里我知道它在写什么,我只想在句子 2 之后得到句子。
  • @user3632921 - 向我们展示您正在谈论的输入。
  • 我添加了一些新内容。我希望你这次能理解
【解决方案2】:

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

 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
   }
 }

【讨论】:

  • 我认为你需要根据一个点来分割它。
  • 可以用'.'分割并修剪结果。
  • 我的第一句话 “M. Smith 在 35.7 秒内洗澡。” ?
  • 谢谢,我得检查一下眼睛 :)。点更难,因为 split 是正则表达式,而点是一个特殊的正则表达式字符,所以要小心。
  • 考虑到在字符串下发布的值,它可能是逗号,而不是点。问题不精确,但如果是点,则可以将其拆分为string.split("[.]");
【解决方案3】:

代码

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]);

【讨论】:

    【解决方案4】:

    这样的东西怎么样,改编自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());
        }
    

    【讨论】:

      猜你喜欢
      • 2017-10-16
      • 2013-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多