String¶
Represents a string value. Strings can be prefixed with r
to parse them as they are. Strings are always multi-line.
'[\S\s]*'
"[\S\s]*"
r'[\S\s]*'
r"[\S\s]*"
'Hello'
"Hello"
r'This is a raw string'
r"This is a raw string"
'Supports escape sequences like \t \n'
"but also unicodes like \u0041 and \x41"
'Easy
and
peasy'
Properties¶
isEmpty
¶
Is string empty?
-> : Boolean
str := "Hello World!";
# prints false
print(str.isEmpty());
isNotEmpty
¶
Is string not empty?
-> : Boolean
str := "Hello World!";
# prints true
print(str.isNotEmpty());
length
¶
Length of the string.
-> : Number
str := "Hello World!";
# prints 12
print(str.length());
compareTo
¶
Compare to another string. Returns 0
if equal.
-> String other : Number
str1 := "Hello";
str2 := "World";
# prints -1
print(str1.compareTo(str2));
contains
¶
Check if other
is present in the string.
-> String other : Boolean
str := "Hello World!";
# prints true
print(str.contains("!"));
startsWith
¶
Check if the string is prefixed with other
.
-> String other : Boolean
str := "Hello World!";
# prints true
print(str.startsWith("Hell"));
endsWith
¶
Check if the string is suffixed with other
.
-> String other : Boolean
str := "Hello World!";
# prints true
print(str.endsWith("!"));
indexOf
¶
Position of substring
in the string. Returns -1
if not present.
-> String substring : Number
str := "Hello World!";
# prints 6
print(str.indexOf("W"));
substring
¶
Returns a substring between start
and end
.
-> Number start, Number end : String
str := "Hello World!";
# prints "World"
print(str.substring(6, 11));
replaceFirst
¶
Replaces first substring
with with
.
-> String pattern, String with : String
str := "Hello World!";
# prints "Heelo World!"
print(str.replaceFirst("l", "e"));
replaceAll
¶
Replaces all substring
with with
.
-> String pattern, String with : String
str := "Hello World!";
# prints "Heeeo World!"
print(str.replaceAll("l", "e"));
replaceFirstMapped
¶
Replaces first substring
with value returned by with
.
-> String pattern, (-> String : String) mapper : String
str := "Hello World!";
# prints "Heilo World!"
print(str.replaceFirstMapped("l", -> _ : "i"));
replaceAllMapped
¶
Replaces all substring
with value returned by with
.
-> String pattern, (-> String : String) mapper : String
str := "Hello World!";
# prints "Heiio World!"
print(str.replaceAllMapped("l", -> _ : "i"));
trim
¶
Removes all whitespaces at the ends.
-> : String
str := "\tHello World! ";
# prints "Hello World!"
print(str.trim());
trimLeft
¶
Removes all whitespaces at the start.
-> : String
str := " Hello World! ";
# prints "Hello World "
print(str.trimLeft());
trimRight
¶
Removes all whitespaces at the end.
-> : String
str := " Hello World! ";
# prints " Hello World"
print(str.trimRight());
padLeft
¶
Pads using with
at the start.
-> Number length, String with : String
str := "1";
# prints 01
print(str.padLeft(2, "0"));
padRight
¶
Pads using with
at the end.
-> Number length, String with : String
str := "1";
# prints 10
print(str.padRight(2, "0"));
split
¶
Splits the string at splitter
s.
-> String splitter : List<String>
str := "Hello!";
# prints ["He", "o!"]
print(str.split("ll"));
charAt
¶
Returns character at index
.
-> Number index : String
str := "Hello World!";
# prints "e"
print(str.charAt(1));
codeUnitAt
¶
Returns code-unit at index
.
-> Number index : String
str := "Hello World!";
# prints 101
print(str.codeUnitAt(1));
toCodeUnits
¶
Returns code-units of the string.
-> : List<Number>
str := "Hello!";
# prints [72, 101, 108, 108, 111, 33]
print(str.toCodeUnits());
toLowerCase
¶
Returns the string in lowercase.
-> : String
str := "Hello World!";
# prints "hello world!"
print(str.toLowerCase());
toUpperCase
¶
Returns the string in uppercase.
-> : String
str := "Hello World!";
# prints "HELLO WORLD!"
print(str.toUpperCase());
format
¶
Returns the formatted string. Example: "{} {1}".format(["Hello", "World"])
, "{hello} {world}".format({ hello: "Hello", world: "World" })
returns Hello World
-> (Object | List<Any>) env : String
# prints "Hello World!"
print("{} {}!".format(["Hello", "World"]));