📌  相关文章
📜  在Java中的String中替换特定索引处的字符

📅  最后修改于: 2022-05-13 01:55:31.879000             🧑  作者: Mango

在Java中的String中替换特定索引处的字符

给定一个字符串,任务是在Java中替换该字符中特定索引处的字符串。

例子:

Input: String = "Geeks Gor Geeks", index = 6, ch = 'F'
Output: "Geeks For Geeks."

Input: String = "Geeks", index = 0, ch = 'g'
Output: "geeks"

方法一:使用字符串类

到目前为止,字符串类中没有预定义的方法来替换字符串中的特定字符。然而,这可以通过构造一个具有 2 个不同子字符串的新字符串来间接实现,一个从开始到特定索引 - 1,在特定索引处的新字符,另一个从索引 + 1 到结尾。
下面是上述方法的实现:

Java
public class GFG {
 
    public static void main(String args[])
    {
 
        // Get the String
        String str = "Geeks Gor Geeks";
 
        // Get the index
        int index = 6;
 
        // Get the character
        char ch = 'F';
 
        // Print the original string
        System.out.println("Original String = " + str);
 
        str = str.substring(0, index) + ch
              + str.substring(index + 1);
 
        // Print the modified string
        System.out.println("Modified String = " + str);
    }
}


Java
public class GFG {
 
    public static void main(String args[])
    {
 
        // Get the String
        String str = "Geeks Gor Geeks";
 
        // Get the index
        int index = 6;
 
        // Get the character
        char ch = 'F';
 
        // Print the original string
        System.out.println("Original String = " + str);
 
        StringBuilder string = new StringBuilder(str);
        string.setCharAt(index, ch);
 
        // Print the modified string
        System.out.println("Modified String = " + string);
    }
}


Java
public class GFG {
 
    public static void main(String args[])
    {
 
        // Get the String
        String str = "Geeks Gor Geeks";
 
        // Get the index
        int index = 6;
 
        // Get the character
        char ch = 'F';
 
        // Print the original string
        System.out.println("Original String = " + str);
 
        StringBuffer string = new StringBuffer(str);
        string.setCharAt(index, ch);
 
        // Print the modified string
        System.out.println("Modified String = " + string);
    }
}


输出
Original String = Geeks Gor Geeks
Modified String = Geeks For Geeks

方法二:使用 StringBuilder

与 String 类不同,StringBuilder 类为此目的有一个预定义的方法——setCharAt()。通过调用此方法并将字符和索引作为参数传递来替换特定索引处的字符。
下面是上述方法的实现:

Java

public class GFG {
 
    public static void main(String args[])
    {
 
        // Get the String
        String str = "Geeks Gor Geeks";
 
        // Get the index
        int index = 6;
 
        // Get the character
        char ch = 'F';
 
        // Print the original string
        System.out.println("Original String = " + str);
 
        StringBuilder string = new StringBuilder(str);
        string.setCharAt(index, ch);
 
        // Print the modified string
        System.out.println("Modified String = " + string);
    }
}
输出
Original String = Geeks Gor Geeks
Modified String = Geeks For Geeks

方法3:使用StringBuffer

与 StringBuilder 一样,StringBuffer 类有一个为此目的预定义的方法——setCharAt()。通过调用此方法并将字符和索引作为参数传递来替换特定索引处的字符。 StringBuffer 是线程安全的。与 StringBuffer 相比,StringBuilder 更快,但不是线程安全的。

下面是上述方法的实现:

Java

public class GFG {
 
    public static void main(String args[])
    {
 
        // Get the String
        String str = "Geeks Gor Geeks";
 
        // Get the index
        int index = 6;
 
        // Get the character
        char ch = 'F';
 
        // Print the original string
        System.out.println("Original String = " + str);
 
        StringBuffer string = new StringBuffer(str);
        string.setCharAt(index, ch);
 
        // Print the modified string
        System.out.println("Modified String = " + string);
    }
}
输出
Original String = Geeks Gor Geeks
Modified String = Geeks For Geeks