用 Java 将一个字符串插入到另一个字符串中
原文:https://www . geesforgeks . org/insert-a-string-in-other-string-in-Java/
给定一个字符串,任务是在 Java 中特定的指定索引处的给定字符串之间插入另一个字符串。
示例:
Input: originalString = "GeeksGeeks",
stringToBeInserted = "For",
index = 4
Output: "GeeksForGeeks"
Input: originalString = "Computer Portal",
stringToBeInserted = "Science ",
index = 8
Output: "Computer Science Portal"
实现这一点的各种方法如下:
-
Without using any pre-defined method
进场:
- 获取字符串和索引。
- 创建新字符串
- 遍历字符串直到指定的索引,并将其复制到新的字符串中。
- 复制要插入到新字符串中的字符串
- 将第一个字符串的剩余字符复制到新字符串中
- 返回/打印新字符串
下面是上述方法的实现:
程序:
```java // 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)); } } ```
Output:
```java Original String: GeeksGeeks String to be inserted: For String to be inserted at index: 4 Modified String: GeeksForGeeks
```
-
Using String.substring() method
进场:
- 获取字符串和索引。
- 创建新字符串
- 使用 substring(0,index + 1)方法将子字符串从 0 插入到指定的(index+1)中。然后插入要插入到字符串中的字符串。然后使用 substring(index+1)方法将原始字符串的剩余部分插入到新字符串中。
- 返回/打印新字符串
下面是上述方法的实现:
程序:
```java // 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)); } } ```
Output:
```java Original String: GeeksGeeks String to be inserted: For String to be inserted at index: 4 Modified String: GeeksForGeeks
```
-
Using StringBuffer.insert() method
进场:
- 获取字符串和索引。
- 创建一个新的字符串缓冲区
- 使用 StringBuffer.insert()方法将字符串插入到原始字符串中。
- 使用 StringBuffer.toString() 方法从 StringBuffer 返回/打印字符串。
下面是上述方法的实现:
程序:
```java // 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)); } } ```
Output:
```java Original String: GeeksGeeks String to be inserted: For String to be inserted at index: 4 Modified String: GeeksForGeeks
```
版权属于:月萌API www.moonapi.com,转载请注明出处