Some functions for modifying and working with strings.
Note: These functions are not implemented in the Unreal Engine. They are for coders to add into their own code if they need them.
Contents
+=, @=
These two operators combine string concatenation and assignment. $=
doesn't work, that's why +=
is used here. (see Scripting Operators)
static final operator string += (out string A, coerce string B) { A = A $ B; return A; }
static final operator string @= (out string A, coerce string B) { A = A @ B; return A; }
Lower
This function is the equivalent to Caps that converts all uppercase characters in a string to lowercase (while leaving non-alphabetical characters untouched).
static final function string Lower(coerce string Text) { local int IndexChar; for (IndexChar = 0; IndexChar < Len(Text); IndexChar++) if (Mid(Text, IndexChar, 1) >= "A" && Mid(Text, IndexChar, 1) <= "Z") Text = Left(Text, IndexChar) $ Chr(Asc(Mid(Text, IndexChar, 1)) + 32) $ Mid(Text, IndexChar + 1); return Text; }
IsUpper, IsLower
These functions detect whether a string is completely uppercase or lowercase.
static final function bool IsUpper(coerce string S) { return S == Caps(S); }
static final function bool IsLower(coerce string S) { return S == Lower(S); }
AlphaNumeric
This function leaves only alphanumerical characters in the string, i.e. A-Z and 0-9, and converts alphabetical characters to uppercase. Useful for sorting items by name.
static final function string AlphaNumeric(string s) { local string result; local int i, c; for (i = 0; i < Len(s); i++) { c = Asc(Right(s, Len(s) - i)); if ( c == Clamp(c, 48, 57) ) // 0-9 result = result $ Chr(c); else if ( c == Clamp(c, 65, 90) ) // A-Z result = result $ Chr(c); else if ( c == Clamp(c, 97, 122) ) // a-z result = result $ Chr(c - 32); // convert to uppercase } return result; }
LTrim, RTrim, Trim
These functions remove spaces from the left, the right or both sides of a string.
static final function string LTrim(coerce string S) { while (Left(S, 1) == " ") S = Right(S, Len(S) - 1); return S; }
static final function string RTrim(coerce string S) { while (Right(S, 1) == " ") S = Left(S, Len(S) - 1); return S; }
static final function string Trim(coerce string S) { return LTrim(RTrim(S)); }
ReplaceText
This function replaces any occurance of a substring Replace
inside a string Text
with the string With
. This is a modified version of the ReplaceText
function available in the UWindowWindow class which doesn't return the string but assigns it to the variable Text
.
static final function string ReplaceText(coerce string Text, coerce string Replace, coerce string With) { local int i; local string Output; i = InStr(Text, Replace); while (i != -1) { Output = Output $ Left(Text, i) $ With; Text = Mid(Text, i + Len(Replace)); i = InStr(Text, Replace); } Output = Output $ Text; return Output; }
Unique string list
The following example will maintain a dynamic array with unique strings. The MyStringList is a sorted list so we break on the first entry that's larger then the new string, and insert the new string at that point.
You could use the same principle of every other type, just make sure you have a equals and larger than compare routines.
/** a sorted string list */ var array<string> MyStringList; /** returns true when added */ function bool AddString(coerce string newstring) { local int i; for (i = 0; i < MyStringList.length; i++) { if (MyStringList[i] == newstring) return false; if (MyStringList[i] > newstring) break; } MyStringList.Insert(i, 1); MyStringList[i] = newstring; return true; }
Splitting a String
Created this method since Unreal Runtime doesn't have a Split() method for splitting strings.
This method works exactly the same as the method in UT2k4, in Object.Split()
/**Parameters - str = input string div = divider bDiv = true to keep dividers, false to remove dividers.**/ function array<string> Split(string str, string div, bool bDiv) { local array<string> temp; local bool bEOL; local string tempChar; local int precount, curcount, wordcount, strLength; strLength = len(str); bEOL = false; precount = 0; curcount = 0; wordcount = 0; while(!bEOL) { tempChar = Mid(str, curcount, 1); //go up by 1 count if(tempChar != div) curcount++; else if(tempChar == div) { temp[wordcount] = Mid(str, precount, curcount-precount); wordcount++; if(bDiv) precount = curcount; //leaves the divider else precount = curcount + 1; //removes the divider. curcount++; } if(curcount == strLength)//end of string, flush out the final word. { temp[wordcount] = Mid(str, precount, curcount); bEOL = true; } } return temp; }
Related Topics
- Useful Maths functions
- Global Function
- Operators
Discussion
Juxtapose: Maybe this is covered in a page that links to this one, but shouldn't there be a better description of how to implement these? Having arrived at this page straight from a search, it took me a minute to figure out that these are not implemented in default code. I discovered in the source code that @= is implemented in UT2k4 Object class where += is not. Is there documentation on that method else where?
Fyfe: Wormbo implemented these functions in WUtils.
Caudex: Added function for Splitting strings, since Unreal Runtime doesn't have it as Native. If you want more methods for Strings, UDN has a pretty good topic about it. (http://udn.epicgames.com/Two/StringsInUnrealScript.html)
Category:Legacy To Do – Redirect to WUtils?