GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — StringBuilder replace() in Java with Examples - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

StringBuilder replace() in Java with Examples

Last Updated : 24 Jan, 2026

The replace(int start, int end, String str) method of StringBuilder is used to replace a specified range of characters with a given string. It modifies the existing StringBuilder object directly, making it efficient for frequent updates to strings.

Java
class Main {
    public static void main(String[] args) {

        StringBuilder sb = new StringBuilder("Hello World");
        sb.replace(6, 11, "Java");

        System.out.println(sb);
    }
}

Output
Hello Java

Explanation:

  • A StringBuilder object is created with the initial value "Hello World".
  • The replace(6, 11, "Java") method replaces the characters from index 6 to 10 ("World") with "Java".
  • The modified StringBuilder is printed, showing the updated string "Hello Java".

Syntax

public StringBuilder replace(int start, int end, String str)

Parameters: This method accepts three parameters:

  • start - Starting index (inclusive)
  • end - Ending index (exclusive)
  • str - String to replace the specified range

Return Value: This method returns StringBuilder object after successful replacement of characters. 

Note:

  • The character at index start is included, while the character at index end is excluded.
  • If start == end, the string is inserted at that position.

Examples of StringBuilder replace() Method

Example 1: Basic Replacement

Java
class GFG {
    public static void main(String[] args) {

        StringBuilder sb = new StringBuilder("WelcomeGeeks");

        System.out.println("Original: " + sb);

        sb.replace(1, 7, "e are ");

        System.out.println("After: " + sb);
    }
}

Output
Original: WelcomeGeeks
After: We are Geeks

Explanation: Characters from index 1 to 6 are replaced with "e are ".

Example 2: Replacing a Substring in the Middle

Java
class Main {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("I like Python");
        sb.replace(7, 13, "Java");
        System.out.println(sb);
    }
}

Output
I like Java

Explanation: The replace(7, 13, "Java") call replaces the substring "Python" with "Java" in the middle of the StringBuilder.

Example 3: Inserting Using replace()

Java
class Main {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Programming");
        sb.replace(0, 0, "Java ");
        System.out.println(sb);
    }
}

Output
Java Programming

Explanation: When start and end are the same, the string is inserted at that position.

Example 4: Replacing Entire String

Java
class Main {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Old Value");
        sb.replace(0, sb.length(), "New Value");
        System.out.println(sb);
    }
}

Output
New Value

Explanation: It replaces all characters from index 0 to the length of the StringBuilder, effectively replacing the entire string with "New Value".

Example 5: Invalid Index Handling

Java
class Main {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Example");
        sb.replace(3, 20, "Test");
    }
}

Output:

StringIndexOutOfBoundsException

Explanation: The end index exceeds the length of the StringBuilder.

Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.