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 ...
随机推荐
- Codeforces 1097F Alex and a TV Show (莫比乌斯反演)
题意:有n个可重集合,有四种操作: 1:把一个集合设置为单个元素v. 2:两个集合求并集. 3:两个集合中的元素两两求gcd,然后这些gcd形成一个集合. 4:问某个可重复集合的元素v的个数取模2之后 ...
- iOS 本地加载js文件
#import "RootViewController.h" @interface RootViewController ()<UIWebViewDelegate> @ ...
- 格式化字符串攻击原理及示例.RP
格式化字符串攻击原理及示例 一.类printf函数簇实现原理 类printf函数的最大的特点就是,在函数定义的时候无法知道函数实参的数目和类型. 对于这种情况,可以使用省略号指定参数表. 带有省略号的 ...
- Spring第六篇---AOP
接着Spring第五篇讲 我们今天将叙述以下几个知识点 1 什么是AOP AOP 是一种思想 横向重复 纵向抽取 在软件业,AOP为Aspect Oriented Programming的缩写,意 ...
- 数据结构--树--AVL树
详情查看:http://www.cnblogs.com/skywang12345/p/3577360.html
- CodeForces 566D Restructuring Company (并查集+链表)
题意:给定 3 种操作, 第一种 1 u v 把 u 和 v 合并 第二种 2 l r 把 l - r 这一段区间合并 第三种 3 u v 判断 u 和 v 是不是在同一集合中. 析:很容易知道是用并 ...
- Visual Studio2012快捷键总结
(1)如何设置更改快捷键 1.进入工具----选项 对话框 2.选择 环境---->键盘 3.在 [显示命令包含] 下面的对话框中输入“对齐”关键字,然后就会在这个编辑框下面一个文本窗口中显 ...
- 适配器设计模式及GenericServlet(九)
一共两个知识点:1.Servlet 里面已经有适配器了,名字叫:GenericServlet. 2.什么是适配器模式. 如果这个接口里面有好多方法,那创建A/B/C这个三个类的时候如果必须继 ...
- Mac下配置MAMP Pro+PHPStorm
一.配置MAMP Pro Hosts 下载地址:http://xclient.info/s/mamp-pro.html 在Hosts页签下,如图所示位置选择你工程目录 二.配置PHPStorm 1.点 ...
- Android View部分消失效果实现
本文来自网易云社区 作者:孙有军 老需求 我们经常会有需求就是View消失的效果,这里我们说的消失往往是全部消失,我们可能采用一个alpha动画,在指定的时间内消失掉View,出现则实现相反的动画.我 ...