commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jon Skeet" <jon.sk...@peramon.com>
Subject RE: [Lang] Repost of StringReplace code as attachment
Date Fri, 11 Oct 2002 09:56:22 GMT
> Ah. Now I know why I didn't get far with this :)
> 
> Your attachment was a: winmail.dat file which contained the source
> 'kinda'. Was surrounded in a bunch of binary bits and pieces.

Blech. Yes, I noticed yesterday that for some reason I'd got Outlook set up to always send
in Outlook RTF. Apologies.

> Do you want to just dump the source into an email? 

I put it in the first one - reposted here below anyway, along with attachment just to give
a belt and braces approach.

> Alternatively, I think
> I can get most of it out of the .dat file. Just being lazy/tired :)

Oh there's no reason to do that, certainly :)

Here's the code again. I've already started thinking about ways of optimising it further,
but I'm happy to wait until I see whether or not there's any interest in it in the first place
before going too far.

    /**
     * Returns a String with all occurrences of <code>from</code>
     * within <code>orig</code> replaced with <code>to</code>. If
     * <code>orig</code> contains no occurrences of
     * <code>from</code>, or if <code>from</code> is equal to
     * <code>to</code>, <code>orig</code> itself is returned rather
     * than a copy being made. None of the parameters should be
     * null.
     * 
     * @param orig the original String. Must not be null.
     * @param from the String to replace within <code>orig</code>.
     * Must not be null.
     * @param to the String to replace <code>from</code> with. Must
     * not be null.
     * 
     * @return a version of <code>orig</code> with all occurrences
     * of <code>from</code> being replaced with <code>to</code>.
     * 
     * @throws IllegalArgumentException if <code>from</code> is empty
     */
    public static String replace (String orig, String from, String to)
    {
        int fromLength = from.length();
        
        if (fromLength==0)
            throw new IllegalArgumentException 
            ("String to be replaced must not be empty");
        
        int start = orig.indexOf (from);
        if (start==-1)
            return orig;
        
        boolean greaterLength = (to.length() >= fromLength);
        
        StringBuffer buffer;
        // If the "to" parameter is longer than (or
        // as long as) "from", the final length will
        // be at least as large
        if (greaterLength)
        {
            if (from.equals (to))
                return orig;
            buffer = new StringBuffer(orig.length());
        }
        else
        {
            buffer = new StringBuffer();
        }
        
        char [] origChars = orig.toCharArray();
        
        int copyFrom=0;
        while (start != -1)
        {
            buffer.append (origChars, copyFrom, start-copyFrom);
            buffer.append (to);
            copyFrom=start+fromLength;
            start = orig.indexOf (from, copyFrom);
        }
        buffer.append (origChars, copyFrom, origChars.length-copyFrom);
        
        return buffer.toString();
    }

Jon

Mime
View raw message