Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lowercase first letter in Apache Velocity

I've got this code, which converts "dotted" string to camelCase in WebStorm File Template:

#set($Controller_name = ${StringUtils.removeAndHump(${NAME}, ".")})

For example it converts foo.bar.test to FooBarTest.

But what I need is to convert it from foo.bar.test to fooBarTest.

How can I do that?

like image 777
zorza Avatar asked Nov 09 '15 14:11

zorza


People also ask

How do I convert a string to lowercase except first letter?

The toLowerCase method converts a string to lowercase letters. The toLowerCase() method doesn't take in any parameters. Strings in JavaScript are immutable. The toLowerCase() method converts the string specified into a new one that consists of only lowercase letters and returns that value.

What is Apache Velocity template?

The Velocity Template Language (VTL) is meant to provide the easiest, simplest, and cleanest way to incorporate dynamic content in a web page. Even a web page developer with little or no programming experience should soon be capable of using VTL to incorporate dynamic content in a web site.

How do you declare a variable in Velocity?

To declare a local variable inside a template, type: $ followed by a string beginning with a letter. A referenced Java object variable is provided by Report Wizard as shown in the following table (see the Linking Report Wizard template to MagicDraw section for a complete list of variables).

How do I test a Velocity template?

Approach 1: The obvious approach: Run and check. Run Velocity against the template to be checked. Look at the output and see if it is what you desired. This must be the most primitive testing approach, but most people nowadays are too lazy and want this done by the computer.


1 Answers

This is what finally worked for me:

#set($Controller_name = ${StringUtils.removeAndHump(${NAME}, ".")})
#set($first_letter = $Controller_name.substring(0,1).toLowerCase())
#set($the_rest = $Controller_name.substring(1))
#set($Controller_name = ${first_letter} + ${the_rest})

It could be shortened to:

#set($Controller_name = ${StringUtils.removeAndHump(${NAME}, ".")})
#set($Controller_name = $Controller_name.substring(0,1).toLowerCase() + $Controller_name.substring(1))

Thanks @LazyOne for pointing me in the right direction.

like image 103
zorza Avatar answered Sep 24 '22 17:09

zorza