You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Huy Do <hu...@tramada.com> on 2001/03/20 02:28:31 UTC

Substring function

Hi,

i'm new to velocity and I was wondering if there was
a substring function built into velocity. I think this
would be useful as it is directly related to the presentation
of strings on a template.

Thanks

Huy

RE: Substring function

Posted by Huy Do <hu...@tramada.com>.
This is cool. I'll now go and change the turbine torque templates for
postgresql
so i can generate my first peers app.

Thanks alot for the rapid replies.

Huy

-----Original Message-----
From: gmj@mta4.srv.hcvlny.cv.net [mailto:gmj@mta4.srv.hcvlny.cv.net]On
Behalf Of Geir Magnusson Jr.
Sent: Tuesday, 20 March 2001 2:11 PM
To: velocity-user@jakarta.apache.org
Subject: Re: Substring function


Huy Do wrote:
>
> Hi,
>
> i'm new to velocity and I was wondering if there was
> a substring function built into velocity. I think this
> would be useful as it is directly related to the presentation
> of strings on a template.

I saw jon responded, but I may be able to provide a little discussion in
addition to his correct but terse suggestion :)

The upshot of what Jon was saying is that in Velocity, Strings from the
context are regular java String classes.  Also, strings you create w/in
the template are also String objects.  All the methods apply.

So you can do things like

#set($foo = "Bar" )
$foo.substring(0,1)

and the output is

B

or other string functions work :

#set($bar = "12345")
#set($len = $bar.length() - 1)
#foreach($i in [0..$len] )
$bar.charAt($i)
#end

which would produce
1
2
3
4
5

geir

--
Geir Magnusson Jr.                               geirm@optonline.net
Developing for the web?  See http://jakarta.apache.org/velocity/


Re: Substring function

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Huy Do wrote:
> 
> Hi,
> 
> i'm new to velocity and I was wondering if there was
> a substring function built into velocity. I think this
> would be useful as it is directly related to the presentation
> of strings on a template.

I saw jon responded, but I may be able to provide a little discussion in
addition to his correct but terse suggestion :)

The upshot of what Jon was saying is that in Velocity, Strings from the
context are regular java String classes.  Also, strings you create w/in
the template are also String objects.  All the methods apply.

So you can do things like

#set($foo = "Bar" )
$foo.substring(0,1)

and the output is

B

or other string functions work :

#set($bar = "12345")
#set($len = $bar.length() - 1)
#foreach($i in [0..$len] )
$bar.charAt($i)
#end

which would produce
1
2
3
4
5

geir

-- 
Geir Magnusson Jr.                               geirm@optonline.net
Developing for the web?  See http://jakarta.apache.org/velocity/

Re: Substring function

Posted by Jon Stevens <jo...@latchkey.com>.
on 3/19/01 5:28 PM, "Huy Do" <hu...@tramada.com> wrote:

> Hi,
> 
> i'm new to velocity and I was wondering if there was
> a substring function built into velocity. I think this
> would be useful as it is directly related to the presentation
> of strings on a template.
> 
> Thanks
> 
> Huy

context.put ("foo", new String());

template:

$foo.substring()

-jon