Core Function SPrintf

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Remarks)
(Example)
Line 54: Line 54:
  
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
$val = SPrintf("Value is 0x'{0:x}'", (int)100)
+
$val = SPrintf("Value is 0x'{0:x}'", (int)100);
Printf($val) ; Prints 0x64 since the {0:x} is telling it to place param 0 as :x meaning hex
+
Printf($val); // Prints 0x64 since the {0:x} is telling it to place param 0 as :x meaning hex
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Revision as of 20:42, 18 November 2011

SPrintf( <expression>, <expressions>... )

Contents

Description

Create a formatted string.

expression

The format string and flags to use (see Remarks).

expressions

Variables that will be output according to the "Format Control".

Return Value

Success: Returns the formatted string.

Failure: Returns an empty string.

Remarks

Warning: If you expect to use int, byte, float etc in the params then you must cast it as such for example:

$myint = 100;
$val = SPrintf("Value is 0x{0:x}", (int)$myint); // The (int) will only take in $myint
println($val);
 
$myint = 100;
$myint2 = 100;
$val = SPrintf("Value is 0x{0:x}", (int)($myint + $myint2)); // The (int) will now take in $myint AND $myint2
println($val);

Note how $myint was cast as an int? using (int)$myint this is vital since if you dont do that it might send a string or a float you never really know with $variables so its best to cast it.

There is an alternative method example:

$myint = 100;
$val = SPrintf("Value is 0x{0:x}", int($myint));

Its up to you to decide which one to use.

Go see Printf( <expression>, <expressions>... ) for details on the format string

Anything thats valid for Printf to print to console window is also valid for SPrintf to use to create strings.

Example

$val = SPrintf("Value is 0x'{0:x}'", (int)100);
Printf($val); // Prints 0x64 since the {0:x} is telling it to place param 0 as :x meaning hex
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox