[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
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
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.
The Answer
Yes, both and method are same. Both and method replace the sequence in the given string. Both return type is String.
But yea only one difference are there in and method that the method replace all the sequence of old char with new char and the method replace all the sequence of old string with new string. But these both replace method work similarly.
In method all sequence should be in char while in 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").
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 and are same, these both methods works similarly.
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.