- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 24
Lesson 24: How to use replace() method in Java
When working with values of string type as a programmer, you may need a function to find a character or group or characters in a given string and replace them with other characters.
Java has inbuilt methods that can be used to replace a character or a group of characters with other characters in a string. The particular method we shall discuss here is replace() method.The syntax of using replace() method in Java is shown below:
Original_string.replace(find_string, replace_string);
The original string comes first before the replace() method, inside the brackets, you first put the string to find then the string that you need to replace. These two strings in the brackets must be separated by a comma.
Replace function can be used in many applications especially when one want to change a single word or several words in a paragraph or document. The replace function has been used in many word processing applications.
Remember the “Find and Replace” function provided in Microsoft Word application? Take a look on the image below:
“Find and Replace” function in Microsoft Word 2010 application
Now, suppose we have a sentence that has a word that we need to find and replace. Let us use the example of a sentence given below:
Java is an object oriented language
In the above sentence, we want to replace the word “language” with “programming language.” Using the Java replace() method, let us write a program to find and replace characters, word or words in a sentence or paragraph. Copy and paste or type the code shown below.
Java program using replace() method
package myfirstprogram; import javax.swing.JOptionPane; public class Replace { public static void main(String[] args) { String sentence = "Java is an object oriented language"; String findString; String replaceString; String amend; //Display string before changes JOptionPane.showMessageDialog( null,"Original sentence: "+sentence); //Find string to replace findString = JOptionPane.showInputDialog("Find what:"); //Provide replacement string replaceString = JOptionPane.showInputDialog("Replace with:"); //Replace and store the new string in variable 'amend' amend = sentence.replace(findString, replaceString); //Display final string JOptionPane.showMessageDialog( null,"Final sentence: "+amend); } }
Java replace() method has many variations as shown on the image below, try the different variations and test what each will produce.
Java has many useful inbuilt methods and I recommend that you check on the others that we have not discussed in this tutorial and also practice on how to use them. In the next lesson, we shall learn how we can write our own methods in Java programming language.
<< Lesson 23 | Lesson 25 >>
Other related hubs...
- Programming In Java NetBeans - A Step By Step Tutorial For Beginners: Lesson 9
In this lesson we shall learn how to use the Switch statement in Java programming. Switch statement is a selection statement, that means when used, it select one value among many values. - Programming In Java NetBeans - A Step By Step Tutorial For Beginners: Lesson 11
The WHILE loop in Java execute a statement or a group of statements so long as the specified condition remains TRUE. The loop starts with the keyword “while”. Just after the keyword you open the round brackets and specify the condition to be tested. - Programming In Java NetBeans - A Step By Step Tutorial For Beginners: Lesson 3
In Lesson 2, we saw how the Java code window looks like and we were able to interpret the various parts of the Java code. In this lesson we’ll learn how to write our first running Java program. - Programming In Java NetBeans - A Step By Step Tutorial For Beginners: Lesson 10
The FOR loop is one of the most common looping controls used in Java. The FOR loop forces the program to repeat a statement or a group of statements a specified number of times. It has three parts; the initial value part, condition part, and ...