Results 1 to 8 of 8

Thread: Recieving a string to search, then replace

  1. #1

    Thread Starter
    Addicted Member ddmeightball's Avatar
    Join Date
    Nov 2004
    Location
    Nebraska
    Posts
    183

    Recieving a string to search, then replace

    I am creating a class called Editor that is able to perform a search/replace function on a document (a sentence or paragraph).

    The Editor class includes two constructors:

    • The first has no parameters
    • The second has one parameter (the document to be searched)

    The class includes a setDocument() and a getDocument() method.

    The class includes two overloaded versions of a method called replace():

    • The first has two parameters (search text, replacement text) and works on the current document.
    • The second receives three parameters (the document to be searched, the search text, the replacement text).
    • The replace method returns the new document as a String.

    The replace method searches the document for instances of the search text, replaces that text with the replacement text.
    Example:

    document: “Rich is here”
    search text: “is”
    replacement text: “was”
    result: “Rich was here”

    What I need to know is how would I go about searching, then replacing the text in the string that I am searching?
    Last edited by ddmeightball; Nov 29th, 2004 at 07:14 AM.

  2. #2

    Thread Starter
    Addicted Member ddmeightball's Avatar
    Join Date
    Nov 2004
    Location
    Nebraska
    Posts
    183
    Here is my code
    Code:
    /**
     * Title:Editor project
     * @author Matthew Davis
     * Class Info 1314
     * Date: November 22, 2004
     **/
     package Editor;
     import java.util.StringTokenizer;
    
     public class Editor 
     { 
      	String document;
      	String searchText;
      	String replacementText;
    
     public Editor() 
      {
      }//end constructor 
      
     public void setDocument(String recdocument)
     	{
    		document=recdocument;
        }
     
     public String getDocument()
     	{
    		return document;
        }
    
    public String replace(String searchText, String replacementText)
    	{
              String [] token = new String[10];
    
              StringTokenizer sTokens = new StringTokenizer(document, ",");
    
              int count = 0;
              while(sTokens.hasMoreTokens()) 
              	{
          		  	token[count++] = sTokens.nextToken().trim();
         	    }
    
              String resultStr = "";
              for(int i = 0; i < count; i++)
              	{
              		 if(token[i].equalsIgnoreCase(searchText))
             	 	 	{
             	 	 		resultStr = resultStr.replaceAll(token[i],replacementText);
            	  		}
            	  	 else
            	  	 	{
            	  	 		resultStr += token[i] + ", ";
            	  		}
          	    }
            	 
              resultStr = resultStr.substring(0, resultStr.trim().length() - 1);
              return resultStr.toString();
    	}
    
    public String replace(String document, String searchText, String replacementText)
    	{
              String [] token = new String[10];
    
              StringTokenizer sTokens = new StringTokenizer(document, ",");
    
              int count = 0;
              while(sTokens.hasMoreTokens()) 
              	{
          		  	token[count++] = sTokens.nextToken().trim();
         	    }
    
              String resultStr = "";
              for(int i = 0; i < count; i++)
              	{
              		 if(token[i].equalsIgnoreCase(searchText))
             	 	 	{
             	 	 		resultStr = resultStr.replaceAll(token[i],replacementText);
            	  		}
            	  	 else
            	  	 	{
            	  	 		resultStr += token[i] + ", ";
            	  		}
          	    }
            	 
              resultStr = resultStr.substring(0, resultStr.trim().length() - 1);
              return resultStr.toString();
    	}
    
    }
    Last edited by ddmeightball; Nov 29th, 2004 at 03:12 PM.

  3. #3
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    search for RegEx or Regular Expression. Dunno this thing but it will help you.

    edit: Please use code tags when posting code. [code][/code]

  4. #4

    Thread Starter
    Addicted Member ddmeightball's Avatar
    Join Date
    Nov 2004
    Location
    Nebraska
    Posts
    183
    Can anyone else help me?

  5. #5
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994
    Use the String types methods, look it up on the java docs.
    "Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
    - Zack de la Rocha


    Hear me roar.

  6. #6

    Thread Starter
    Addicted Member ddmeightball's Avatar
    Join Date
    Nov 2004
    Location
    Nebraska
    Posts
    183
    Originally posted by vbNeo
    Use the String types methods, look it up on the java docs.
    what do you mean by string types methods and what are the java docs?

    I am very new and java and just have been having some trouble even writing anything.

  7. #7

  8. #8

    Thread Starter
    Addicted Member ddmeightball's Avatar
    Join Date
    Nov 2004
    Location
    Nebraska
    Posts
    183
    Thank you everyone that helped me. I figured it out. My main problem was my delimiter on my string tokenizer. thanks again for the help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width