Skip to main content
Version: v0.20.0

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|INTEGER)โ€‹

Returns INTEGER

Counts how often a given string or integer occurs in the string. Converts given integers to strings automatically.

๐Ÿš€ > "test".count("t")
=> 2
๐Ÿš€ > "test".count("f")
=> 0
๐Ÿš€ > "test1".count("1")
=> 1
๐Ÿš€ > "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|INTEGER)โ€‹

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)โ€‹

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()