0

文字列があるとします。String stringy = "<Monkey><Banana><Raffle/>"

そして別の文字列String stringier = <Cool stuff to add/>

stringierの終了と開始の間に>どの<Banana>よう<に挿入でき<Raffle/>ますか? 文字列のインデックスも使用できません。これは、stringyの前に多かれ少なかれ文字が含ま<Raffle/>れる可能性があり、おそらく同じものになることはないからです。何か案は?

4

4 に答える 4

0

Java 文字列は不変ですが、StringBuilder は不変です。

public class StringyThingy {
  public static void main(String[] args) {
    StringBuilder stringy = new StringBuilder("<Monkey><Banana><Raffle/>");

    System.out.println(stringy);

    // there has to be a more elegant way to find the index, but I'm busy.
    stringy.insert(stringy.indexOf("Banana")+"Banana".length()+1,"<Cool stuff to add/>");

    System.out.println(stringy);
  }
}

// output
<Monkey><Banana><Raffle/>
<Monkey><Banana><Cool stuff to add/><Raffle/>
于 2013-06-28T17:27:09.853 に答える