String.replaceAll function

I know that if Java 1.4.2 I can use String.replaceall to replace all of the character I specified to another specified character .
However, I am using Java 1.3.1 and I can't use string.replaceall function.  would somebody please teach me how to solve the problem ?

I want to have a function that does the same as string.replaceall. Thanks.

mikekwokAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zzynxSr. Software engineerCommented:
What about
public String replace(char oldChar, char newChar)
?
zzynxSr. Software engineerCommented:
Write your own using the functions indexOf() and substring()
riaancorneliusCommented:
just use a recursive function, that keeps replacing the character, until indexOf() returns -1:
something like this:

public String replaceAll(String oldString, String value, String newValue){
  String newString = oldString.replace(oldValue, newValue)
  if( newString.indexOf(value)!=-1 )
    return replaceAll( newString, value, newValue );
  else
    return newString;
}
   
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

mikekwokAuthor Commented:
What I want to do is :

Check for string if it contain ' , if it is true , then replace the single quote to 2 single quote .... ''

would you please help?
zzynxSr. Software engineerCommented:
       char singleQuote = '\'';
        char doubleQuote = '\"';
        System.out.println("hello ' all !".replace(singleQuote,doubleQuote));
zzynxSr. Software engineerCommented:
>> single quote to 2 single quote
Sorry. The previous replaced the single quote by a double quote.
zzynxSr. Software engineerCommented:
>> replace the single quote to 2 single quote

    public static String doubleTheQuotes(String input) {
        StringBuffer buf = new StringBuffer(input.length());
        for (int i=0; i<input.length(); i++) {
            char c = input.charAt(i);
            if (c=='\'')
                buf.append("''");  // That's a double quote, a single quote, a single quote and a double quote ;°)
            else
                buf.append(c);
        }
        return buf.toString();
    }
zzynxSr. Software engineerCommented:
Otherwise written:

    public static String doubleTheQuotes(String input) {
        char originalChar = '\'';
        String newChars = "''";       // replace this line by  char newChar = '\"'; if you want one double quote

        StringBuffer buf = new StringBuffer(input.length());
        for (int i=0; i<input.length(); i++) {
            char c = input.charAt(i);
            if (c==originalChar)
                buf.append(newChars);
            else
                buf.append(c);
        }
        return buf.toString();
    }

Initialize 'originalChar' and 'newChars' according to your needs
:°)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.