9 Strings
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的更多相关文章
- Hacker Rank: Two Strings - thinking in C# 15+ ways
March 18, 2016 Problem statement: https://www.hackerrank.com/challenges/two-strings/submissions/code ...
- 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 ...
- Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [LeetCode] Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- [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 ...
- [LeetCode] Group Shifted Strings 群组偏移字符串
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- [LeetCode] Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- [LeetCode] Multiply Strings 字符串相乘
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 使用strings查看二进制文件中的字符串
使用strings查看二进制文件中的字符串 今天介绍的这个小工具叫做strings,它实现功能很简单,就是找出文件内容中的可打印字符串.所谓可打印字符串的涵义是,它的组成部分都是可打印字符,并且以nu ...
- LeetCode 205 Isomorphic Strings
Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...
随机推荐
- Swing绘图机制
------------------siwuxie095 工程名:TestSwingPaintMethod 包名:com.siwuxie095.swin ...
- C++中的一类临时对象
类名(参数名)这样的对象是临时对象,不能取地址,不能被引用,不过可以给同类型的其他对象赋值,该临时对象定以后可以进行一次操作,然后立即销毁. 当我们定义一个对象以后并不想立即给它赋初值,而是以后给它赋 ...
- SQl Server 表链接查询
之前漏下了,这里补一偏 select * from student,score ——笛卡尔积 可以想想成c#里面的多维函数的样子,打印时每一张表的每一条数据都会连带着第二张表的所有数据 两个表的连接: ...
- c#基础 函数
函数 就是方法 独立完成某项功能的一个个体 函数的的三个好处:1.提高代码的重用性.2.提高功能开发的效率,3.提高代码的可维护性. 函数也分为 固定功能函数, 高度抽象函数.高度抽象函数应 ...
- 100078D Domestic Networks
传送门 题目大意 有两种染料,给定它们的单价和数量,每染色一米需耗费一个单位的染料,一条边只能用一种燃料,给你一张图,要求你将其中的一些边染色使得在满足图联通的情况下花费最小并输出方案. 分析 首先, ...
- input框去只读
设置为只读 $('input[name=video_link]').attr("readonly","readonly"); 去只读 $("#vide ...
- java线程安全的元素
集合类: Vector Stack HashTable enumeration 字符串: StringBuffer
- svn冲突问题详解 SVN版本冲突解决详解
svn冲突问题详解 SVN版本冲突解决详解 (摘自西西软件园,原文链接http://www.cr173.com/html/46224_1.html) 解决版本冲突的命令.在冲突解决之后,需要使用svn ...
- 《Maven实战》笔记-2-坐标和依赖
一.依赖范围 Maven在编译项目主代码的时候,需要使用一套classpath——编译classpath: 在编译和执行测试的时候,使用另一套classpath——测试classpath: 实际运行M ...
- C#多线程 线程嵌套调用问题
线程嵌套指的是:线程A的执行代码启动了线程B,线程B的执行代码又启动了线程C. 我原本以为线程A被Abort后,线程B会自动被Abort,但是我大错特错了. 在这种场景下,线程的管理就非常重要了. 线 ...