You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Philip Mark Donaghy <ph...@yahoo.com> on 2004/11/27 23:23:35 UTC

Replace characters in a string

Hi,

I can't seem to get String.replace('-','_') or
String.replace("-","_") to work in my template

<table
name="$element.name.localPart.toUpperCase().replace("\-","_")">

In this template localPart is a Java String.

With gratitude,
Philip Donaghy


		
__________________________________ 
Do you Yahoo!? 
Take Yahoo! Mail with you! Get it on your mobile phone. 
http://mobile.yahoo.com/maildemo 

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


Re: Replace characters in a string

Posted by Philip Mark Donaghy <ph...@yahoo.com>.
Thanks Shinobu,

> You can use String in 1.5.0.
>   
>
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#replace(java.lang.CharSequence,
> java.lang.CharSequence)
> 
> ## So that's why it worked for me.  :)

It is true this only works in tiger java 1.5.0.

Philip Donaghy


		
__________________________________ 
Do you Yahoo!? 
Meet the all-new My Yahoo! - Try it today! 
http://my.yahoo.com 
 


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


Re: Replace characters in a string

Posted by Shinobu Kawai <sh...@gmail.com>.
Hi Will,

> According to the JavaDocs for java.lang.String, the params for replace need
> to be chars.
> 
> Try replaceAll or replaceFirst, assuming you are using JDK 1.4+.

You can use String in 1.5.0.
   http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#replace(java.lang.CharSequence,
java.lang.CharSequence)

## So that's why it worked for me.  :)

Best regards,
-- Shinobu Kawai

-- 
Shinobu Kawai <sh...@gmail.com>

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


Re: Replace characters in a string

Posted by Will Glass-Husain <wg...@forio.com>.
The problem is that Literal strings in velocity are Strings, not chars.

According to the JavaDocs for java.lang.String, the params for replace need 
to be chars.

Try replaceAll or replaceFirst, assuming you are using JDK 1.4+.

WILL


----- Original Message ----- 
From: "Philip Mark Donaghy" <ph...@yahoo.com>
To: <ve...@jakarta.apache.org>
Sent: Saturday, November 27, 2004 2:23 PM
Subject: Replace characters in a string


> Hi,
>
> I can't seem to get String.replace('-','_') or
> String.replace("-","_") to work in my template
>
> <table
> name="$element.name.localPart.toUpperCase().replace("\-","_")">
>
> In this template localPart is a Java String.
>
> With gratitude,
> Philip Donaghy
>
>
>
> __________________________________
> Do you Yahoo!?
> Take Yahoo! Mail with you! Get it on your mobile phone.
> http://mobile.yahoo.com/maildemo
>
> ---------------------------------------------------------------------
> 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: Replace characters in a string

Posted by Shinobu Kawai <sh...@gmail.com>.
Hi Philip,

> I can't seem to get String.replace('-','_') or
> String.replace("-","_") to work in my template
> 
> <table
> name="$element.name.localPart.toUpperCase().replace("\-","_")">
> 
> In this template localPart is a Java String.

Try removing the backslash.  Velocity invokes methods as is was told to.  ;)

Best regards,
-- Shinobu Kawai

-- 
Shinobu Kawai <sh...@gmail.com>

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