Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Grails使行中的每个单词都大写_Grails - Fatal编程技术网

Grails使行中的每个单词都大写

Grails使行中的每个单词都大写,grails,Grails,我想把每个单词的第一个字符作为大写,剩下的字符作为小写。以下代码打印的内容与原始字符串相同。怎样才能让它工作呢 def name = "hello world grails" println name.split(" +").each{ it[0]?.toUpperCase()+it[1..-1]?.toLowerCase() } 这将完成您的工作: ​def name = "hello world grails" def n

我想把每个单词的第一个字符作为大写,剩下的字符作为小写。以下代码打印的内容与原始字符串相同。怎样才能让它工作呢

def name = "hello world grails"

        println  name.split(" +").each{
             it[0]?.toUpperCase()+it[1..-1]?.toLowerCase()
        }

这将完成您的工作:

 ​def name = "hello world grails"

 def newName = ""
 name.split(" ").each { word ->
    newName += word[0].toUpperCase() + word[1..(word.size()-1)].toLowerCase()+" "
 }

 println newName​

这将完成您的工作:

 ​def name = "hello world grails"

 def newName = ""
 name.split(" ").each { word ->
    newName += word[0].toUpperCase() + word[1..(word.size()-1)].toLowerCase()+" "
 }

 println newName​

您可以使用在1.7.3版本中添加到Groovy中的
capitalize()
方法:

def name = "hello world grails"
def splitted = name.split("\\s+").collect { it.toLowerCase().capitalize() }
println splitted
如果您想要一个字符串:

println splitted.inject('') { accumulator, current -> accumulator + current + ' ' }.trim()
您的代码也有问题。使用
。每个{…}
都不会“转换”结果列表中的元素,例如

def list = ["Asdf", "XCVB"]
def ret = list.each { return it.toLowerCase() }
println ret == list // true
ret = list.collect { return it.toLowerCase() }
println ret == list // false

您可以使用在1.7.3版本中添加到Groovy中的
capitalize()
方法:

def name = "hello world grails"
def splitted = name.split("\\s+").collect { it.toLowerCase().capitalize() }
println splitted
如果您想要一个字符串:

println splitted.inject('') { accumulator, current -> accumulator + current + ' ' }.trim()
您的代码也有问题。使用
。每个{…}
都不会“转换”结果列表中的元素,例如

def list = ["Asdf", "XCVB"]
def ret = list.each { return it.toLowerCase() }
println ret == list // true
ret = list.collect { return it.toLowerCase() }
println ret == list // false

使用以下代码:

def requiredString = org.apache.commons.lang.WordUtils.capitalizeFully('i AM gRooT') // yields 'I Am Groot'

您需要包含Apache Commons Lang作为依赖项。

使用以下代码:

def requiredString = org.apache.commons.lang.WordUtils.capitalizeFully('i AM gRooT') // yields 'I Am Groot'

您需要包含Apache Commons Lang作为依赖项。

您的代码可以工作,但我喜欢@Xeon代码。谢谢你的帮助。你的代码很有效,但我喜欢@Xeon代码。谢谢你的帮助。嗨,当我们print@sfgroups因为被拆分的
变量的类型为list。看看我编辑过的答案。嗨,当我们print@sfgroups因为被拆分的
变量的类型为list。看看我编辑过的答案。