How to replace a character at a specific index in a String in Java

In this section, we will show you how replace a character at a specific index in a String in Java.

Note: String is an immutable class in java. That means we cannot make any change in the String object. Any method which seems to modify it always returns a new string object with modification. 

Following ways can be used to replace a character at a specific index in a String in Java:

  1. By using substring () method
  2. By using StringBuilder setCharAt() method
  3. By using StringBuffer setCharAt() method
  4. By using toCharArray() method


Example 1: Using substring () method

Create a new string with the character replaced.

public class Main {

// Driver code
public static void main(String[] args)
{
// Get the String
String string = "How ate you?";

// Get the index
int index = 5;

// Get the character
char ch = 'r';

// Print the original string
System.out.println("Original String = " + string);

string = string.substring(0, index) + ch
+ string.substring(index + 1);

// Print the modified string
System.out.println("Modified String = " + string);
}
}


Console Output:
Original String = How ate you?
Modified String = How are you?


Example 2: Using StringBuilder setCharAt() method

The StringBuilder setCharAt() method sets a specified character at the given index. This method changes the character sequence represented by StringBuilder object as it replaces the existing char with new char.

public class Main {

// Driver code
public static void main(String[] args)
{
// Get the String
String string = "How ate you?";

// Get the index
int index = 5;

// Get the character
char ch = 'r';

// Print the original string
System.out.println("Original String = " + string);

StringBuilder result = new StringBuilder(string);
result.setCharAt(index, ch);

// Print the modified string
System.out.println("Modified String = " + result);
}
}


Console Output:
Original String = How ate you?
Modified String = How are you?


Example 3: Using StringBuffer setCharAt() method

The StringBuffer setCharAt() method sets a specified character at the given index. This method changes the character sequence represented by StringBuilder object as it replaces the existing char with new char.

public class Main {

// Driver code
public static void main(String[] args)
{
// Get the String
String string = "How ate you?";

// Get the index
int index = 5;

// Get the character
char ch = 'r';

// Print the original string
System.out.println("Original String = " + string);

StringBuffer result = new StringBuffer(string);
result.setCharAt(index, ch);

// Print the modified string
System.out.println("Modified String = " + result);
}
}


Console Output:
Original String = How ate you?
Modified String = How are you?


Example 4: Using toCharArray() method

The given string is converted to a character array using  toCharArray() method and then replace the character at the given index in the character array. Finally, convert the character array back into a string using String.valueOf(char[]) method.

public class Main {

// Driver code
public static void main(String[] args)
{
// Get the String
String string = "How ate you?";

// Get the index
int index = 5;

// Get the character
char ch = 'r';

// Print the original string
System.out.println("Original String = " + string);

// convert the given string to a character array
char[] chars = string.toCharArray();

// replace character at the specified position in a char array
chars[index] = ch;

// convert the character array back into a string
string = String.valueOf(chars);

// Print the modified string
System.out.println("Modified String = " + string);
}
}

Console Output:
Original String = How ate you?
Modified String = How are you?

More related topics,

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Spring Boot + Mockito simple application with 100% code coverage

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete