简体   繁体   中英

How to convert a string to camel case in phpstorms (velocity based) file tempates?

What I've tried so far is:

## $NAME is something like 'my_controller_c'
#set($NAME = $NAME.removeAndHump($NAME))
#set($NAME = $NAME.underscoresToCamelCase(String)

But that does not work. The first one does nothing, the second one throws an java error.

I also tried using regular expressions and to loop through the string, but my java knowledge is very basic.

The following works in PhpStorm 9 (and probably all of the other JetBrains IDEs, I would guess):

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

class $new_name {
}

This is, what I ended up doing:

#set($ctrlName = $NAME.replaceAll("-c$", ""))
#set($ctrlNewName = "")
#foreach($str in $ctrlName.split("-"))
  #set($str = $str.substring(0,1).toUpperCase()+$str.substring(1))
  #set($ctrlNewName = $ctrlNewName + $str)
#end
#set ( $ctrlNewName = $ctrlNewName + "Ctrl" )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM