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. php中用大括号把?>和<?php框起来的作用

    <?php function my_function() { ?> My function was called <!--就是这里,为什么前面要用?>和< ?php 把M ...

  2. Java3D-对象基本变换

    一个球体与三个圆柱体形成一个组合体,在该组合体中,球体的透明度属性是由全透明到不透明之间变换,而且包括:旋转.平移等变换. package com.vfsd.test0621; import java ...

  3. Excel VBA连接MySql 数据库获取数据

    编写Excel VBA工具,连接并操作Mysql 数据库. 系统环境: OS:Win7 64位 英文版 Office 2010 32位 英文版 1.VBA连接MySql前的准备 Tools---> ...

  4. JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-007Inheritance of embeddable classes(@MappedSuperclass、@Embeddable、@AttributeOverrides、、)

    一.结构 二.代码 1. package org.jpwh.model.inheritance.embeddable; import javax.persistence.MappedSuperclas ...

  5. 一个小错误,在for循环中,每次repaint()的时候,记得先把frame涂成白色的。等于擦掉原来的痕迹·。

    import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Animate { int x=1; in ...

  6. R: 一页显示多张图的方法

    ################################################### 问题:一页多图显示   18.4.30 怎么实现,在一页上画多幅图,并且安排图的大小.个数等?? ...

  7. 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 ...

  8. html 里 checkbox里 只要选中就会自动添加checked=“checked”么?

    事实上HTML代码是不会发生变化的,但是控件对象的属性会发生变化以反映这个操作的结果.也就是说,该对象的checked属性值会由false变成true.但元素标签中并不会插入checked=" ...

  9. 发现C#winform编程中不常用的控件(一)<FlowLayoutPanel控件><拆分器控件Splitcontainer >

    第一部分:FlowLayoutPanel控件 实现效果: 将FlowLayoutPanel做为导航菜单按钮的容器 以实现 某个菜单按钮不显示时 整体的导航菜单布局不至于"缺憾" 原 ...

  10. MSCN(Mean Subtracted Contrast Normalized)系数的直方图

    MSCN系数是无参考的空间域图像质量评估算法BRISQUE(No-Reference Image Quality Assessment in the Spatial Domain)中提出的,MSCN系 ...