String
a = "test_string";
b = "test" + "_string";
is_true = "test" == "test";
is_false = "test" == "string";
s = "abcdef"
puts(s[2])
puts(s[-2])
puts(s[:2])
puts(s[:-2])
puts(s[2:])
puts(s[-2:])
puts(s[1:-2])
s[2] = "C"
s[-2] = "E"
puts(s)
// should output
"c"
"e"
"ab"
"abcd"
"cdef"
"ef"
"bcd"
"abCdEf"
// you can also use single quotes
'test "string" with doublequotes'
// and you can scape a double quote in a double quote string
"te\"st" == 'te"st'
Literal Specific Methodsโ
count(STRING)โ
Returns
INTEGER
Counts how often a given substring occurs in the string.
๐ > "test".count("t")
=> 2
๐ > "test".count("f")
=> 0
๐ > "test1".count("1")
=> 1
downcase()โ
Returns
STRING
Returns the string with all uppercase letters replaced with lowercase counterparts.
๐ > "TeST".downcase()
=> test
downcase!()โ
Returns
NIL
Replaces all upcase characters with lowercase counterparts.
๐ > a = "TeST"
=> TeST
๐ > a.downcase!()
=> nil
๐ > a
=> test
find(STRING)โ
Returns
INTEGER
Returns the character index of a given string if found. Otherwise returns -1
๐ > "test".find("e")
=> 1
๐ > "test".find("f")
=> -1
format(STRING|INTEGER|FLOAT|BOOLEAN|ARRAY|HASH)โ
Returns
STRING
Formats according to a format specifier and returns the resulting string
๐ ยป "test%9d".format(1)
ยป "test 1"
๐ ยป "test%1.2f".format(1.5)
ยป "test1.50"
๐ ยป "test%s".format("test")
ยป "testtest"
lines()โ
Returns
ARRAY
Splits the string at newline escape sequence and return all chunks in an array. Shorthand for string.split("\n")
.
๐ > "test\ntest2".lines()
=> ["test", "test2"]
plz_i(INTEGER)โ
Returns
INTEGER
Interprets the string as an integer with an optional given base. The default base is 10
and switched to 8
if the string starts with 0x
.
๐ > "1234".plz_i()
=> 1234
๐ > "1234".plz_i(8)
=> 668
๐ > "0x1234".plz_i(8)
=> 668
๐ > "0x1234".plz_i()
=> 668
๐ > "0x1234".plz_i(10)
=> 0
replace(STRING, STRING)โ
Returns
STRING
Replaces the first string with the second string in the given string.
๐ > "test".replace("t", "f")
=> "fesf"
reverse()โ
Returns
STRING
Returns a copy of the string with all characters reversed.
๐ > "stressed".reverse()
=> "desserts"
reverse!()โ
Returns
NIL
Replaces all the characters in a string in reverse order.
๐ > a = "stressed"
=> "stressed"
๐ > a.reverse!()
=> nil
๐ > a
=> "desserts"
size()โ
Returns
INTEGER
Returns the amount of characters in the string.
๐ > "test".size()
=> 4
split(STRING)โ
Returns
ARRAY
Splits the string on a given seperator and returns all the chunks in an array. Default seperator is " "
๐ > "a,b,c,d".split(",")
=> ["a", "b", "c", "d"]
๐ > "test and another test".split()
=> ["test", "and", "another", "test"]
strip()โ
Returns
STRING
Returns a copy of the string with all leading and trailing whitespaces removed.
๐ > " test ".strip()
=> "test"
strip!()โ
Returns
NIL
Removes all leading and trailing whitespaces in the string.
๐ > a = " test "
=> " test "
๐ > a.strip!()
=> nil
๐ > a
=> "test"
upcase()โ
Returns
STRING
Returns the string with all lowercase letters replaced with uppercase counterparts.
๐ > "test".upcase()
=> TEST
upcase!()โ
Returns
NIL
Replaces all lowercase characters with upcase counterparts.
๐ > a = "test"
=> test
๐ > a.upcase!()
=> nil
๐ > a
=> TEST
Generic Literal Methodsโ
methods()โ
Returns
ARRAY
Returns an array of all supported methods names.
๐ > "test".methods()
=> [count, downcase, find, reverse!, split, lines, upcase!, strip!, downcase!, size, plz_i, replace, reverse, strip, upcase]
to_json()โ
Returns
STRING|ERROR
Returns the object as json notation.
๐ > a = {"test": 1234}
=> {"test": 1234}
๐ > a.to_json()
=> "{"test":1234}"
type()โ
Returns
STRING
Returns the type of the object.
๐ > "test".type()
=> "STRING"
wat()โ
Returns
STRING
Returns the supported methods with usage information.
๐ > true.wat()
=> BOOLEAN supports the following methods:
plz_s()