Jenis Rentetan di Groovy

1. Gambaran keseluruhan

Dalam tutorial ini, kita akan melihat dengan lebih dekat beberapa jenis rentetan di Groovy, termasuk rentetan tunggal, ganda, petik tiga, dan rentetan slashy.

Kami juga akan meneroka sokongan rentetan Groovy untuk watak khas, berbilang baris, regex, melarikan diri, dan interpolasi berubah-ubah.

2. Meningkatkan java.lang.String

Mungkin baik untuk dimulakan dengan menyatakan bahawa sejak Groovy berbasis di Java, ia memiliki semua kemampuan String Java seperti penggabungan, API String, dan manfaat yang melekat dari kumpulan tetap String kerana itu.

Mari lihat dahulu bagaimana Groovy memperluas beberapa asas ini.

2.1. Gabungan Rentetan

Gabungan rentetan hanyalah gabungan dua rentetan:

def first = 'first' def second = "second" def concatenation = first + second assertEquals('firstsecond', concatenation)

Di mana Groovy membina ini adalah dengan beberapa jenis rentetan lain, yang akan kita perhatikan sebentar lagi. Perhatikan bahawa kita dapat menggabungkan setiap jenis secara bergantian.

2.2. Interpolasi Rentetan

Sekarang, Java menawarkan beberapa templat yang sangat asas melalui printf , tetapi Groovy semakin mendalam, menawarkan interpolasi rentetan, proses templat rentetan dengan pemboleh ubah :

def name = "Kacper" def result = "Hello ${name}!" assertEquals("Hello Kacper!", result.toString())

Walaupun Groovy menyokong penggabungan untuk semua jenis rentetan, ia hanya memberikan interpolasi untuk jenis tertentu.

2.3. GString

Tetapi tersembunyi dalam contoh ini adalah sedikit keriput - mengapa kita memanggil toString () ?

Sebenarnya, hasilnya bukan jenis String , walaupun kelihatan seperti itu.

Oleh kerana kelas String adalah muktamad , kelas rentetan Groovy yang menyokong interpolasi, GString , tidak mengklasifikasikannya . Dengan kata lain, untuk Groovy memberikan peningkatan ini, ia mempunyai kelas rentetan sendiri, GString , yang tidak dapat dilanjutkan dari String.

Ringkasnya, jika kita melakukannya:

assertEquals("Hello Kacper!", result)

ini memanggil assertEquals (Objek, Objek), dan kami mendapat:

java.lang.AssertionError: expected: java.lang.String but was: org.codehaus.groovy.runtime.GStringImpl Expected :java.lang.String Actual :org.codehaus.groovy.runtime.GStringImpl

3. Rentetan Petikan Tunggal

Mungkin rentetan termudah di Groovy adalah satu dengan petikan tunggal:

def example = 'Hello world'

Di bawah tudung, ini adalah Java Strings lama yang biasa , dan sangat berguna apabila kita memerlukan tanda kutip di dalam string kita.

Daripada:

def hardToRead = "Kacper loves \"Lord of the Rings\""

Kami dapat menggabungkan satu rentetan dengan rentetan yang lain dengan mudah:

def easyToRead = 'Kacper loves "Lord of the Rings"'

Kerana kita dapat menukar jenis petikan seperti ini, ini mengurangkan keperluan untuk melepaskan sebut harga.

4. Rentetan Petikan Tunggal Tiga

Rentetan petikan tunggal tiga berguna dalam konteks menentukan kandungan berbilang baris.

Sebagai contoh, katakan kita mempunyai beberapa JSON untuk diwakili sebagai rentetan:

{ "name": "John", "age": 20, "birthDate": null }

Kami tidak perlu menggunakan gabungan dan watak barisan baru yang eksplisit untuk mewakili ini.

Sebagai gantinya, mari kita gunakan rentetan tiga kutipan tunggal:

def jsonContent = ''' { "name": "John", "age": 20, "birthDate": null } '''

Groovy menyimpan ini sebagai Java String yang sederhana dan menambah gabungan dan baris baru yang diperlukan untuk kami.

Namun, masih ada satu cabaran yang harus diatasi.

Biasanya untuk kebolehbacaan kod, kami memasukkan kod kami:

def triple = ''' firstline secondline '''

