String Functions in UnrealScript

class StringFunctions extends Object;

// We define a custom print function to mimic Go's fmt.Println
function Print(string Message)
{
    `log(Message);
}

// Main function to demonstrate string operations
static function ExecuteStringOperations()
{
    local array<string> SplitResult;
    local array<string> JoinArray;

    // Here's a sample of the string operations available in UnrealScript.
    // Note that UnrealScript doesn't have a dedicated strings package,
    // so we're using built-in string functions and operators.

    Print("Contains:  " $ string(InStr("test", "es") != -1));
    Print("Count:     " $ string(Len("test") - Len(Repl("test", "t", ""))));
    Print("HasPrefix: " $ string(Left("test", 2) == "te"));
    Print("HasSuffix: " $ string(Right("test", 2) == "st"));
    Print("Index:     " $ string(InStr("test", "e")));
    
    JoinArray.AddItem("a");
    JoinArray.AddItem("b");
    Print("Join:      " $ JoinArray(JoinArray, "-"));
    
    Print("Repeat:    " $ Repl("a", 5));
    Print("Replace:   " $ Repl("foo", "o", "0"));
    Print("Replace:   " $ Repl("foo", "o", "0",, 1));
    
    Split("a-b-c-d-e", "-", SplitResult);
    Print("Split:     " $ string(SplitResult));
    
    Print("ToLower:   " $ Locs("TEST"));
    Print("ToUpper:   " $ Caps("test"));
}

defaultproperties
{
}

This UnrealScript code demonstrates various string operations that are similar to the ones shown in the Go example. Here’s a breakdown of the differences and similarities:

  1. UnrealScript doesn’t have a dedicated strings package, so we’re using built-in string functions and operators.

  2. We’ve created a custom Print function to mimic Go’s fmt.Println.

  3. The Contains operation is simulated using the InStr function.

  4. The Count operation is implemented by comparing the length of the original string with the length of the string after removing all occurrences of the target character.

  5. HasPrefix and HasSuffix are implemented using Left and Right functions respectively.

  6. Join is implemented using the JoinArray function.

  7. Repeat is implemented using the Repl function with an empty replace string.

  8. Replace is also implemented using the Repl function.

  9. Split is a built-in function in UnrealScript.

  10. ToLower and ToUpper are implemented using Locs and Caps functions respectively.

To run this code, you would typically include it in an UnrealScript class and call the ExecuteStringOperations function from elsewhere in your Unreal Engine project. The output would be similar to the Go example, printed to the Unreal Engine log.

Note that UnrealScript has some limitations compared to more general-purpose languages like Go, but it provides sufficient functionality for most game development needs within the Unreal Engine environment.