You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by trad-ex <tr...@inter7.jp> on 2006/05/19 07:28:43 UTC

How can I trimming $string ?

Hi all,

I have a question how to trim string smarter way.
My code is:

#macro( returnString $arg )
  ...do something
  $returnVal
#end

....

  #set( $somVal = "#returnString( $arg )" )
  
When I checked the contents of $someVal, it contained two leading space-
charachters and trailing \r\n.
So I should use trim() before I use $someVal value.
For instances,

 #if( $someVal.trim() == "SomeThing" )
 
My question is:
1. Is there smarter way to trim the white-spaces ?
2. What's wrong with my codes ?

Best Regards,
trad-ex


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


Re: How can I trimming $string ?

Posted by trad-ex <tr...@inter7.jp>.
Hi Chris,

Thanks a lot for your quick responce and solution!
It does work very well!

I will try to read the discussion in the wiki about White Space Issue.

Thanks again!

Best regards,
trad-ex

>Hi,
>
>this is a typical whitespace issue. Please note that
>Velocity mostly leaves the whitespaces you coded in
>your template. Many times it would be desirable to
>be able to add indented markup without introducing
>spacing and formatting artifacts. This issue is
>under discussion in the wiki page:
>   http://wiki.apache.org/jakarta-velocity/VelocityWhitespaceGobbling
>
>In your specific case the proposed solutions will not
>have any impact on your code. You have clearly included
>two spaces and an EOL symbol. If you do not want these,
>you have to encapsule these formatting artifacts within
>comments:
>#macro( returnString $arg )#*
>   *#...do something#*
>   *#$returnVal##
>#end
>
>With the http://wiki.apache.org/jakarta-velocity/VelocityWhitespaceGobbleSt
>ructuredTemplates
>approach it would only need a markup addition to indicate
>that surrounding whitespaces are not desired (this is *not*
>implemented yet):
>#macro( returnString $arg )
>   ...do something
>   #text( $returnVal )
>#end
>
>Hope this clarifies your issue,
>Christoph
>
>trad-ex wrote:
>> Hi all,
>> 
>> I have a question how to trim string smarter way.
>> My code is:
>> 
>> #macro( returnString $arg )
>>   ...do something
>>   $returnVal
>> #end
>> 
>> ....
>> 
>>   #set( $somVal = "#returnString( $arg )" )
>>   
>> When I checked the contents of $someVal, it contained two leading space-
>> charachters and trailing \r\n.
>> So I should use trim() before I use $someVal value.
>> For instances,
>> 
>>  #if( $someVal.trim() == "SomeThing" )
>>  
>> My question is:
>> 1. Is there smarter way to trim the white-spaces ?
>> 2. What's wrong with my codes ?
>> 
>> Best Regards,
>> trad-ex
>> 
>> 
>> ---------------------------------------------------------------------
>> 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: How can I trimming $string ?

Posted by ap...@recks.org.
Hi,

this is a typical whitespace issue. Please note that
Velocity mostly leaves the whitespaces you coded in
your template. Many times it would be desirable to
be able to add indented markup without introducing
spacing and formatting artifacts. This issue is
under discussion in the wiki page:
   http://wiki.apache.org/jakarta-velocity/VelocityWhitespaceGobbling

In your specific case the proposed solutions will not
have any impact on your code. You have clearly included
two spaces and an EOL symbol. If you do not want these,
you have to encapsule these formatting artifacts within
comments:
#macro( returnString $arg )#*
   *#...do something#*
   *#$returnVal##
#end

With the http://wiki.apache.org/jakarta-velocity/VelocityWhitespaceGobbleStructuredTemplates
approach it would only need a markup addition to indicate
that surrounding whitespaces are not desired (this is *not*
implemented yet):
#macro( returnString $arg )
   ...do something
   #text( $returnVal )
#end

Hope this clarifies your issue,
Christoph

trad-ex wrote:
> Hi all,
> 
> I have a question how to trim string smarter way.
> My code is:
> 
> #macro( returnString $arg )
>   ...do something
>   $returnVal
> #end
> 
> ....
> 
>   #set( $somVal = "#returnString( $arg )" )
>   
> When I checked the contents of $someVal, it contained two leading space-
> charachters and trailing \r\n.
> So I should use trim() before I use $someVal value.
> For instances,
> 
>  #if( $someVal.trim() == "SomeThing" )
>  
> My question is:
> 1. Is there smarter way to trim the white-spaces ?
> 2. What's wrong with my codes ?
> 
> Best Regards,
> trad-ex
> 
> 
> ---------------------------------------------------------------------
> 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