📅  最后修改于: 2020-11-04 06:39:29             🧑  作者: Mango
在Groovy中,通过将字符串文本括在引号中来构造String字面量。
Groovy提供了多种表示String字面量。 Groovy中的字符串可以用单引号(’),双引号(“)或三引号(”“”)括起来。此外,用三引号引起来的Groovy字符串可能会跨越多行。
以下是Groovy中字符串用法的示例-
class Example {
static void main(String[] args) {
String a = 'Hello Single';
String b = "Hello Double";
String c = "'Hello Triple" + "Multiple lines'";
println(a);
println(b);
println(c);
}
}
当我们运行上面的程序时,我们将得到以下结果-
Hello Single
Hello Double
'Hello TripleMultiple lines'
Groovy中的字符串是字符的有序序列。字符串的单个字符可以通过其位置进行访问。这由索引位置给出。
字符串索引从零开始,以小于字符串长度的一结束。 Groovy还允许负索引从字符串的结尾算起。
以下是Groovy中字符串索引用法的示例-
class Example {
static void main(String[] args) {
String sample = "Hello world";
println(sample[4]); // Print the 5 character in the string
//Print the 1st character in the string starting from the back
println(sample[-1]);
println(sample[1..2]);//Prints a string starting from Index 1 to 2
println(sample[4..2]);//Prints a string starting from Index 4 back to 2
}
}
当我们运行上面的程序时,我们将得到以下结果-
o
d
el
oll
首先,让我们学习groovy中的基本字符串操作。它们在下面给出。
S.No. | String Operation & Description |
---|---|
1 | Concatenation of two strings
The concatenation of strings can be done by the simple ‘+’ operator. |
2 | String Repetition
The repetition of strings can be done by the simple ‘*’ operator. |
3 | String Length
The length of the string determined by the length() method of the string. |
这是String类支持的方法的列表。
S.No. | Methods & Description |
---|---|
1 | center()
Returns a new String of length numberOfChars consisting of the recipient padded on the left and right with space characters. |
2 | compareToIgnoreCase()
Compares two strings lexicographically, ignoring case differences. |
3 | concat()
Concatenates the specified String to the end of this String. |
4 | eachMatch()
Processes each regex group (see next section) matched substring of the given String. |
5 | endsWith()
Tests whether this string ends with the specified suffix. |
6 | equalsIgnoreCase()
Compares this String to another String, ignoring case considerations. |
7 | getAt()
It returns string value at the index position |
8 | indexOf()
Returns the index within this String of the first occurrence of the specified substring. |
9 | matches()
It outputs whether a String matches the given regular expression. |
10 | minus()
Removes the value part of the String. |
11 | next()
This method is called by the ++ operator for the class String. It increments the last character in the given String. |
12 | padLeft()
Pad the String with the spaces appended to the left. |
13 | padRight()
Pad the String with the spaces appended to the right. |
14 | plus()
Appends a String |
15 | previous()
This method is called by the — operator for the CharSequence. |
16 | replaceAll()
Replaces all occurrences of a captured group by the result of a closure on that text. |
17 | reverse()
Creates a new String which is the reverse of this String. |
18 | split()
Splits this String around matches of the given regular expression. |
19 | subString()
Returns a new String that is a substring of this String. |
20 | toUpperCase()
Converts all of the characters in this String to upper case. |
21 | toLowerCase()
Converts all of the characters in this String to lower case. |