Groovy GDK 相当于 Apache Commons StringUtils.capitalize(str) 或 Perl 的 ucfirst(str)

发布于 2024-07-15 22:57:43 字数 532 浏览 4 评论 0原文

是/否问题:是否有 Groovy GDK 函数可以将字符串的第一个字符大写?

我正在寻找 Perl 的 ucfirst(..) 或 Apache Commons StringUtils.capitalize( str)(后者将输入字符串中所有单词的第一个字母大写)。

我目前正在使用 .. .. 手动编码,

str = str[0].toUpperCase() + str[1 .. str.size() - 1]

这可行,但我认为有一种更 Groovy 的方法可以做到这一点。 我想象 ucfirst(..) 是比 center(..) 更常见的操作,center(..) 是 Groovy GDK 中的标准方法(参见 http://groovy.codehaus.org/groovy-jdk/java/lang/String.html)。

Yes/no-question: Is there a Groovy GDK function to capitalize the first character of a string?

I'm looking for a Groovy equivalent of Perl's ucfirst(..) or Apache Commons StringUtils.capitalize(str) (the latter capitalizes the first letter of all words in the input string).

I'm currently coding this by hand using ..

str = str[0].toUpperCase() + str[1 .. str.size() - 1]

.. which works, but I assume there is a more Groovy way to do it. I'd imagine ucfirst(..) being a more common operation than say center(..) which is a standard method in the Groovy GDK (see http://groovy.codehaus.org/groovy-jdk/java/lang/String.html).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

兔姬 2024-07-22 22:57:44

不,没有任何东西直接内置于该语言中。

有一些更绝妙的方法可以完成您所要求的任务(如果您不想像 Vladimir 建议的那样以 Java 惯用方式使用 StringUtils)。

您可以在范围的后半部分使用负值来简化您的方法:

def str = "foo"

assert "Foo" == str[0].toUpperCase() + str[1..-1]

或者您可以使用 import static 使其看起来像本机方法:

import static org.apache.commons.lang.StringUtils.*

assert "Foo" == capitalize("foo")

您还可以修改元类以使其包含所有 StringUtils 方法,因此它看起来像一个 GDK 方法:

import org.apache.commons.lang.StringUtils

String.metaClass.mixin StringUtils

assert "Foo" == "foo".capitalize()

No, nothing built directly into the language.

There are a couple of more groovy ways to do what you're asking though (if you don't want to use StringUtils in the Java idiomatic way as Vladimir suggests).

You can simplify your method using a negative value in the second half of your range:

def str = "foo"

assert "Foo" == str[0].toUpperCase() + str[1..-1]

Or you can use an import static to make it look like a native method:

import static org.apache.commons.lang.StringUtils.*

assert "Foo" == capitalize("foo")

You can also modify the metaClass to have all of StringUtils methods right on it, so it looks like a GDK method:

import org.apache.commons.lang.StringUtils

String.metaClass.mixin StringUtils

assert "Foo" == "foo".capitalize()
断念 2024-07-22 22:57:44

我不知道有任何这样的方法,但解决方法是在您的 Groovy 代码中直接使用 Apache Commons 库:

import org.apache.commons.lang.StringUtils

def str = StringUtils.capitalize(input)

它使您的 Groovy 代码有点 Java 风格(有些人可能不喜欢它),但它可以完成工作。

在我看来,Groovy 的巨大优势在于您可以非常轻松地利用通常与更传统的 Java 代码库一起使用的所有 Java 库。

I'm not aware of any such method, but a workaround is to directly use the Apache Commons library in your Groovy code:

import org.apache.commons.lang.StringUtils

def str = StringUtils.capitalize(input)

It makes your Groovy code a bit Java-ish (some may not like it) but it does the job.

IMO the great advantage of Groovy is that you can very easily leverage all the Java libraries you normally use with a more traditional Java code base.

会发光的星星闪亮亮i 2024-07-22 22:57:44

要使其在您的应用程序中全局可用,只需在启动时初始化此块

String.metaClass.capitalize = {
委托[0].toUpperCase()+委托[1..-1]
}

To make it available globally in your app ,just initialise this block at start up

String.metaClass.capitalize = {
delegate[0].toUpperCase()+delegate[1..-1]
}

上课铃就是安魂曲 2024-07-22 22:57:44

如果您想更进一步并将每个单词大写,您可以使用如下内容:

def words = sentence.split(" ")
def newStr = []
words.each { w ->
    def capW = [w[0].toUpperCase()]
    if (w.length() > 1) {
        capW << w[1..-1].toLowerCase()
    }
    newStr << capW.join()
}
return newStr.join(' ')

If you wanted to take it a step further and capitalize each word, you can use something like this:

def words = sentence.split(" ")
def newStr = []
words.each { w ->
    def capW = [w[0].toUpperCase()]
    if (w.length() > 1) {
        capW << w[1..-1].toLowerCase()
    }
    newStr << capW.join()
}
return newStr.join(' ')
方觉久 2024-07-22 22:57:44

那么你可以尝试这个:

“嘿这是一个字符串”.split(' ').collect{it.capitalize()}.join(' ')

或者可能是这样的:

char c = ' '
“嘿,这是一个字符串”.collect{
c = c==' '?it.capitalize():it
}.join()

well you can try this:

"hey this is a string".split(' ').collect{it.capitalize()}.join(' ')

or may be this:

char c = ' '
"hey this is a string".collect{
c = c==' '?it.capitalize():it
}.join()

关于从前 2024-07-22 22:57:44

从 Groovy 1.8.2(早在 2011 年 9 月发布)开始,capitalize() 是对 String 实现的 CharSequence 的内置增强。

def str = "hello world"
str = str.capitalize()
assert "Hello world" == str

As of Groovy 1.8.2 (released way back in September 2011), capitalize() is a built-in enhancement to CharSequence which String implements.

def str = "hello world"
str = str.capitalize()
assert "Hello world" == str
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文