Tetapi rentetan petikan tunggal tiga mengekalkan ruang kosong . Ini bermaksud bahawa tali di atas benar-benar:

(newline) firstline(newline) secondline(newline)

tidak:

1 2 firstline(newline)secondline(newline)

seperti mungkin yang kita maksudkan.

Stay tuned to see how we get rid of them.

4.1. Newline Character

Let's confirm that our previous string starts with a newline character:

assertTrue(triple.startsWith("\n"))

It's possible to strip that character. To prevent this, we need to put a single backslash \ as a first and last character:

def triple = '''\ firstline secondline '''

Now, we at least have:

1 2 firstline(newline)secondline(newline)

One problem down, one more to go.

4.2. Strip the Code Indentation

Next, let's take care of the indentation. We want to keep our formatting, but remove unnecessary whitespace characters.

The Groovy String API comes to the rescue!

To remove leading spaces on every line of our string, we can use one of the Groovy default methods, String#stripIndent():

def triple = '''\ firstline secondline'''.stripIndent() assertEquals("firstline\nsecondline", triple)

Please note, that by moving the ticks up a line, we've also removed a trailing newline character.

4.3. Relative Indentation

We should remember that stripIndent is not called stripWhitespace.

stripIndent determines the amount of indentation from the shortened, non-whitespace line in the string.

So, let's change the indentation quite a bit for our triple variable:

class TripleSingleQuotedString { @Test void 'triple single quoted with multiline string with last line with only whitespaces'() { def triple = '''\ firstline secondline\ '''.stripIndent() // ... use triple } }

Printing triple would show us:

firstline secondline

Since firstline is the least-indented non-whitespace line, it becomes zero-indented with secondline still indented relative to it.

Note also that this time, we are removing the trailing whitespace with a slash, like we saw earlier.

4.4. Strip with stripMargin()

For even more control, we can tell Groovy right where to start the line by using a | and stripMargin:

def triple = '''\ |firstline |secondline'''.stripMargin()

Which would display:

firstline secondline

The pipe states where that line of the string really starts.

Also, we can pass a Character or CharSequence as an argument to stripMargin with our custom delimiter character.

Great, we got rid of all unnecessary whitespace, and our string contains only what we want!

4.5. Escaping Special Characters

With all the upsides of the triple single-quote string, there is a natural consequence of needing to escape single quotes and backslashes that are part of our string.

To represent special characters, we also need to escape them with a backslash. The most common special characters are a newline (\n) and tabulation (\t).

For example:

def specialCharacters = '''hello \'John\'. This is backslash - \\ \nSecond line starts here'''

will result in:

hello 'John'. This is backslash - \ Second line starts here

There are a few we need to remember, namely:

