Talk:StringUtil Script

From the CreationKit Wiki
Jump to navigation Jump to search
; Simple C++ like sprintf
;
; 
; %%    a percent sign
; %s    a string
; %d    a signed integer
; %f    a floating-point number
;
; @asSource String - The format string containing the % tokens
; @asReplacements String Array - The stack of replacements for the % tokens
String Function sprintf(String asSource, String[] asReplacements )
	Int iStart = 0
	Int iEnd = 0
	Int iIndex  = 0
	String sReturn = ""
	String sOperator = ""
	
	iEnd = StringUtil.Find(asSource, "%", iStart)
	If ( iEnd == -1 )
		Debug.Trace( "You are passing an unformatted string into sprintf: " + asSource )
		Return asSource
	Else
		While ( iEnd != -1 && iIndex < asReplacements.Length )
			sOperator = getNthChar(asSource, iEnd + 1)

			If ( sOperator == "%" )
				sReturn += Substring(asSource, iStart, iEnd) + "%"
			ElseIf ( sOperator == "s" )
				sReturn += Substring(asSource, iStart, iEnd) + asReplacements[ iIndex ] as String
			ElseIf ( sOperator == "d" )
				sReturn += Substring(asSource, iStart, iEnd) + asReplacements[ iIndex ] as Int
			ElseIf ( sOperator == "f" )
				sReturn += Substring(asSource, iStart, iEnd) + asReplacements[ iIndex ] as Float
			Else
				Debug.Trace( "Improper format for sprintf: " + asSource )
				Return asSource
			EndIf

			iIndex += 1
			iStart = iEnd + 2
			iEnd = StringUtil.Find(asSource, "%", iStart)
		EndWhile

		sReturn += Substring(asSource, iStart)
	EndIf
	
	Return sReturn
EndFunction

~ jbezorg Mar 17 2013

How to display the actor IDs in game[edit source]

; Simple way to display the actor IDs properly by removing the not-printable < and > characters.

Debug.Notification("Magic effect started on actor " + StringUtil.Substring (akTarget, StringUtil.GetLength (akTarget) - 11, 8) )