插入字符代码:

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岁的";

注意:字符用‘',字符串用""

补充知识:java格式化字符串,在指定位置插入指定字符串,兼容中英文以及特殊字符,例如:换行,用于解决生成pdf换行问题等问题

原因: 由于html转pdf时,不能自动换行,因此才有下面的代码.

注释:完全模拟html页面的自动换行!

最后的页面展示效果:每一行的长度都一样,看上去像

的自动换行一样

package test;
import java.io.UnsupportedEncodingException;
/**
* 解决pdf换行问题,在指定位置插入指定字符串,兼容中英文以及特殊字符
*
* @author xg君
*
*/
public class app {
public static void main(String[] args) throws UnsupportedEncodingException {
System.out.println(addStr(10, "
", "as阿萨德dsa阿斯蒂芬fladadasdsjf阿斯蒂芬ljdsljkjlfdsklfd啥地方都是skljdsasfasdfads"));}
/**
* 插入方法
*
* @param num
* 每隔几个字符插入一个字符串(中文字符)
* @param splitStr
* 待指定字符串
* @param str
* 原字符串
* @return 插入指定字符串之后的字符串
* @throws UnsupportedEncodingException
*/
public static String addStr(int num, String splitStr, String str) throws UnsupportedEncodingException {
StringBuffer sb = new StringBuffer();
String temp = str;
int len = str.length();
while (len > 0) {
int idx = getEndIndex(temp, num);
sb.append(temp.substring(0, idx + 1)).append(splitStr);
temp = temp.substring(idx + 1);
len = temp.length();
}
return sb.toString();
}
/**
* 两个数字/英文
*
* @param str
* 字符串
* @param num
* 每隔几个字符插入一个字符串
* @return int 最终索引
* @throws UnsupportedEncodingException
*/
public static int getEndIndex(String str, double num) throws UnsupportedEncodingException {
int idx = 0;
double val = 0.00;
// 判断是否是英文/中文
for (int i = 0; i < str.length(); i++) {
if (String.valueOf(str.charAt(i)).getBytes("UTF-8").length >= 3) {
// 中文字符或符号
val += 1.00;
} else {
// 英文字符或符号
val += 0.50;
}
if (val >= num) {
idx = i;
if (val - num == 0.5) {
idx = i - 1;
}
break;
}
}
if (idx == 0) {
idx = str.length() - 1;
}
return idx;
}
}

效果:

java字符串中插入字符 java向字符串中添加字符_java字符串中插入字符