Find and replace a string using java awt source code: This simple java example tutorial performs the string operations as well. It is used to find a particular string in the given sequence of text, and replace with another word. This program can be done easily by using the Java AWT class. This source code can be used for various kind of students and professionals as well. An example tutorial is given below for your reference.
This java awt program is to find the string in a document, and replace another text. Find the string value and the string you want to replace is entered in a replace string value. This string operations are gets done using awt class.
Find & Replace Text - Java AWT Code:
Q: Write a java program to find and replace string using AWT class.
[Useful: C Programming Guide] & [IT Companies List]
//Import statements import java.awt.*; import java.awt.event.*; //Class definition class Find extends Frame implements ActionListener { //Variables declaration TextArea tax; TextField te1, te2; Label la1, la2; Button bu1, bu2, bu3, bu4; int startindex, ln, i; public Find(String title) { super(title); i=startindex=ln=0; tax=new TextArea(10,50); te1=new TextField(10); te2=new TextField(10); la1=new Label("Find What:"); la2=new Label("Replace With:"); bu1=new Button("Find"); bu2=new Button("Replace"); bu3=new Button("Replace All"); bu4=new Button("Cancel"); setLayout(new FlowLayout()); bu1.addActionListener(this); bu2.addActionListener(this); bu3.addActionListener(this); bu4.addActionListener(this); add(tax); add(la1); add(te1); add(la2); add(te2); add(bu1); add(bu2); add(bu3); add(bu4); } // © http://students3k.com – Karthikh Venkat public void actionPerformed(ActionEvent e) { String findwhat=new String(""); String replacewith=new String(""); String text=tax.getText(); findwhat=te1.getText(); ln=findwhat.length(); replacewith=te2.getText(); if(e.getSource()==bu1)//Decision making statement { startindex=text.indexOf(findwhat,i); tax.select(startindex,startindex+ln); i=startindex+ln; } else if(e.getSource()==bu2) ta.replaceRange(replacewith,startindex,startindex+len); else if(e.getSource()==bu3) { //For statement for(int i=0;i<text.length();i=startindex+ln ) { startindex=text.indexOf(findwhat,i); tax.replaceRange(replacewith,startindex,startindex+ln); } } // © http://students3k.com – Karthikh Venkat } public static void main(String args[]) { Find f=new Find("Find and Replace"); f.setSize(400,400); f.show(); } }
- SEE: All Exam Results