ここにいくつかのコードがあります:
String cakes = "I like to eat ice cream sandwiches at night";
cakes = cakes.replace("ice cream", "");
System.out.println(cakes);
これはアイスクリームを削除します。涼しい。しかし、私が欲しいのは:
String cakes = "I like to eat ice cream sandwiches at night";
cakes = "ice" thru "sandwiches";
System.out.println(cakes);
作り上げた操作は、氷とサンドイッチの間の文字を除いてすべてを削除し、ストリングケーキを「アイスクリームサンドイッチ」にすることです。それは可能ですか?
編集:私は新しいコードを持っています:
String cakes = "I like to eat ice cream sandwiches at night";
String ice = "ice";
String sandwiches = "sandwiches";
cakes = cakes.substring(cakes.indexOf(ice),cakes.indexOf(sandwiches)+sandwiches.length());
System.out.println(cakes);
これは機能しますが、問題があります。ケーキの一部の値(たとえば、Webサイトのhtmlコード)について、エラーが発生します。
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -number
at java.lang.String.substring(Unknown Source)
at package.cclass.main(cat.java:31)