Java String replace(), replaceFirst() and replaceAll() Method

Java String replace(), replaceFirst() & replaceAll() Method EXAMPLE

Java String has three types of Replace method

1. Java String replace() Method

Description:

This Java method returns a new string resulting from replacing every occurrence of characters with a new characters

Syntax:

Output:

Original String is ‘: the quick fox jumped
String after replacing ‘fox’ with ‘dog’: the quick dog jumped
String after replacing all ‘t’ with ‘a’: ahe quick fox jumped

2.Java String Replaceall()

Description

The java string replaceAll() method returns a string replacing all the sequence of characters matching regular expression and replacement string.

Signature:

regx: regular expression

replacement: replacement sequence of characters

Example 2

Parameters

rgex − the regular expression to which given string need to matched.

replacement − the string that replaces regular expression.

Return Value

This method returns resulting String as an output.

Example 3:

top
X
Glad to have you at Our Website
Welcome to WPBot
wpChatIcon