0

私はひもを持っています、言います

"the dog jumped over the fox"

私がそうするならば、string.contains("the")それは戻りtrueます。の最初のインスタンスだけを引き出す方法はあります"the"か?

編集:私の質問が曖昧であることに気づきました。ご想像のとおり、最初のインスタンスを見つけて「引き出し」、「」に置き換えたいと思います。

4

2 に答える 2

3

「引き出す」が削除を意味する場合は、 を使用できますString#replaceFirst

すなわち

String sentence = "the dog jumped over the fox";
if (sentence.contains("the")) {
    sentence = sentence.replaceFirst("the\\s*","");
}
于 2012-12-06T02:06:09.117 に答える
0

文字列の最初のインスタンスを見つけたいですか? 見るstring.indexof

最初のインスタンスを置き換えますか? 見るstring.ReplaceFirst

http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html

于 2012-12-06T02:06:56.280 に答える