  • \t – tabulation
  • \n – newline
  • \b – backspace
  • \r – carriage return
  • \\ – backslash
  • \f – formfeed
  • \' – single quote

5. Double-Quoted String

While double-quoted strings are also just Java Strings, their special power is interpolation. When a double-quoted string contains interpolation characters, Groovy switches out the Java String for a GString.

5.1.GString and Lazy Evaluation

We can interpolate a double-quoted string by surrounding expressions with ${} or with $ for dotted expressions.

Its evaluation is lazy, though – it won't be converted to a String until it is passed to a method that requires a String:

def string = "example" def stringWithExpression = "example${2}" assertTrue(string instanceof String) assertTrue(stringWithExpression instanceof GString) assertTrue(stringWithExpression.toString() instanceof String)

5.2. Placeholder with Reference to a Variable

The first thing we probably want to do with interpolation is send it a variable reference:

def name = "John" def helloName = "Hello $name!" assertEquals("Hello John!", helloName.toString())

5.2. Placeholder with an Expression

But, we can also give it expressions:

def result = "result is ${2 * 2}" assertEquals("result is 4", result.toString())

We can put even statements into placeholders, but it's considered as bad practice.

5.3. Placeholders with the Dot Operator

We can even walk object hierarchies in our strings:

def person = [name: 'John'] def myNameIs = "I'm $person.name, and you?" assertEquals("I'm John, and you?", myNameIs.toString())

With getters, Groovy can usually infer the property name.

But if we call a method directly, we'll need to use ${}because of the parentheses:

def name = 'John' def result = "Uppercase name: ${name.toUpperCase()}".toString() assertEquals("Uppercase name: JOHN", result)

5.4. hashCode in GString and String

Interpolated strings are certainly godsends in comparison to plain java.util.String, but they differ in an important way.

See, Java Strings are immutable, and so calling hashCode on a given string always returns the same value.

But, GString hashcodes can vary since the String representation depends on the interpolated values.

And actually, even for the same resulting string, they won't have the same hash codes:

def string = "2+2 is 4" def gstring = "2+2 is ${4}" assertTrue(string.hashCode() != gstring.hashCode())

Thus, we should never use GString as a key in a Map!

6. Triple Double-Quote String

So, we've seen triple single-quote strings, and we've seen double-quoted strings.

Let's combine the power of both to get the best of both worlds – multi-line string interpolation:

def name = "John" def multiLine = """ I'm $name. "This is quotation from 'War and Peace'" """

Also, notice that we didn't have to escape single or double-quotes!

7. Slashy String

Now, let's say that we are doing something with a regular expression, and we are thus escaping backslashes all over the place:

def pattern = "\\d{1,3}\\s\\w+\\s\\w+\\\\\\w+"

It's clearly a mess.

To help with this, Groovy supports regex natively via slashy strings:

def pattern = /\d{3}\s\w+\s\w+\\\w+/ assertTrue("3 Blind Mice\Men".matches(pattern))

Slashy strings may be both interpolated and multi-line:

def name = 'John' def example = / Dear ([A-Z]+), Love, $name /

Of course, we have to escape forward slashes:

def pattern = /.*foobar.*\/hello.*/ 

And we can't represent an empty string with Slashy Stringsince the compiler understands // as a comment:

// if ('' == //) { // println("I can't compile") // }

8. Dollar-Slashy String

Slashy strings are great, though it's a bummer to have to escape the forward slash. To avoid additional escaping of a forward slash, we can use a dollar-slashy string.

Let's assume that we have a regex pattern: [0-3]+/[0-3]+. It's a good candidate for dollar-slashy string because in a slashy string, we would have to write: [0-3]+//[0-3]+.

Dollar-slashy strings are multiline GStrings that open with $/ and close with /$. To escape a dollar or forward slash, we can precede it with the dollar sign ($), but it's not necessary.

We don't need to escape $ in GString placeholder.

For example:

def name = "John" def dollarSlashy = $/ Hello $name!, I can show you a $ sign or an escaped dollar sign: $$ Both slashes work: \ or /, but we can still escape it: $/ We have to escape opening and closing delimiters: - $$$/ - $/$$ /$ 

would output:

Hello John!, I can show you a $ sign or an escaped dollar sign: $ Both slashes work: \ or /, but we can still escape it: / We have to escape opening and closing delimiter: - $/ - /$

9. Character

Those familiar with Java have already wondered what Groovy did with characters since it uses single quotes for strings.

Actually, Groovy doesn't have an explicit character literal.

There are three ways to make a Groovy string an actual character:

  • explicit use of ‘char' keyword when declaring a variable
  • using ‘as' operator
  • by casting to ‘char'

Let's take a look at them all:

char a = 'A' char b = 'B' as char char c = (char) 'C' assertTrue(a instanceof Character) assertTrue(b instanceof Character) assertTrue(c instanceof Character)

The first way is very convenient when we want to keep the character as a variable. The other two methods are more interesting when we want to pass a character as an argument to a function.

10. Summary

Jelas, itu banyak, jadi mari kita meringkaskan beberapa perkara penting dengan cepat:

  • rentetan yang dibuat dengan satu petikan (') tidak menyokong interpolasi
  • rentetan petikan berganda langsing dan tiga kali ganda boleh terdiri daripada pelbagai baris
  • rentetan berbilang baris mengandungi watak ruang kosong kerana lekukan kod
  • backslash (\) digunakan untuk melarikan diri watak khas dalam setiap jenis, kecuali rentetan dolar-slashy, di mana kita mesti menggunakan dolar ($) untuk melarikan diri

11. Kesimpulannya

Dalam artikel ini, kami membincangkan banyak cara untuk membuat rentetan di Groovy dan sokongannya untuk pelbagai baris, interpolasi, dan regex.

Semua coretan ini terdapat di Github.

Dan untuk maklumat lebih lanjut mengenai ciri-ciri bahasa Groovy itu sendiri, mulakan dengan pengenalan kami kepada Groovy.