Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

replace()

Example

Generate a fresh string in which every “l” character is substituted with “p”

String myStr = "Hello";
System.out.println(myStr.replace('l', 'p'));

Definition and Usage

The replace() method scans a string for a designated character and furnishes a new string with the designated character(s) substituted.

Syntax

public String replace(char searchChar, char newChar)

Parameter Values

Parameter

Description

searchChar

A character that indicates which character will be substituted with the new one.

newChar

A character that denotes the character to be substituted for the searchChar.

Technical Details

Returns:

A fresh String in which the specified character has been substituted with the new character(s).