1

段落から文全体を削除するコードを作成しようとしています。どの文であるかは問題ではありませんが、少なくとも 1 つの文である必要があります。

    String edit = "The cow goes moo. The cow goes boo. The cow goes roo. The cow goes jew.";
    int sentencestart = (edit.substring(edit.length()/2).indexOf('.') + edit.length()/2);
    int sentenceend = edit.substring(sentencestart).indexOf('.') + sentencestart;
    edit = edit.substring(0, sentencestart) + edit.substring(sentenceend);
    System.out.println(edit);

これは私が現在持っているコードです。現在、私が始めたのとまったく同じ文字列を印刷しています。アイデアはありますか?

編集:文を削除する必要があることを示唆するのは間違っていました。私は最初の文以外の文を意味しました。削除する文は文字列の途中に配置し、実際のアプリケーションは非常に大きな文字列で使用することをお勧めします。

4

3 に答える 3

1

.分割して、次のような必要な行を取得してみませんか

string edit = "The cow goes moo. The cow goes boo. The cow goes roo. The cow goes jew.";
return edit.Substring(edit.Split('.')[0].Length + 1,edit.Length - edit.Split('.')[0].Length - 1);

出力:The cow goes boo. The cow goes roo. The cow goes jew.

免責事項:上記のコードはC#構文であり、最小限の変更Javaで同じことができることを願っています。Java

于 2014-09-16T23:11:08.547 に答える