Core Function StrIndex

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Example)
(Example)
Line 66: Line 66:
 
{
 
{
 
println(  $a[$i]  );
 
println(  $a[$i]  );
// StrIndex with 2 params only READS the strings chars
 
// It does not modify them
 
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Revision as of 15:05, 29 November 2011

StrIndex( <expression>, <index>, <value> )

Contents

Description

Get or set a char from/to a string at a specific index.

Parameters

expression

The string to use.

index

Index of the string to get/set data to/from.

If index is lower than 0 it becomes 0 and if its higher than strings length it becomes the strings length.

So it will always do something.

value

Optional; If this param exists it will set the CHAR value of this param to the string at the given index.

OTHERWISE it will return the char at the index.

Return Value

Success: Returns the char the given index IF only 2 params are used. Success: Returns nothing IF 3 params are used.

Example

// Set a string
$a = "Hello";
 
// This is one of the few functions
// that will modify the value directly
// rather than return it modified.
StrIndex($a, 1, "C"); // Actually changes the char in the string
StrIndex($a, 2, "a"); // Actually changes the char in the string
$b = StrIndex($a, 3, "t"); // Actually changes the char in the string
// Note - StrIndex with 3 parms does not return a value
 
println($a); // Prints HCato
println($b); // Does not print anything
 
// Print all chars
for($i = 0; $i < StrLen($a); $i++)
{
	println(   StrIndex($a, $i)   );
	// StrIndex with 2 params only READS the strings chars
	// It does not modify them
}
 
 
// Print all chars using the []
// The $a[$i] works to GET chars but it wont work to SET chars
// This is because of how strings are handled in Sputnik it
// would simply become an array if you tried to set data to that position
// but getting data from it is no problem
for($i = 0; $i < StrLen($a); $i++)
{
	println(   $a[$i]  );
}
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox