Results 1 to 7 of 7

Thread: How do I....

  1. #1

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

    How do I....

    How do i search a string for a certain word, then replace that word with something else.

    For example:
    String sentance="This is an example";
    String searchword="is";
    String replaceword="was";

    What I need to do is be able to search sentance for the string "is" and replace it with the string "was"

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    There's a replace method in java.lang.String.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Addicted Member ddmeightball's Avatar
    Join Date
    Nov 2004
    Location
    Nebraska
    Posts
    183
    since I am using strings, should I use the replaceAll() method?

    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.

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Posted by ddmeightball

    since I am using strings, should I use the replaceAll() method?
    Yes use the replaceAll(String regex, String replacement) method. replace(char oldChar, char newChar) is just used if you want to replace
    a character with another.
    Code:
    public class ReplaceEx{
      public static void main(String[] args){
       String s = "Hello my name is bill"; 
       String s2 = s.replaceAll("bill", "Jim");
       System.out.println(s);
       System.out.println(s2);
      }
    }

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Java 1.5 has a replace(CharSequence, CharSequence) which I would recommend over replaceAll, because it is faster.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    replace(CharSequence target, CharSequence replacement)
    Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.

    replaceAll(String regex, String replacement)
    Replaces each substring of this string that matches the given regular expression with the given replacement.

    Aren't they the same?

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The latter uses regular expression matching, which allows advanced features, but consequently is far slower than the plain-text matching of the former. Also, you don't have to worry about escaping characters in the former.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

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