Java将 String 插入到另一个 String 中
假设我们有一个字符串“That’sgood!”在其中我们需要插入文本“no”。因此,结果字符串应为“
String str = "That's good!"; String newSub = "no ";
现在,将在其中插入新子字符串的索引-
int index = 6;
现在插入新的子字符串-
StringBuffer resString = new StringBuffer(str); resString.insert(index + 1, newSub);
现在让我们看一个将字符串插入另一个的示例-
示例
import java.lang.*;
public class Main {
public static void main(String[] args) {
String str = "That's good!";
String newSub = "no ";
int index = 6;
System.out.println("初始字符串 = " + str);
System.out.println("将插入新字符串的索引 = " + index);
StringBuffer resString = new StringBuffer(str);
resString.insert(index + 1, newSub);
System.out.println("结果字符串 = "+resString.toString());
}
}输出结果
初始字符串 = That's good! 将插入新字符串的索引 = 6 结果字符串 = That's no good!
热门推荐
10 祝孙辈生日祝福语简短
11 恋人跨年祝福语大全简短
12 考大学敬酒简短祝福语
13 开工投产大吉祝福语 简短
14 偶然结婚祝福语朋友简短
15 感谢学长学姐祝福语简短
16 戏精的祝福语简短
17 我要结婚祝福语简短
18 婚前女儿生日祝福语简短