1       Strings

1.1  Strings and GStrings

Groovy允许你使用2种不同类型的字符串类型,分别是java.lang.String和groovy.lang.GString类。在Groovy中,你也可以定义单行或多行的字符串。

字符串使用””,括起来。ofGString 会自动转换为java.lang.String。

package first

class StringTest {

static main(args) {

def name="John"

def s1="Hello $name" //$name将会被替换

def s2="Hello $name"

println s1

println s2

println s1.class

println s2.class

//举例对象引用并方法调用

def date = new Date()

println "We met at $date"

println "We met at ${date.format('yyyy-MM-dd')}"

}

}

 

输出

下边表格中,定义了不同类型的Strings。

Table 2. Define Strings in Groovy

String example

Description

'This is a String'

标准的java String

"This is a GString"

Groovy GString,允许变量替换和方法调用

''' Multiline string (with line breaks)'''

多行string

""" Multiline string (with line breaks)"""

多行GString

/regularexpression/

前后都加/,使得正则表达式更加可读。

tokenize()方法,切割String进入到一个String list,使用空白符作为分隔符。

Groovy JDK增加toURL()到String,意思是允许一个String转为URL。

trim 方法,删除字符串收尾的空白字符(空格)。

1.2  Operator overloading in Strings

String支持操作重载。使用+连接字符串,使用-截取字符串,使用 left-shift追加到字符串。

9 Strings的更多相关文章

  1. Hacker Rank: Two Strings - thinking in C# 15+ ways

    March 18, 2016 Problem statement: https://www.hackerrank.com/challenges/two-strings/submissions/code ...

  2. StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?

    StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing t ...

  3. Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  4. [LeetCode] Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  5. [LeetCode] Encode and Decode Strings 加码解码字符串

    Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...

  6. [LeetCode] Group Shifted Strings 群组偏移字符串

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  7. [LeetCode] Isomorphic Strings 同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  8. [LeetCode] Multiply Strings 字符串相乘

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  9. 使用strings查看二进制文件中的字符串

    使用strings查看二进制文件中的字符串 今天介绍的这个小工具叫做strings,它实现功能很简单,就是找出文件内容中的可打印字符串.所谓可打印字符串的涵义是,它的组成部分都是可打印字符,并且以nu ...

  10. LeetCode 205 Isomorphic Strings

    Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...

随机推荐

  1. 通过iOS中的按钮来触发html文件中按钮所触发的函数

    html文件的代码 <!DOCTYPE html> <html> <head> <title>标题</title> </head> ...

  2. acrord32 pdf自动化

    这个东西就是帮助用户自动化处理pdf的 . 可以看作是adobe reader的命令行

  3. loj10100 网络

    这个题目描述好难理解呀qwq... 传送门 分析 在读懂题之后我们不难发现这道题实际就是在求一张图中有多少个割点.只需要注意读入方式即可. 代码 #include<iostream> #i ...

  4. 精美3D中国象棋

    本人2013年的巅峰之作,现在已经完全免费放送.象棋界面的史诗革命.当前下载版本仅支持Windows 平台. 操作: 方向键的 上,下,左,右 控制棋盘翻转.Home 键回到初始状态,End按键回到平 ...

  5. Entity Framework Tutorial Basics(23):Add Single Entity

    Add New Entity using DBContext in Disconnected Scenario: In this chapter you will learn how to add n ...

  6. widget自定义控件【android.view.InflateException: Binary XML file line #2: Error inflating class...】

    此错误比较难定位,场景是这样的:在一个widget中使用了自定义控件,始终会报 android.view.InflateException: Binary XML file line #2: Erro ...

  7. jQuery 插件开发——GridData(表格)

    导读:我个人认为做开发最幸福的事之一就是设计一套属于自己的控件,老早之前就想去做这样的事情,一直碍于事件的冲突和个人的想法,最终没有定论,最近难得抽出一些空隙,去完成这件事情.其实自定义控件并不是难事 ...

  8. 使用穷人版profiler定位调试MySQL

    此文已由作者温正湖授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 周末闲得蛋疼,来英飞特做人工空气净化器.开了电脑后,习惯性得点击xshell按钮,进入InnoSQL稳定性测 ...

  9. Python3中实现简单的购物车程序

    product_list = [ ('iphone',5800), ('imac',15800), ('watch',9800), ('cloth',550), ('coffe latee',35), ...

  10. Java性能优化之高性能JAVA代码的若干个习惯

    创建对象: 1.避免在循环体中创建对象,循环前应该创建对象,避免浪费更多内存空间和增加GC负担 这种情况在我们的实际应用中经常遇到,而且我们很容易犯类似的错误,例如下面的代码: for (int i ...