velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Bill Kaufman" <BKauf...@zaplet.com>
Subject RE: how can i replace char in a $variable
Date Fri, 27 Sep 2002 17:27:47 GMT
> Why are you doing this in Velocity ?

Well, yeah, I wasn't _recommending_ this,...

(BTW, the code you supplied won't work, for the same reason String.replace(char,char) won't
work: Velocity will never realize that a one-character String is equivalent to char.  Plus,
the function takes three args but you passed in two.)

Here's some code I have tested.  It also works on Strings of length other than 1.

vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    /**
     * Replace one string with another.
     * @param source The source string.
     * @param oldString The string in <code>source</code> to replace.
     * @param newString The string to replace <code>oldString</code> with.  If
     * this is <code>null</code> or empty, <code>oldString</code>
is simply
     * removed.
     * @return The source string with all occurrences of <code>oldString</code>
     * replaced with <code>newString</code>.
     */
    public String replace(String source, String oldString, String newString) {
        int oldLen = (oldString == null ? 0 : oldString.length());
        int idx;

        // If there's nothing to replace, return the original string
        if (source == null || oldLen == 0 ||
            (idx = source.lastIndexOf(oldString)) < 0)
            return source;

        if (newString == null) {
            // Treat null as empty string
            newString = "";
        } else if (oldLen == 1 && newString.length() == 1) {
            // Optimization: Use String.replace(char,char) where possible
            return source.replace(oldString.charAt(0), newString.charAt(0));
        }

        StringBuffer target = new StringBuffer(source);
        do {
            // Replace the region in the duplicate buffer
            target.replace(idx, idx+oldLen, newString);

            // Look for the next occurence in the source
            idx = (idx == 0 ? -1 : source.lastIndexOf(oldString, idx-1));
        } while (idx >= 0);

        return target.toString();
    }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

                                                      -- Bill K.


> -----Original Message-----
> From: Guido Sohne [mailto:guido@sohne.net]
> Sent: Friday, September 27, 2002 10:58 AM
> To: Velocity Users List
> Subject: Re: how can i replace char in a $variable
> 
> 
> Why are you doing this in Velocity ?
> You should be doing this type of thing in Java and
> place an object into the Velocity context that has
> this type of functionality.
> 
> You should be doing something like this instead:-
> 
> 	$string.replace(' ', '_')
> 
> and you should have a java class like this instead
> 
> public class StringUtil {
> 
> 	public String replace(String s, char oldChar, char newChar) {
> 		StringBuffer sb = new StringBuffer(s);
> 		while((int i = sb.indexOf(oldChar)) != -1) 
> 			sb.setCharAt(i) = newChar;
> 
> 		return sb.toString();
> 	}
> 
> }
> 
> then when you are creating your context, you do
> 
> 	StringUtil su = new StringUtil();
> 
> 	context.put("string", su);
> 
> It's much better than doing wierd stuff in Velocity. In fact,
> that is the whole point of the Velocity design, to stop you
> from putting app logic into your display code.
> 
> I prefer saying how to do this right than retching because
> the workaround is truly disgusting.
> 
> caveat: I have not compiled or tested the code above!
> 
> On Fri, Sep 27, 2002 at 08:54:32AM -0700, Bill Kaufman wrote:
> > Ummm, OK,... It seems that Velocity can't cope with a 
> conversion from String (like ' ' or '_') to char.  An obvious 
> work-around would be to write your own tool or such like, 
> which accepts the three arguments as Strings.
> > 
> > But I've found a truly repulsive work-around:
> > 
> > 	#set( $var = "I'm a little teapot" )
> > 	#set( $oldC = ' ' )
> > 	#set( $newC = '_' )
> > 	$var.replace($oldC.charAt(0), $newC.charAt(0))
> > 
> > The congregation may now retch.
> > 
> >                                                       -- Bill K.
> > 
> > 
> > > -----Original Message-----
> > > From: niv [mailto:niv@movious.com]
> > > Sent: Thursday, September 26, 2002 8:27 AM
> > > To: 'velocity-user@jakarta.apache.org'
> > > Subject: how can i replace char in a $variable
> > > 
> > > 
> > > Hi Gurus.
> > > 
> > > I need to replace a space char with '_' in my velocity variable 
> > > 
> > >  
> > > 
> > > I can not do $var.replace(' ','_')
> > > 
> > > 
> > 
> > --
> > To unsubscribe, e-mail:   
<mailto:velocity-user-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail: <mailto:velocity-user-help@jakarta.apache.org>
> 

--
To unsubscribe, e-mail:   <mailto:velocity-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:velocity-user-help@jakarta.apache.org>


--
To unsubscribe, e-mail:   <mailto:velocity-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:velocity-user-help@jakarta.apache.org>


Mime
View raw message