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. 算法Sedgewick第四版-第1章基础-012一用stack实现输出一个数的二进制形式

    @Test public void e1_3_5() { Stack<Integer> stack = new Stack<Integer>(); int N = 7; whi ...

  2. Git 之 github分享代码

    作为一个技术人员还是脱离不了屌丝的本质,所以每天都是逛逛github,看看别人有什么好的项目,自己可以给他挑挑bug也可以提供自己的水平,但是别人不那怎么才能给别人贡献代码呢?那就是fork了.... ...

  3. docker安装oracle

    最近工作上面遇到一个性能相关的问题,大体描述一下: 批量任务执行的过程中导致数据库sql执行时间过长,查看之后是由于批量任务占满数据库连接池导致的,至于为什么批量任务会不受系统控制导致连接池占满就不说 ...

  4. Java NIO学习笔记

    Java NIO学习笔记 一 基本概念 IO 是主存和外部设备 ( 硬盘.终端和网络等 ) 拷贝数据的过程. IO 是操作系统的底层功能实现,底层通过 I/O 指令进行完成. 所有语言运行时系统提供执 ...

  5. Win10下Tensorflow+GPU的环境配置

    不得不说,想要为深度学习提前打好框架确实需要花费一番功夫.本文主要记录了Win10下,Cuda9.0.Cudnn7.3.1.Tensorflow-gpu1.13.1.python3.6.8.Keras ...

  6. HBase - 安装过程中的问题

    问题1:启动时start-hbase.sh 报 权限不够 原因:在移动文件时,使用root用户在/usr/local下创建的hbase,所以hbase文件夹的使用者为root,其他人没权限 解决方案: ...

  7. 「十二省联考 2019」皮配——dp

    题目 [题目描述] #### 题目背景一年一度的综艺节目<中国好码农>又开始了.本季度,好码农由 Yazid.Zayid.小 R.大 R 四位梦想导师坐镇,他们都将组建自己的梦想战队,并率 ...

  8. ObjectARXWizards & AutoCAD .NET Wizards 下载地址

    Autodesk Developer Network ObjectARX Wizards The ObjectARX Wizards for AutoCAD 2016 for  Visual Stud ...

  9. SharePoint安装注意点

    在安装SharePoint之前需要注意的地方(整理如下:) 1.首先得先安装IIS服务器和ApplicationServer 2.然后要在运行setup.exe之前先运行prerequisiteins ...

  10. Python——用socket和线程实现全双工收发数据

    用socket和线程实现全双工收发数据 1.基础知识 Socket(套接字) 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket.由此知道套接字是全双工的. 线程 ...