Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Need help in replacing a words inside a string
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
WooFerPPK




PostPosted: Wed Mar 24, 2004 8:01 pm   Post subject: Need help in replacing a words inside a string

i need to make a program that will find a word inside a sentence then replace that word with another word i type.

i cannot fiqure out how to replace the word without deleting the string and just getting the word

this is what i have so far

code:
    private static int searchAndReplace (String origStr, String searchStr,String replaceStr)
    {
        int findIndex = origStr.indexOf (searchStr);

        while (findIndex >= 0)
        {

            findIndex = origStr.indexOf (searchStr, findIndex + 1);
        }

    }


Thanks for your help
(btw, i like what you guys did to the site 8) )
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Wed Mar 24, 2004 8:08 pm   Post subject: (No subject)

well you use indexOf to find where the string to be replaced is located. Then you construct a new string in this order

everything to the left of index + replacement string + everything to the right of (index + length of string to replace)
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: