Results 1 to 6 of 6

Thread: String Replace??

  1. #1

    Thread Starter
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    String Replace??

    Is there a function in the Java API to replace strings ? There are methods in the String and StringBuffer objects to replace a single character, but is there any such method for replacing an entire string?

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  2. #2
    Lively Member gigsvoo's Avatar
    Join Date
    Oct 2000
    Location
    Malaysia
    Posts
    109
    Look into method like getString() or StringBuffer() will give u some clue on that...

  3. #3
    DaoK
    Guest
    myString.Replace('a',b');

  4. #4

    Thread Starter
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Originally posted by gigsvoo
    Look into method like getString() or StringBuffer() will give u some clue on that...
    I searched the Java Language API for JDK 1.2.2 and have found nothing in the String or the StringBuffer class that can give me a clue.

    Daok, excuse me, but I have said already that I need a function to replace strings, and not individual characters.

    .
    Last edited by honeybee; Feb 8th, 2002 at 12:37 PM.
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  5. #5
    Addicted Member
    Join Date
    Nov 2001
    Location
    Yewston, Texis
    Posts
    240
    Here y'go honeybee, add this puppy to your class:

    Code:
      /**
       * General purpose utility for searching and replacing strings
       */
      public String replace(String source, String find, String replace) {
        // bail if nothing to look for or replace
        String retval = "";
        if((find.length() == 0) ||
          ((replace.length() == 0))) return source; 		
          StringTokenizer st = new StringTokenizer(source, find, true);
          try {
            while(st.hasMoreTokens()) {	
            String token = st.nextToken();
            retval += token.equals(find) ? replace : token;
          }
        } catch(Exception e) { // shouldn't happen }	
      }
        return retval;
      }

  6. #6
    Member hgroot's Avatar
    Join Date
    Dec 2001
    Location
    Amsterdam
    Posts
    52
    sigh.... here is some code that works...



    Code:
    Public class Test {
    	Public static void main(String[] args) {
    		System.out.println(Test.replaceStr("ThisABCisABCaABCtest", "ABC", " "));
    	}
    
    	Public static String replaceStr(String replace, String oldStr, String newStr) {
    		While (replace.indexOf(oldStr) != -1) {
    			replace = replace.substring(0, replace.indexOf(oldStr)) +
    				newStr + replace.substring(replace.indexOf(oldStr) + oldStr.length());
    		}
                    return replace;
    	}	
    }

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