Talk:StringUtil Script
Revision as of 11:45, 17 March 2013 by imported>Jbezorg (Created page with "<source lang="papyrus"> ; Simple C++ like sprintf ; ; ; %% a percent sign ; %s a string ; %d a signed integer ; %f a floating-point number ; ; @asSource String - ...")
; 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