You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Vijay Verma <vi...@rediffmail.com> on 2002/07/22 10:32:02 UTC

String Tokenizer

Please Assist me :

I wish to tokenize a String using Velocity Code.
How Can I do this?

Re: String Tokenizer

Posted by Daniel Rogers <ii...@acm.org>.
On Mon, Jul 22, 2002 at 04:13:56PM +0530, Vijay Verma wrote:
> Hello Sir,
> 
> please review and please help...

Might I suggest a slightly different alternative - you could add the 
following small utility to the context and use the StringTokenizer 
directly:

import java.util.StringTokenizer;
public class StringTokenizerUtil {
    public StringTokenizer createTokenizer(String s, String token) {
        return new StringTokenizer(s, token);
    }
}

You could then do something like this in your Java code that sets up the 
context:

context.put("stu", new StringTokenizerUtil())

And then in your template, you could use the countTokens method on 
StringTokenizer to get what you want:

#set( $str = "A,B,C,D" )
count: $stu.createTokenizer($str, ",").countTokens()

The result would be 4. You can also foreach through it, since 
StringTokenizer implements Enumeration:

#foreach( $t in $stu.createTokenizer($str, ",") )
* $t
#end

and the result would be:
* A
* B
* C
* D

This is perhaps not the most desireable solution, but it should give you 
an idea of how Velocity works with objects in the context based on your 
current problem.

Daniel

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: String Tokenizer

Posted by Vijay Verma <vi...@rediffmail.com>.
Hello Sir,

please review and please help...

---------------------------------------
My bean is:
---------------------------------

import java.util.StringTokenizer;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.*;


public class RepairUtil
{

  private String value = null;

  public RepairUtil() { }

  public void setValue(String value)
  {
    this.value = value;
  }

  public int getLength()
  {
  StringTokenizer st = new StringTokenizer(this.value,",");
  int total=0;
  while (st.hasMoreTokens())
  {
   total++;
   st.nextToken();
        }
  return total;
  }

}

=====================================
and my template is:

#set($string = "A,B,C,D")

I wish to print the total characters which have tokened....ie... here is 4

what changes I will have to made in Bean or in Velocity code...

Please Guide!!!!!!!!

Thanks in advance
Vijay
=====/===========/==============/==============/==============/===========/





----- Original Message -----
From: "Konstantin Priblouda" <kp...@yahoo.com>
To: "Velocity Users List" <ve...@jakarta.apache.org>
Sent: Monday, July 22, 2002 3:23 PM
Subject: Re: String Tokenizer


>
> --- Vijay Verma <vi...@rediffmail.com> wrote:
> > Hello Sir,
> >
> > thanks for the reply....can u please tell me how can
> > i put a bean in the
> > context....
> > i am quiet new to velocity.
>
> RTFM. The same way you put any other bean in context.
> Refer to user dokumentation and examples.
>
> regards,
>
> =====
> Konstantin Priblouda ( ko5tik )    Freelance Software developer
> < http://www.pribluda.de > < play java games -> http://www.yook.de >
> < render charts online -> http://www.pribluda.de/povray/ >
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Health - Feel better, live better
> http://health.yahoo.com
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: String Tokenizer

Posted by Konstantin Priblouda <kp...@yahoo.com>.
--- Vijay Verma <vi...@rediffmail.com> wrote:
> Hello Sir,
> 
> thanks for the reply....can u please tell me how can
> i put a bean in the
> context....
> i am quiet new to velocity.

RTFM. The same way you put any other bean in context.
Refer to user dokumentation and examples. 

regards,

=====
Konstantin Priblouda ( ko5tik )    Freelance Software developer
< http://www.pribluda.de > < play java games -> http://www.yook.de >
< render charts online -> http://www.pribluda.de/povray/ >

__________________________________________________
Do You Yahoo!?
Yahoo! Health - Feel better, live better
http://health.yahoo.com

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: String Tokenizer

Posted by Vijay Verma <vi...@rediffmail.com>.
Hello Sir,

thanks for the reply....can u please tell me how can i put a bean in the
context....
i am quiet new to velocity.

thanks in advance
VIjay


----- Original Message -----
From: "Konstantin Priblouda" <kp...@yahoo.com>
To: "Velocity Users List" <ve...@jakarta.apache.org>
Sent: Monday, July 22, 2002 2:10 PM
Subject: Re: String Tokenizer


>
> --- Vijay Verma <vi...@rediffmail.com> wrote:
> > Please Assist me :
> >
> > I wish to tokenize a String using Velocity Code.
> > How Can I do this?
> >
> Put a bean in context, which does it for you.
>
> regards,
>
> =====
> Konstantin Priblouda ( ko5tik )    Freelance Software developer
> < http://www.pribluda.de > < play java games -> http://www.yook.de >
> < render charts online -> http://www.pribluda.de/povray/ >
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Health - Feel better, live better
> http://health.yahoo.com
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: String Tokenizer

Posted by Konstantin Priblouda <kp...@yahoo.com>.
--- Vijay Verma <vi...@rediffmail.com> wrote:
> Please Assist me :
> 
> I wish to tokenize a String using Velocity Code.
> How Can I do this?
> 
Put a bean in context, which does it for you. 

regards,

=====
Konstantin Priblouda ( ko5tik )    Freelance Software developer
< http://www.pribluda.de > < play java games -> http://www.yook.de >
< render charts online -> http://www.pribluda.de/povray/ >

__________________________________________________
Do You Yahoo!?
Yahoo! Health - Feel better, live better
http://health.yahoo.com

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>