Home >Java >javaTutorial >Replace() Function in Java
The replace() function in Java is used to remove a particular letter or character sequence and put another letter or character sequence in its place. After the introduction of JDK 1.5, this function “replace()” was introduced. Before this function, a core logic could have been written to ease up this functionality by encapsulating the code logic in the function named replace(). This function reduces the work of coders as they can directly use this function to take two input parameters and return a new user modified string. This can be used as per the business requirements.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
There are some other variants of replace function as well, like “replaceAll()”, “replaceFirst()”, which use the regular expression to manipulate the string.
Syntax:
public String replace(char oldcharacter, char newcharacter)
Here this function has an access modifier “public”, allowing it to be used by other functions as well. String type designated that this function will have a return type of “string”. The input parameters are passed in the form of two-character variables named “oldcharacter” and “newcharacter”. These variables will be used to scan the character to be replaced, and then logic in the function will work to replace this character with the new one sourcing from the “newcharacter” variable.
Parameters:
Return Value: This function returns a string with the old characters replaced with the new ones.
The internal code logic of replace () function is given below with an explanation.
Note: This is not a running code. It is a code logic on which replace function works.Here the function named “replacefunction” is actually a “replac” function in Java. This function will work only when the character to be replaced is different from the character that should be placed in replaced value. In the case in string “abcdecd”, “d’ should be replaced by “d” itself; in that case, the same string will be outputted rather than entering into unnecessary logic of this function. Once the control enters into a function, all necessary checks are done to determine the value that needs to change.
Variable “oldcharacter” and “newcharacter” are used to get input parameters for this function. These variables are then used in further function while replacing the values. Variable “characterlen” is used to store the length of the character string from which the value should be scanned out and changed. Char array “valtobereplaced” is used to store the value which needs a change. This array is declared in case multiple characters of a character sequence should be changed. Array works to store multiple characters at a time. The new character array “buffer” is used to store the modified string, which is created after replacing the old character with the new ones. This string is then returned as an output from this function.
Code:
public String replacefunction(char oldcharacter, char newcharater) { if (oldcharacter != newcharater) { int characterlen = value.length; int k = -1; char[] valtobereplcaed = value; while (++k < characterlen) { if (valtobereplcaed[k] == oldcharacter) { break; } } if (k < characterlen) { char buffer[] = new char[characterlen]; for (int j = 0; j < k; j++) { buffer[j] = valtobereplcaed[j]; }
Below is the core logic to replace the particular character with a new one. Here, while loops designate, we have to keep control in this loop till we do not reach the end of the string. Here variable to be replaced, which is being carried forward from the start, is parked in character variable “c”. The conditional statement is put then where if character “c” matched with the “oldcharacter” variable, then the value of “c” should be changed with “newcharacter”; otherwise “, c” should be retained as it is.
Code:
while (k < characterlen) { char c = valtobereplcaed[k]; buffer[k] = (c == oldcharacter) ? newcharater : c; k++; } return new String(buffer, true); } }
The below example demonstrates the working of replace function in the JAVA language. It has got two parameters as input and returns a changed string after replacing the targeted character or character sequence from the input string.
Code:
public class test { public static void main(String args[]) { // In the below line a new string Str_new is being created. For this to implement a new string object is being introduced. String Str_new = new String("dEmonsRating the functionality of REplacE function"); // Below code explains the use of replace function. This function returns a string as a return value. // This returned value is being captured in print statement. System.out.print("This is string after replacing all Rs with Ks : " ); System.out.println(Str_new.replace('R', 'K')); // The below line will work the same as previous code line only with small changes made with input parametrs provided to this function. System.out.print("This is string after replacing all E with U : " ); System.out.println(Str_new.replace('E', 'U')); } }
Output:
Hence replace() function is very useful when we need a clean way to replace anything from another in a string. It is extensively used in the JAVA programming language for string manipulation purposes during logic building.
The above is the detailed content of Replace() Function in Java. For more information, please follow other related articles on the PHP Chinese website!