Grails converts a string to a letter case

Do I need to convert a string to letter case (correct case or title), is there any default function to support Groovy for this, or do I need to manually substitute the first character?

+4
source share
4 answers
assert org.apache.commons.lang.WordUtils.capitalizeFully('man OF stEEL') == 'Man Of Steel'

The class is WordUtilsprovided by Apache Commons Lang, which is available on the way to Grails application classes using defatult

+10
source

, ,

assert "John Doe" == "john dOE".tokenize(" ")*.toLowerCase()*.capitalize().join(" ")

,

+3

, ,

def capitalize(s) { s[0].toUpperCase() + s[1..-1].toLowerCase() }
caps = "man OF stEEL".replaceAll(/\w+/) { w -> capitalize(w) }
-2

Source: https://habr.com/ru/post/1541988/


All Articles