Computer Science, asked by BrainlyProgrammer, 2 months ago

[Not a Challenge]

Question:-
••• Is replace() and replaceAll() both are same? Give Reason.

Programming language: Java
__
>No Spams :)
> ProTips:- Always provide suitable example while explaining your answer. It will help the questioner to understand the concept well.

Happy Codin' :)​

Answers

Answered by nitishshaw720
1

Explanation:

String’s replace function takes either two chars or two Char Sequences as arguments and it will replace all occurrences of char or String but replaceAll function method takes regex String as argument and replaces each substring of that matches given regex with replacement string

Answered by Anonymous
5

Interesting question - Java

Before we know the answer and the reason of this question, let's first know about that what is java and what is programming.

Java

Java is a high level programming language, it was developed by Sun Microsystems in the early early 1990s, it was designed by different platforms.

Programming

Programming means to create or develop a software which is called a program that contains the instructions to tell a computer what to do.

\rule{300}{1}

The Answer

Yes, both \tt replace() and \tt replaceAll() method are same. Both \tt replace() and \tt replaceAll() method replace the sequence in the given string. Both return type is String.

But yea only one difference are there in \tt replace() and \tt replaceAll() method that the \tt replace() method replace all the sequence of old char with new char and the \tt replaceAll() method replace all the sequence of old string with new string. But these both replace method work similarly.

In \tt replace() method all sequence should be in char while in \tt replaceAll() method all sequence should be in string. Suppose if you want to replace any char, then conjointly you ought to use single quotes('k'), and if you want to replace any string then conjointly you ought to use double quotes("k").

\rule{300}{1}

Let's take one-one example of both replace methods for better understanding.

Example of replace() method

public class replace_example

{

⠀ public static void main(String[] args) {

⠀⠀⠀String str;

⠀⠀⠀str="Brainly Is The best Learning App.";

⠀⠀⠀System.out.println(str.replace('b','B'));

⠀ }

}

>>> Brainly Is The Best Learning App.

Example of replaceAll() method

public class replaceAll_example

{

⠀ public static void main(String[] args) {

⠀⠀⠀String str;

⠀⠀⠀str="Brainly Is The best Learning App.";

⠀⠀⠀System.out.println(str.replaceAll("b","B"));

⠀ }

}

>>> Brainly Is The Best Learning App.

So from both example we can conclude that the both \tt replace() and \tt replaceAll() are same, these both methods works similarly.

\rule{300}{1}

Note: Do not copy-paste the exact c‎օde and run it, there are some white-space and no-space characters that will need to be removed before use, I've entered them for the sake of showing indentations in the c‎օde.

Similar questions