The three virtues of a programmer: Laziness, Impatience, and Hubris. – Larry Wall

Difference between revisions of "Legacy:Useful String Functions"

From Unreal Wiki, The Unreal Engine Documentation Site
Jump to: navigation, search
(Splitting a String)
 
(One intermediate revision by one other user not shown)
Line 203: Line 203:
 
   }
 
   }
 
   return temp;
 
   return temp;
 +
}
 +
</uscript>
 +
==DrawText for multiline text==
 +
 +
<uscript>
 +
function DrawText(Canvas C, coerce string Text, float X, float Y, optional bool CR) {
 +
local float _, DY;
 +
local int Pos;
 +
local string cCR, LF, Line;
 +
cCR = chr(13);
 +
LF = chr(10);
 +
C.StrLen("X", _, DY);
 +
while (true) {
 +
Pos = InStr(Text, LF);
 +
if (Pos == -1) break;
 +
Line = Left(Text, Pos);
 +
Text = Mid(Text, Pos + 1);
 +
if (Pos > 0 && Right(Line, 1) == cCR) Line = Left(Line, Pos - 1);
 +
C.SetPos(X, Y);
 +
C.DrawText(Line, CR);
 +
Y = Y + DY;
 +
}
 +
if (Text != "" && Right(Text, 1) == cCR) Text = Left(Text, Len(Text) - 1);
 +
C.SetPos(X, Y);
 +
C.DrawText(Text, CR);
 +
}
 +
</uscript>
 +
==ClientMessage for multiline text==
 +
 +
<uscript>
 +
function ClientMessage(Pawn Pawn, coerce string Text, optional name Type, optional bool bBeep) {
 +
local int Pos;
 +
local string CR, LF, Line;
 +
CR = chr(13);
 +
LF = chr(10);
 +
while (true) {
 +
Pos = InStr(Text, LF);
 +
if (Pos == -1) break;
 +
Line = Left(Text, Pos);
 +
Text = Mid(Text, Pos + 1);
 +
if (Pos > 0 && Right(Line, 1) == CR) Line = Left(Line, Pos - 1);
 +
Pawn.ClientMessage(Line, Type);
 +
}
 +
if (Text != "" && Right(Text, 1) == CR) Text = Left(Text, Len(Text) - 1);
 +
Pawn.ClientMessage(Text, Type, bBeep);
 
}
 
}
 
</uscript>
 
</uscript>
Line 218: Line 263:
 
'''Fyfe:''' Wormbo implemented these functions in [[Legacy:WUtils|WUtils]].  
 
'''Fyfe:''' Wormbo implemented these functions in [[Legacy:WUtils|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.
+
'''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]] &ndash; Redirect to [[Legacy:WUtils|WUtils]]?
 
[[:Category:Legacy To Do]] &ndash; Redirect to [[Legacy:WUtils|WUtils]]?
 
[[Category:Legacy To Do|{{PAGENAME}}]]
 
[[Category:Legacy To Do|{{PAGENAME}}]]

Latest revision as of 17:25, 4 November 2018

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.

+=, @=[edit]

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[edit]

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[edit]

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[edit]

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[edit]

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[edit]

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[edit]

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[edit]

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

DrawText for multiline text[edit]

function DrawText(Canvas C, coerce string Text, float X, float Y, optional bool CR) {
	local float _, DY;
	local int Pos;
	local string cCR, LF, Line;
	cCR = chr(13);
	LF = chr(10);
	C.StrLen("X", _, DY);
	while (true) {
		Pos = InStr(Text, LF);
		if (Pos == -1) break;
		Line = Left(Text, Pos);
		Text = Mid(Text, Pos + 1);
		if (Pos > 0 && Right(Line, 1) == cCR) Line = Left(Line, Pos - 1);
		C.SetPos(X, Y);
		C.DrawText(Line, CR);
		Y = Y + DY;
	}
	if (Text != "" && Right(Text, 1) == cCR) Text = Left(Text, Len(Text) - 1);
	C.SetPos(X, Y);
	C.DrawText(Text, CR);
}

ClientMessage for multiline text[edit]

function ClientMessage(Pawn Pawn, coerce string Text, optional name Type, optional bool bBeep) {
	local int Pos;
	local string CR, LF, Line;
	CR = chr(13);
	LF = chr(10);
	while (true) {
		Pos = InStr(Text, LF);
		if (Pos == -1) break;
		Line = Left(Text, Pos);
		Text = Mid(Text, Pos + 1);
		if (Pos > 0 && Right(Line, 1) == CR) Line = Left(Line, Pos - 1);
		Pawn.ClientMessage(Line, Type);
	}
	if (Text != "" && Right(Text, 1) == CR) Text = Left(Text, Len(Text) - 1);
	Pawn.ClientMessage(Text, Type, bBeep);
}

Related Topics[edit]

Discussion[edit]

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?