Java如何在StringBuilder中插入字符串?
package org.nhooo.example.lang;
public class StringBuilderInsert {
public static void main(String[] args) {
StringBuilder alphabets = new StringBuilder("abcdfghopqrstuvwxyz");
System.out.println("alphabets = " + alphabets);
//|a|b|c|d|f|g|h|i|...。
//0|1|2|3|4|5|6|7|8|...
//
//从上面的序列中,您可以看到字符串的索引是
//从0开始,因此当我们在第四个偏移量中插入字符串时,
//表示它将被插入“d”字母之后。还有其他过载"d" letter. There are other overload
//此方法的版本,可用于插入其他类型的数据
//例如char,int,long,float,double,Object等。
alphabets.insert(4, "e");
System.out.println("alphabets = " + alphabets);
//在这里,我们将一个字符数组插入到StringBuilder中。
alphabets.insert(8, new char[] {'i', 'j', 'k', 'l', 'm', 'n'});
System.out.println("alphabets = " + alphabets);
}
}上面的代码片段的结果:
alphabets = abcdfghopqrstuvwxyz alphabets = abcdefghopqrstuvwxyz alphabets = abcdefghijklmnopqrstuvwxyz