You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Emmanuel Boudrant <em...@gmail.com> on 2004/09/12 16:34:49 UTC
call replace('.', '_') on a VTL variabel
Hi,
Is it possible to call a java.lang.String method on a variable like this :
#set ($codedName = $name.replace('.', '_'))
What is exactly the type of variable $name (java.lang.String?)
Thx,
-emmanuel
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: call replace('.', '_') on a VTL variabel
Posted by Christoph Reck <ap...@recks.org>.
Anyone though about using a macro instead of a tool?
Next is untested code... but probably works and should
give the idea:
#* Utility macro to do character repacement in a string *#
#macro( replace $string $from $to )##
$string.replace($from.charAt(0), $to.charAt(0))##
#end
Whoever tests this and ensures it worked, please reply here
or add it to the Wiki macro collections.
Note that it may throw runtime exceptions if the macro
parameters are unproper...
Cheers,
Christoph
Emmanuel Boudrant wrote:
> Hi,
>
> Is it possible to call a java.lang.String method on a variable like this :
>
> #set ($codedName = $name.replace('.', '_'))
#set( $from = '.' )
#set( $to = '_' )
#set( $codedName = $name.replace($from.charAt(0), $to.charAt(0)) )
or using the macro above (and string interpolation...)
#set( $codedName = "#replace($name '.' '_')" )
>
> What is exactly the type of variable $name (java.lang.String?)
>
> Thx,
> -emmanuel
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: call replace('.', '_') on a VTL variabel
Posted by Shinobu Kawai <sh...@gmail.com>.
Hi emmanuel,
> If I understand, it is impossible to call methods with primitives
> types as parameters in velocity ?
You can use their wrapper classes instead.
ie. int -> Integer, boolean -> Boolean, etc.
Best regards,
-- Shinobu Kawai
--
Shinobu Kawai(shinobu.kawai@gmail.com)
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: call replace('.', '_') on a VTL variabel
Posted by Emmanuel Boudrant <em...@gmail.com>.
If I understand, it is impossible to call methods with primitives
types as parameters in velocity ?
-emmanuel
On Sun, 12 Sep 2004 19:14:11 +0200, Emmanuel Boudrant
<em...@gmail.com> wrote:
> Ok Thx ... I don't want to modify velocity context (add a tool), I
> will use replaceAll method.
>
> -emmanuel
>
>
>
> On Sun, 12 Sep 2004 13:06:58 -0400, Terry Steichen <te...@net-frame.com> wrote:
> > Correct, it won't work. However, you can easily add a replace(String mainString, String toReplaceString, String replacementString) method to a tool. Then, on a string varb $name, do something like $tool.replace($name,".", "_"),
> >
> >
> >
> > ----- Original Message -----
> > From: Emmanuel Boudrant
> > To: Velocity Users List ; Shinobu Kawai
> > Sent: Sunday, September 12, 2004 12:52 PM
> > Subject: Re: call replace('.', '_') on a VTL variabel
> >
> > You mean with #set ($codedName = $name.replace('.', '_')) '.' and '_'
> > are String, not char. So it doesn't work ?
> >
> > On Mon, 13 Sep 2004 00:58:41 +0900, Shinobu Kawai
> > <sh...@gmail.com> wrote:
> > > Hi emmanuel,
> > >
> > >
> > >
> > > > Is it possible to call a java.lang.String method on a variable like this :
> > > >
> > > > #set ($codedName = $name.replace('.', '_'))
> > > >
> > > > What is exactly the type of variable $name (java.lang.String?)
> > > Whatever type of object you put in it. If it's a primitive type, it
> > > would be the wrapper object for it. i.e. int becomes Integer.
> > >
> > > So the answer to your first question would be, yes, if you put a String
> > > in $name, and String has a public method #replace(String, String). I
> > > don't know about the first half, but the second half is false. You will
> > > have to use #replaceAll(String, String), or create Character variables
> > > to give to #replace(char, char).
> > >
> > > Best regards,
> > > -- Shinobu Kawai
> > >
> > > --
> > > Shinobu Kawai(shinobu.kawai@gmail.com)
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> > > For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> > >
> > >
> >
> > ---------------------------------------------------------------------
> >
> >
> > To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> >
> >
>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: call replace('.', '_') on a VTL variabel
Posted by Emmanuel Boudrant <em...@gmail.com>.
Ok Thx ... I don't want to modify velocity context (add a tool), I
will use replaceAll method.
-emmanuel
On Sun, 12 Sep 2004 13:06:58 -0400, Terry Steichen <te...@net-frame.com> wrote:
> Correct, it won't work. However, you can easily add a replace(String mainString, String toReplaceString, String replacementString) method to a tool. Then, on a string varb $name, do something like $tool.replace($name,".", "_"),
>
>
>
> ----- Original Message -----
> From: Emmanuel Boudrant
> To: Velocity Users List ; Shinobu Kawai
> Sent: Sunday, September 12, 2004 12:52 PM
> Subject: Re: call replace('.', '_') on a VTL variabel
>
> You mean with #set ($codedName = $name.replace('.', '_')) '.' and '_'
> are String, not char. So it doesn't work ?
>
> On Mon, 13 Sep 2004 00:58:41 +0900, Shinobu Kawai
> <sh...@gmail.com> wrote:
> > Hi emmanuel,
> >
> >
> >
> > > Is it possible to call a java.lang.String method on a variable like this :
> > >
> > > #set ($codedName = $name.replace('.', '_'))
> > >
> > > What is exactly the type of variable $name (java.lang.String?)
> > Whatever type of object you put in it. If it's a primitive type, it
> > would be the wrapper object for it. i.e. int becomes Integer.
> >
> > So the answer to your first question would be, yes, if you put a String
> > in $name, and String has a public method #replace(String, String). I
> > don't know about the first half, but the second half is false. You will
> > have to use #replaceAll(String, String), or create Character variables
> > to give to #replace(char, char).
> >
> > Best regards,
> > -- Shinobu Kawai
> >
> > --
> > Shinobu Kawai(shinobu.kawai@gmail.com)
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> >
> >
>
> ---------------------------------------------------------------------
>
>
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: call replace('.', '_') on a VTL variabel
Posted by Terry Steichen <te...@net-frame.com>.
Correct, it won't work. However, you can easily add a replace(String mainString, String toReplaceString, String replacementString) method to a tool. Then, on a string varb $name, do something like $tool.replace($name,".", "_"),
----- Original Message -----
From: Emmanuel Boudrant
To: Velocity Users List ; Shinobu Kawai
Sent: Sunday, September 12, 2004 12:52 PM
Subject: Re: call replace('.', '_') on a VTL variabel
You mean with #set ($codedName = $name.replace('.', '_')) '.' and '_'
are String, not char. So it doesn't work ?
On Mon, 13 Sep 2004 00:58:41 +0900, Shinobu Kawai
<sh...@gmail.com> wrote:
> Hi emmanuel,
>
>
>
> > Is it possible to call a java.lang.String method on a variable like this :
> >
> > #set ($codedName = $name.replace('.', '_'))
> >
> > What is exactly the type of variable $name (java.lang.String?)
> Whatever type of object you put in it. If it's a primitive type, it
> would be the wrapper object for it. i.e. int becomes Integer.
>
> So the answer to your first question would be, yes, if you put a String
> in $name, and String has a public method #replace(String, String). I
> don't know about the first half, but the second half is false. You will
> have to use #replaceAll(String, String), or create Character variables
> to give to #replace(char, char).
>
> Best regards,
> -- Shinobu Kawai
>
> --
> Shinobu Kawai(shinobu.kawai@gmail.com)
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: call replace('.', '_') on a VTL variabel
Posted by Emmanuel Boudrant <em...@gmail.com>.
You mean with #set ($codedName = $name.replace('.', '_')) '.' and '_'
are String, not char. So it doesn't work ?
On Mon, 13 Sep 2004 00:58:41 +0900, Shinobu Kawai
<sh...@gmail.com> wrote:
> Hi emmanuel,
>
>
>
> > Is it possible to call a java.lang.String method on a variable like this :
> >
> > #set ($codedName = $name.replace('.', '_'))
> >
> > What is exactly the type of variable $name (java.lang.String?)
> Whatever type of object you put in it. If it's a primitive type, it
> would be the wrapper object for it. i.e. int becomes Integer.
>
> So the answer to your first question would be, yes, if you put a String
> in $name, and String has a public method #replace(String, String). I
> don't know about the first half, but the second half is false. You will
> have to use #replaceAll(String, String), or create Character variables
> to give to #replace(char, char).
>
> Best regards,
> -- Shinobu Kawai
>
> --
> Shinobu Kawai(shinobu.kawai@gmail.com)
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: call replace('.', '_') on a VTL variabel
Posted by Shinobu Kawai <sh...@gmail.com>.
Hi emmanuel,
> Is it possible to call a java.lang.String method on a variable like this :
>
> #set ($codedName = $name.replace('.', '_'))
>
> What is exactly the type of variable $name (java.lang.String?)
Whatever type of object you put in it. If it's a primitive type, it
would be the wrapper object for it. i.e. int becomes Integer.
So the answer to your first question would be, yes, if you put a String
in $name, and String has a public method #replace(String, String). I
don't know about the first half, but the second half is false. You will
have to use #replaceAll(String, String), or create Character variables
to give to #replace(char, char).
Best regards,
-- Shinobu Kawai
--
Shinobu Kawai(shinobu.kawai@gmail.com)
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org