javaは元の文字に新しい文字または文字列を挿入します。
2680 ワード
文字コードを挿入:
実行結果:
文字列コードを挿入:
public class Test {
/** **/
public static void main(String[] args){
StringBuffer sb = new StringBuffer(" !");// , " !"
System.out.println(" :"+sb);//
System.out.println(" :"+sb.length() );//
System.out.println(" :"+sb.capacity() );//
sb.insert(5, ' ');//
System.out.println(" :"+sb);//
System.out.println(" :"+sb.length() );//
System.out.println(" :"+sb.capacity() );//
}
}
解釈:sb.insert(5、‘小’);下付き位置を指定する前の値に新しい値を割り当てます。sbの6番目の文字の前に小さい文字を挿入するという意味です。5は文字キャッシュエリアの下付きの位置で、配列と同じ0から始まります。実行結果:
: !
:8
:24
: !
:9
:24
*********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************文字列コードを挿入:
public class Test {
/** **/
public static void main(String[] args){
StringBuffer sb = new StringBuffer(" !");// , " !"
System.out.println(" :"+sb);//
System.out.println(" :"+sb.length() );//
System.out.println(" :"+sb.capacity() );//
sb.insert(5, " 18 ");//
System.out.println(" :"+sb);//
System.out.println(" :"+sb.length() );//
System.out.println(" :"+sb.capacity() );//
}
}
実行結果: : !
:8
:24
: 18 !
:14
:24
まとめ: sb.insert(5, ' ') sb 6 ‘ ’ ;
sb.insert(5, " 18 "); sb 6 " 18 ";
: ‘’, ""