简体   繁体   English

Groovy文字StringBuilder / StringBuffer

[英]Groovy literal StringBuilder/StringBuffer

Groovy supports a literal syntax for creating a StringBuilder/StringBuffer instead of the usual Groovy支持用于创建StringBuilder / StringBuffer的文字语法,而不是通常的

def sb = new StringBuilder()

However, I can't seem to remember (or find on Google) the correct syntax. 但是,我似乎无法记住(或在Google上找到)正确的语法。

To get a StringBuffer in a single step, you could use 要在一个步骤中获得StringBuffer,您可以使用

def sb = 'Hello'<<''

or even: 甚至:

def sb = ''<<'' //4 single quotes, not double quotes

for an initially empty one. 对于最初空的。

I think (but I could be wrong) the reason for using a StringBuffer rather than a StringBuilder is to maintain compatibility with Java 1.4. 我认为(但我可能错了)使用StringBuffer而不是StringBuilder的原因是为了保持与Java 1.4的兼容性。

To create a StringBuffer: 要创建StringBuffer:

text = 'Hello '

To append: 附加:

text <<= 'World!'

And this might help some more. 这可能会有所帮助。

I've just played with StringBuilder / StringBuffer in Groovy. 我刚刚在Groovy中使用了StringBuilder / StringBuffer。

Some examples below: 以下是一些例子:

// << operator example
def year = StringBuilder.newInstance()
year << 2
year << "0"
year << '1' << 4
assert year.toString() == "2014"

// You can use with operator
def month = StringBuilder.newInstance()
month.with {
    append "0"
    append '5'
}
assert month.toString() == "05"

// or just append String like in Java or you can leave parenthesis
def day = StringBuilder.newInstance()
day.append "1"
day.append('1')
assert day.toString() == '11'

// It's nice to know, that we can use StringBuilder directly in GString
def date = "$year-${month}-$day"
assert date == "2014-05-11"

Note: Use StringBuilder when it is used only by one thread. 注意:仅在一个线程使用时使用StringBuilder。 StringBuilder provides an API compatible with StringBuffer. StringBuilder提供与StringBuffer兼容的API。 StringBuffer is synchronized, StringBuilder is not. StringBuffer是同步的,StringBuilder不是。 Check this link for more info. 请查看此链接以获取更多信息。

String.leftShift() creates a StringBuffer . String.leftShift() 创建一个StringBuffer

AFAICS, you cannot create a String Builder via groovy syntax sugar. AFAICS,你不能通过groovy语法糖创建一个String Builder You can only create a StringBuilder explicitly. 您只能显式创建StringBuilder。 No operator overload on String or GString creates a StringBuilder that you can explicitly use. StringGString上没有运算符重载会创建一个可以显式使用的StringBuilder。 Java String concatenation might create a StringBuilder , but that instance wouldn't be usable in your code. Java String连接可能会创建一个StringBuilder ,但该实例在您的代码中不可用。

At the time of writing this answer: 在撰写本答案时:

  • Another answer said to use <<= . 另一个答案是用<<= <<= is merely a java compound assignment that uses << . <<=仅仅是一个使用<<java复合赋值 In particular, there is no method for overloading <<= . 特别是,没有重载 <<=方法。
  • The accepted answer said to use ''<<'' //4 single quotes, not double quotes . 接受的答案是说使用''<<'' //4 single quotes, not double quotes Double quotes work fine, at least on modern groovy. 双引号工作正常,至少在现代时尚。

The following code shows that we always get a StringBuffer, not a StringBuilder, for the result of various combinations of << vs <<= and "" vs '' : 以下代码显示我们总是得到一个StringBuffer,而不是StringBuilder,用于<< vs <<="" vs ''的各种组合的结果:

def ls = ''<<''
println ls.class
def lse = ''
lse <<=''
println lse.class

ls = ""<<''
println ls.class
lse = ""
lse <<=''
println lse.class

ls = ''<<""
println ls.class
lse = ''
lse <<=""
println lse.class

ls = ""<<""
println ls.class
lse = ""
lse <<=""
println lse.class

prints: 打印:

class java.lang.StringBuffer
class java.lang.StringBuffer
class java.lang.StringBuffer
class java.lang.StringBuffer
class java.lang.StringBuffer
class java.lang.StringBuffer
class java.lang.StringBuffer
class java.lang.StringBuffer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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