FA/Replacer

From ggc

Jump to: navigation, search

01 package FA;
02 
03 
04 /**
05  * AltProject  Writing Replacer class.
06  @author Frank Anderson
07  */
08 
09 public class Replacer
10 {
11   /** target is the section of string to be replaced */
12   String target;
13   /**targed is replaced with the string, replacement. */
14   String replacement;
15   /** phrase is the string that is brought in transferred to Replacer.*/
16   String phrase;
17 
18   /** Replacer brings in the value of two string into the program and constructs
19     * the string  target   and    replacement. */
20   /** a is the expression to be replaced in the string.*/
21   /**  b is the replacement for string  a. */
22   public Replacer(String a, String b)
23   {
24     target = a;
25     replacement = b;
26   }
27   /** getReplaced brings in the string, source. It will then replace every
28     * substring equal to  target   with    replacement   and then return the
29     new string to the program which invoked this method.  */
30   /** @ param  source is the string whose characters are changed in  phrase.*/
31   /**@ return  the phrase to the calling program.*/
32   public String getReplaced(String source)
33   {
34     phrase=source;
35     boolean more = true;
36 
37     /** If the length of phrase is less than the length of target, then target
38       * can not be a substring of phrase. Return phrase.*/
39     if (phrase.length() < target.length())
40       return phrase;
41 
42     /** If phrase and target are the same, return phrase. */
43     if((phrase.length()==target.length())&&(target.equals(phrase)))
44       return replacement;
45 
46     /** Search phrase for all occurrences of  target and replace  with replacemet. */
47     while (more)
48     {
49       //System.out.println("Phrase is "+phrase);
50       more=false;
51       if(phrase.indexOf(target)>=0)
52       {
53         phrase=phrase.substring(0,phrase.indexOf(target))+ replacement +phrase.substring(phrase.indexOf(target)+target.length());
54         more=true;
55       }
56 
57     }
58     return phrase;
59   }
60 
61 }


Download/View FA/Replacer.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter