String Methods

The following are the currently-supported string methods:

basename

Defaults to string variables. When given a path-like string as input, this will return the basename (complete filename, extension included). IE: /parent/filename.ext -> filename.ext

echo $basename("/parent/filename.ext")
filename.ext

extension

Defaults to string variables. When given a path-like string as input, this will return the extension of the complete filename. IE: /parent/filename.ext -> ext.

echo $extension("/parent/filename.ext")
ext

filename

Defaults to string variables. When given a path-like string as input, this will return the file name portion of the complete filename. IE: /parent/filename.ext -> filename.

echo $filename("/parent/filename.ext")
filename

join

Defaults to array variables. When given an array as input, the join string method will concatenate each element in the array and return a string. If no argument is given, then those elements will be joined by a single space. Otherwise, each element will be joined with a given pattern.

let array = [1 2 3 4 5]
echo $join(array)
echo $join(array ", ")
1 2 3 4 5
1, 2, 3, 4, 5

find

Defaults to string variables. When given an string, it returns the first index in which that string appears. It returns -1 if it isn't contained.

echo $find("FOOBAR" "OB")
echo $find("FOOBAR" "ob")
2
-1

len

Defaults to string variables. Counts the number of graphemes in the output. If an array expression is supplied, it will print the number of elements in the array.

echo $len("foobar")
echo $len("❤️")
echo $len([one two three four])
6
1
4

len_bytes

Defaults to string variables. Similar to the len method, but counts the number of actual bytes in the output, not the number of graphemes.

echo $len_bytes("foobar")
echo $len_bytes("❤️")
6
6

parent

Defaults to string variables. When given a path-like string as input, this will return the parent directory's name. IE: /root/parent/filename.ext -> /root/parent

echo $parent("/root/parent/filename.ext")
/root/parent

repeat

Defaults to string variables. When supplied with a number, it will repeat the input N amount of times, where N is the supplied number.

echo $repeat("abc, " 3)
abc, abc, abc, 

replace

Defaults to string variables. Given a pattern to match, and a replacement to replace each match with, a new string will be returned with all matches replaced.

let input = "one two one two"
echo $replace(input one 1)
echo $replace($replace(input one 1) two 2)
1 two 1 two
1 2 1 2

replacen

Defaults to string variables. Equivalent to replace, but will only replace the first N amount of matches.

let input = "one two one two"
echo $replacen(input "one" "three" 1)
echo $replacen(input "two" "three" 2)
three two one two
one three one three

regex_replace

Defaults to string variables. Equivalent to replace, but the first argument will be treated as a regex.

PS: By default, unicode support will be disabled to trim the size of Ion. Add the "unicode" flag to enable it.

echo $regex_replace("bob" "^b" "B")
echo $regex_replace("bob" 'b$' "B")
Bob
boB

reverse

Defaults to string variables. Simply returns the same string, but with each grapheme displayed in reverse order.

echo $reverse("foobar")
raboof

to_lowercase

Defaults to string variables. All given strings have their characters converted to an lowercase equivalent, if an lowercase equivalent exists.

echo $to_lowercase("FOOBAR")
foobar

to_uppercase

Defaults to string variables. All given strings have their characters converted to an uppercase equivalent, if an uppercase equivalent exists.

echo $to_uppercase("foobar")
FOOBAR

escape

Defaults to string variables. Escapes the content of the string.

let line = " Mary   had\ta little  \n\t lamb\t"
echo $escape($line)
 Mary   had\\ta little  \\n\\t lamb\\t

unescape

Defaults to string variables. Unescapes the content of the string.

let line = " Mary   had\ta little  \n\t lamb\t"
echo $unescape($line)
 Mary   had	a little  
	 lamb	

or

Defaults to string variables. Fallback to a given value if the variable is not defined or is an empty string.

echo $or($unknown_variable "Fallback")
let var = 42
echo $or($var "Not displayed")
Fallback
42