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:
UnrealScript doesn’t have a dedicated strings package, so we’re using built-in string functions and operators.
We’ve created a custom
Printfunction to mimic Go’sfmt.Println.The
Containsoperation is simulated using theInStrfunction.The
Countoperation is implemented by comparing the length of the original string with the length of the string after removing all occurrences of the target character.HasPrefixandHasSuffixare implemented usingLeftandRightfunctions respectively.Joinis implemented using theJoinArrayfunction.Repeatis implemented using theReplfunction with an empty replace string.Replaceis also implemented using theReplfunction.Splitis a built-in function in UnrealScript.ToLowerandToUpperare implemented usingLocsandCapsfunctions 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.