NVelocity String Functions - Split

296 views
Skip to first unread message

Alex

unread,
May 31, 2008, 5:01:43 PM5/31/08
to Castle Project Users
I am using the NVelocity view engine with the latest ASP.NET MVC
framework, preview 3. I cannot figure out how to split a comma
delimited string. Here is what I am trying and it is not working.

#set($myString = "one,two,three,four")

#foreach($str in $myString.Split(","))
$str
#end

I am not sure why this is not working. I can get substring and replace
to work just fine. What is wrong with split? What references should I
be using to see what string functions are available? I have tried the
VTL reference.

Thanks.

José Carvalho

unread,
May 31, 2008, 5:28:46 PM5/31/08
to Castle Project Users
Why not use:

#set( $numbers = ["one", "two", "three","four"] )

#foreach( $str in $numbers)
$str
#end

I've never used NVelocity with ASP.NET MVC, but I don't know if it is
possible call the Split method from your variable $myString. One
possible reason is that $myString is not created by NVelocity as a
normal C# string. Have you tried do the same with a variable passed
from controller?

Cheers,
José Carvalho
http://www.fe.up.pt/~ei03067/

Alex

unread,
Jun 1, 2008, 12:51:20 AM6/1/08
to Castle Project Users
José,

Thanks for the help. I do not have an array as you used in your
example. I have a string that is comma delimited.

#set( $numbers = ["one", "two", "three","four"] ) = array
#set( $numbers ="one,two,three,four") = my comma delimited string

Since I have a string delimited by commas, I need to use split to
create an array to iterate over. I am not sure why split does not
appear to work, nor am I sure what reference documentation I should be
using. For example, is this just VTL documentation, or is this Java
string documentation, or C# strings, ect. The Java docs have a string
util function called split, that I tried that. Then I tried the C#
version of split, and nothing worked there.

Thanks again for any ideas,

Alex

On May 31, 3:28 pm, José Carvalho <jose.barbosa.carva...@gmail.com>
wrote:
> Why not use:
>
> #set( $numbers = ["one", "two", "three","four"] )
>
> #foreach( $str in $numbers)
>         $str
> #end
>
> I've never used NVelocity with ASP.NET MVC, but I don't know if it is
> possible call the Split method from your variable $myString. One
> possible reason is that $myString is not created by NVelocity as a
> normal C# string. Have you tried do the same with a variable passed
> from controller?
>
> Cheers,
> José Carvalhohttp://www.fe.up.pt/~ei03067/
>
> On May 31, 10:01 pm, Alex <bamuel...@gmail.com> wrote:
>
>
>
> > I am using the NVelocity view engine with the latest ASP.NET MVC
> > framework, preview 3. I cannot figure out how to split a comma
> > delimited string. Here is what I am trying and it is not working.
>
> > #set($myString = "one,two,three,four")
>
> > #foreach($str in $myString.Split(","))
> >   $str
> > #end
>
> > I am not sure why this is not working. I can get substring and replace
> > to work just fine. What is wrong with split? What references should I
> > be using to see what string functions are available? I have tried the
> > VTL reference.
>
> > Thanks.- Hide quoted text -
>
> - Show quoted text -

Hamilton Verissimo

unread,
Jun 1, 2008, 1:40:06 PM6/1/08
to castle-pro...@googlegroups.com
From here http://msdn.microsoft.com/en-us/library/system.string.split.aspx
it looks like there's no overload for a string, only char and string
arrays. so it's not finding the overload, and that's why it's not
working

--
Cheers,
hamilton verissimo
ham...@castlestronghold.com
http://www.castlestronghold.com/

Alex

unread,
Jun 1, 2008, 11:21:45 PM6/1/08
to Castle Project Users
Thanks for the help, both of you. I have moved forward, without the
split, passing the array to my view as needed. Split would sure be
useful in that case.

On Jun 1, 11:40 am, "Hamilton Verissimo"
<hamm...@castlestronghold.com> wrote:
> From herehttp://msdn.microsoft.com/en-us/library/system.string.split.aspx
> hamm...@castlestronghold.comhttp://www.castlestronghold.com/- Hide quoted text -
Reply all
Reply to author
Forward
0 new messages