Results 1 to 8 of 8

Thread: Recieving a string to search, then replace

Threaded View

  1. #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.

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