String Functions in AngelScript

import string from "string";

void println(const string &in msg)
{
    print(msg + "\n");
}

void main()
{
    // Here's a sample of the functions available in
    // the string module. Since these are functions from the
    // module, not methods on the string object itself,
    // we need to pass the string in question as the first
    // argument to the function.

    println("Contains:  " + string::contains("test", "es"));
    println("Count:     " + string::count("test", "t"));
    println("HasPrefix: " + string::beginsWith("test", "te"));
    println("HasSuffix: " + string::endsWith("test", "st"));
    println("Index:     " + string::findFirst("test", "e"));
    println("Join:      " + string::join(array<string> = {"a", "b"}, "-"));
    println("Repeat:    " + string::repeat("a", 5));
    println("Replace:   " + string::replace("foo", "o", "0"));
    println("Replace:   " + string::replaceFirst("foo", "o", "0"));
    println("Split:     " + string::split("a-b-c-d-e", "-"));
    println("ToLower:   " + string::toLower("TEST"));
    println("ToUpper:   " + string::toUpper("test"));
}

The standard library’s string module provides many useful string-related functions. Here are some examples to give you a sense of the module.

We import the string module and define a println function for convenience, as AngelScript doesn’t have a built-in println function.

In the main function, we demonstrate various string operations:

  1. contains: Checks if a string contains a substring.
  2. count: Counts occurrences of a substring.
  3. beginsWith: Checks if a string starts with a prefix (equivalent to HasPrefix).
  4. endsWith: Checks if a string ends with a suffix (equivalent to HasSuffix).
  5. findFirst: Finds the index of the first occurrence of a substring (equivalent to Index).
  6. join: Joins an array of strings with a separator.
  7. repeat: Repeats a string a specified number of times.
  8. replace: Replaces all occurrences of a substring.
  9. replaceFirst: Replaces the first occurrence of a substring.
  10. split: Splits a string by a separator.
  11. toLower: Converts a string to lowercase.
  12. toUpper: Converts a string to uppercase.

Note that AngelScript’s string module functions might have slightly different names or behavior compared to Go’s strings package. For example, HasPrefix is beginsWith in AngelScript, and HasSuffix is endsWith.

To run this script, you would typically use an AngelScript interpreter or embed it in a host application that supports AngelScript.