JavaScript
Object Oriented JavaScript(OOJS) Tutorial with Example
What is OOPS Concept in JavaScript? Many times, variables or arrays are not sufficient to simulate...
The String Class Java has three types of Replace methods:
With the help of replace() function in Java, you can replace characters in your string. Lets study each Java string API functions in details:
Java String replace() method replaces every occurrence of a given character with a new character and returns a new string. The Java replace() string method allows the replacement of a sequence of character values.
Syntax:
public Str replace(char oldC, char newC)
Parameters:
oldCh − old character
newCh − new character
Return Value
The Java replace() function returns a string by replacing oldCh with newCh.
Example of replace() in Java:
Let's understand replace() in Java function with an example:
public class gtupapersEx1 {
public static void main(String args[]) {
String S1 = new String("the quick fox jumped");
System.out.println("Original String is ': " + S1);
System.out.println("String after replacing 'fox' with 'dog': " + S1.replace("fox", "dog"));
System.out.println("String after replacing all 't' with 'a': " + S1.replace('t', 'a'));
}
}
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
Java String replaceAll() method finds all occurrences of sequence of characters matching a regular expression and replaces them with the replacement string. At the end of call, a new string is returned by the function replaceAll() in Java.
Signature:
public Str replaceAll(String regex, String replacement)
Parameters:
regx: regular expression
replacement: replacement sequence of characters
Example:
public class gtupapersEx2 {
public static void main(String args[]) {
String str = "gtupapers is a site providing free tutorials";
//remove white spaces
String str2 = str.replaceAll("\\s", "");
System.out.println(str2);
}
}
Output:
gtupapersisasiteprovidingfreetutorials
Java String replaceFirst() method replaces ONLY the first substring which matches a given regular expression. Matching of the string starts from the beginning of a string (left to right). At the end of call, a new string is returned by the Java replaceFirst() function.
Syntax
public Str replaceFirst(String rgex, String replacement)
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 of replaceAll() in Java:
public class gtupapersEx2 {
public static void main(String args[]) {
String str = "This website providing free tutorials";
//Only Replace first 's' with '9'
String str1 = str.replaceFirst("s", "9");
System.out.println(str1);
}
}
Output:
Thi9 website providing free tutorials
What is OOPS Concept in JavaScript? Many times, variables or arrays are not sufficient to simulate...
What is = in JavaScript? Equal to (=) is an assignment operator, which sets the variable on the...
You can use JavaScript code in two ways. You can either include the JavaScript code internally within...
JavaScript is the most popular client-side scripting language supported by all browsers....
Insertion sort is a simple sorting algorithm suited for small data sets. During each iteration,...
How to read a file in Java? Java provides several mechanisms to read from File. The most useful...