Skip to content

The String Standard Library

C272 edited this page Dec 18, 2019 · 9 revisions

Contains all string manipulation and regex functions for Algo. The contents of this library are automatically imported into Algo on runtime. You can call all of these without using any import statements. Don't try and import this library, as it will throw an error.


string.toChars(x)

Splits a string into its individual characters, returned in a list.

Parameters:

Name Description
x The string to split into characters.

Usage:

let x = "hello";
print string.toChars(x); //["h", "e", "l", "l", "o"]

string.contains(base, sub)

Checks whether a substring is contained within a string.

Parameters:

Name Description
base The base string to check for substrings in.
sub The substring to check for.

Usage:

if (string.contains("foo", "oo"))
{
    print "hello"; //this is printed
}

string.split(str, delim)

Splits a given string using the provided delimiter, returning a list. The list is always at least one value long.

Parameters:

Name Description
str The string to split into a list.
delim The delimiter to split by.

Usage:

let x = "This! is! a! string";
print string.split(x, "! "); //["This", "is", "a", "string"]

string.replace(base, old, new)

Replace any occurences of a substring with another string (eg. replace "a" with "b" in "aaa" will result in "bbb").

Parameters:

Name Description
base The base string to replace in.
old The substring to replace.
new The new string to replace the old one with.

Usage:

let x = "hello world!";
x = string.replace(x, "hello", "eyo");
print x; //eyo world!

string.reverse(x)

Reverses the given string, and returns it.

Parameters:

Name Description
x The string to reverse.

Usage:

print string.reverse("wahoo"); //oohaw

string.substring(base, start, length)

Returns a substring of the original string, given a start and a length.

Parameters:

Name Description
base The base string to get a substring from.
start The starting index (included).
length The length of the substring.

Usage:

print string.substring("hello world", 2, 3); //llo 

string.endsWith(base, sub)

Returns a boolean, representing whether a given string ends with a substring (eg. returns true for "does 'chocolate' end in 'ate').

Parameters:

Name Description
base The base string to check the end of.
sub The substring to look for at the end.

Usage:

if (string.endsWith("qualification", "cation")) //true
{
    print "\qQualification\q ends with \qcation\q";
}

string.startsWith(base, sub)

Returns a boolean, representing whether a given string starts with a substring (eg. returns true for "does 'chocolate' start with 'cho').

Parameters:

Name Description
base The base string to check the start of.
sub The substring to look for at the start.

Usage:

if (string.startsWith("qualification", "qualif")) //true
{
    print "\qQualification\q starts with \qqualif\q";
}

string.isInteger(x)

Checks whether a given string would properly evaluate to an Algo integer, returns a boolean.

Parameters:

Name Description
x The string to check.

Usage:

let x = "3";
print string.isInteger(x); //true

string.isFloat(x)

Checks whether a given string would properly evaluate to an Algo floating point number, returns a boolean.

Parameters:

Name Description
x The string to check.

Usage:

let x = "3.14159265";
print string.isFloat(x); //true

string.toUpper(x)

Returns the given string, but converted to be all upper case.

Parameters:

Name Description
x The string to make upper case.

Usage:

print string.toUpper("Hello world."); //HELLO WORLD.

string.toLower(x)

Returns the given string, but converted to be all lower case.

Parameters:

Name Description
x The string to make lower case.

Usage:

print string.toLower("HeLlo WorLd."); //hello world.