| Home Page | Recent Changes | Preferences

Useful String Functions

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.

+=, @=

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;
}

Related Topics

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.

Category To Do – Redirect to WUtils?

The Unreal Engine Documentation Site

Wiki Community

Topic Categories

Image Uploads

Random Page

Recent Changes

Offline Wiki

Unreal Engine

Console Commands

Terminology

FAQs

Help Desk

Mapping Topics

Mapping Lessons

UnrealEd Interface

UnrealScript Topics

UnrealScript Lessons

Making Mods

Class Tree

Modeling Topics

Chongqing Page

Log In