在Java中将一个字符串插入另一个字符串
给定一个字符串,任务是在给定字符串之间插入另一个字符串,位于Java中的特定索引处。
例子:
输入: originalString = "GeeksGeeks", stringToBeInserted = "For", index = 4输出: "GeeksForGeeks" 输入: originalString = “计算机门户”,stringToBeInserted = “科学”,索引 = 8输出: “计算机科学门户”
执行此操作的各种方法如下:
- 不使用任何预定义的方法
方法:
- 获取字符串和索引。
- 创建一个新字符串
- 遍历字符串直到指定索引并将其复制到新字符串中。
- 将要插入的字符串复制到这个新字符串中
- 将第一个字符串的剩余字符复制到新字符串中
- 返回/打印新字符串
下面是上述方法的实现:
程序:
// Java program to insert a string into another string // without using any pre-defined method import java.lang.*; class GFG { // Function to insert string public static String insertString( String originalString, String stringToBeInserted, int index) { // Create a new string String newString = new String(); for (int i = 0; i < originalString.length(); i++) { // Insert the original string character // into the new string newString += originalString.charAt(i); if (i == index) { // Insert the string to be inserted // into the new string newString += stringToBeInserted; } } // return the modified String return newString; } // Driver code public static void main(String[] args) { // Get the Strings String originalString = "GeeksGeeks"; String stringToBeInserted = "For"; int index = 4; System.out.println("Original String: " + originalString); System.out.println("String to be inserted: " + stringToBeInserted); System.out.println("String to be inserted at index: " + index); // Insert the String System.out.println("Modified String: " + insertString(originalString, stringToBeInserted, index)); } }
输出:Original String: GeeksGeeks String to be inserted: For String to be inserted at index: 4 Modified String: GeeksForGeeks
- 使用 String.substring() 方法
方法:
- 获取字符串和索引。
- 创建一个新字符串
- 使用 substring(0, index+1) 方法插入从 0 到指定 (index + 1) 的子字符串。然后将要插入的字符串插入到字符串中。然后使用 substring(index+1) 方法将原始字符串的剩余部分插入到新字符串中。
- 返回/打印新字符串
下面是上述方法的实现:
程序:
// Java program to insert a string into another string // without using any pre-defined method import java.lang.*; class GFG { // Function to insert string public static String insertString( String originalString, String stringToBeInserted, int index) { // Create a new string String newString = originalString.substring(0, index + 1) + stringToBeInserted + originalString.substring(index + 1); // return the modified String return newString; } // Driver code public static void main(String[] args) { // Get the Strings String originalString = "GeeksGeeks"; String stringToBeInserted = "For"; int index = 4; System.out.println("Original String: " + originalString); System.out.println("String to be inserted: " + stringToBeInserted); System.out.println("String to be inserted at index: " + index); // Insert the String System.out.println("Modified String: " + insertString(originalString, stringToBeInserted, index)); } }
输出:Original String: GeeksGeeks String to be inserted: For String to be inserted at index: 4 Modified String: GeeksForGeeks
- 使用 StringBuffer.insert() 方法
方法:
- 获取字符串和索引。
- 创建一个新的字符串缓冲区
- 使用 StringBuffer.insert() 方法将 stringToBeInserted 插入原始字符串。
- 使用 StringBuffer.toString() 方法从 StringBuffer 返回/打印字符串。
下面是上述方法的实现:
程序:
// Java program to insert a string into another string // without using any pre-defined method import java.lang.*; class GFG { // Function to insert string public static String insertString( String originalString, String stringToBeInserted, int index) { // Create a new StringBuffer StringBuffer newString = new StringBuffer(originalString); // Insert the strings to be inserted // using insert() method newString.insert(index + 1, stringToBeInserted); // return the modified String return newString.toString(); } // Driver code public static void main(String[] args) { // Get the Strings String originalString = "GeeksGeeks"; String stringToBeInserted = "For"; int index = 4; System.out.println("Original String: " + originalString); System.out.println("String to be inserted: " + stringToBeInserted); System.out.println("String to be inserted at index: " + index); // Insert the String System.out.println("Modified String: " + insertString(originalString, stringToBeInserted, index)); } }
输出:Original String: GeeksGeeks String to be inserted: For String to be inserted at index: 4 Modified String: GeeksForGeeks