Interface: String Stay organized with collections Save and categorize content based on your preferences.
rules. String
Primitive type representing a string value.
Strings can be lexicographically compared using the==,!=,>,<,>= and<= operators.
Strings can be concatenated using the+ operator:
// Concatenate a username and an email domain'username'+'@domain.com'
Sub-strings can be accessed using the index operator[]. They can also be accessed using the range operator[i:j]. Note that parameterj, the upper bound in the range operator, is not inclusive.
// Check if the first character of a string is 'a'mystring[0]=='a'// Check if the string starts with 'abc'mystring[0:3]=='abc'
Boolean, integer, float, and null values can be converted into strings using thestring() function:
string(true) == "true"string(1) == "1"string(2.0) == "2.0"string(null) == "null"
Methods
lower
lower() returns rules.String
Returns a lowercase version of the input string.
- Returns
non-nullrules.Stringthe lowercase string.
Example
'ABC'.lower() == 'abc''ABC123'.lower() == 'abc123'matches
matches(re) returns rules.Boolean
Performs a regular expression match on the whole string.
Parameter | |
|---|---|
re | A regular expression usingGoogle RE2 syntax. Value must not be null. |
- Returns
non-nullrules.Booleantrue if the whole string matches, false otherwise.
Example
'user@domain.com'.matches('.*@domain[.]com') ==true'banana'.matches('.*@domain[.]com') ==falsereplace
replace(re, sub) returns rules.String
Replaces all occurrences of substrings matching a regular expression with a user-supplied string.
Parameter | |
|---|---|
re | A regular expression usingGoogle RE2 syntax. Value must not be null. |
sub | A string to substitute. Value must not be null. |
- Returns
non-nullrules.StringA string representing the result of the replacement operation. If no substrings matched the regular expression, the unmodified original string is returned.
Example
'banana'.replace("a","o") =='bonono''banana'.replace("ana","ee") =='beena''foo@test.com'.replace(".","-") =='---------------'//'.'regexmatchallsize
size() returns rules.Integer
Returns the number of characters in the string.
- Returns
non-nullrules.Integerthe number of characters.
Example
'a'.size() == 1'abc'.size() == 3split
split(re) returns rules.List
Splits a string according to a regular expression.
Parameter | |
|---|---|
re | A regular expression usingGoogle RE2 syntax. Value must not be null. |
- Returns
non-nullrules.Lista list of strings.
Example
'a/b/c'.split('/') == ['a', 'b', 'c']toUtf8
toUtf8() returns rules.Bytes
Returns the UTF-8 byte encoding of a string.
- Returns
non-nullrules.Bytesa Bytes sequence containing the UTF-8 encoded representation of the string.
Example
'**'.toUtf8() == b'\x2A\x2A''€'.toUtf8() == b'\xE2\x82\xAC'trim
trim() returns rules.String
Returns a version of the string with leading and trailing spaces removed.
- Returns
non-nullrules.Stringthe trimmed string.
Example
' a '.trim() == 'a''b'.trim() == 'b'upper
upper() returns rules.String
Returns an uppercase version of the input string.
- Returns
non-nullrules.Stringthe uppercase string.
Example
'abc'.upper() == 'ABC''abc123'.upper() == 'ABC123'Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-06-18 UTC.