1       Strings

1.1  Strings and GStrings

Groovy允许你使用2种不同类型的字符串类型,分别是java.lang.Stringgroovy.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. Struts2框架01【如果使用struts框架】【利用struts框架写一个 hello world】

    1 什么是Struts2框架 基于MVC设计模式的web应用框架 Struts2框架是一个轻量级的MVC流程框架 轻量级是指程序的代码不是很多,运行时占用的资源不是很多,MVC流程框架就是说它是支持分 ...

  2. 获取当前设备的CPU个数

    public class Test { public static void main(String[] args) { //获取当前设备的CPU个数 int availableProcessors ...

  3. iOS 打包生成ipa文件(使用终端命令打包)

    1. 打开终端 2.在终端输入cd +空格 把工程文件直接拖到终端,然后回车 3. 在终端输入xcodebuild,回车 然后你可以发现工程文件里多了一个build文件夹 在build文件夹中,有一个 ...

  4. GCD学习(六) dispatch_async 和dispatch_sync

    dispatch_sync(),同步添加操作.他是等待添加进队列里面的操作完成之后再继续执行. dispatch_queue_t concurrentQueue = dispatch_queue_cr ...

  5. oracle date 和 timestamp区别

    在今天的工作中,学到了以下几个知识点: 一.date和timestamp 的区别 date类型是Oracle常用的日期型变量,他的时间间隔是秒.两个日期型相减得到是两个时间的间隔,注意单位是“天”.例 ...

  6. 检测评价函数 IOU

    在目标检测的评价体系中,有一个参数叫做 IoU(intersection-over-union),简单来讲就是模型产生的目标窗口和原来标记窗口的交叠率.具体我们可以简单的理解为: 即检测结果(Dete ...

  7. tensorboard的安装及遇到的问题

    1 安装tensorboard 打开anaconda prompt,键入下边的命令: activate tensorflow pip install tensorboard 当执行“activate ...

  8. 与HDFS交互- By java API编程

    环境(ubuntu下) jdk eclipse jar(很烦,整了很久才清楚) - 导包方法 查看:https://www.cnblogs.com/floakss/p/9739030.html ()” ...

  9. html中img标签的url如何拼接变量

    <img id="pic" /> <script type="text/javascript"> var url = "xxx ...

  10. VS2015无法创建C++工程解决方法!!

    VS2015默认安装时候没有安装C++,如果安装C++没有选择全部C++项目,则无法创建C++工程,在控制面板里的删除程序中,选择VS2015,随后选择修改,把C++项目都选择上就可以了,这样安装完毕 ...