Core Function StrIndex

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "<pre> StrIndex( <expression>, <index>, <value> ) </pre> === Description === Get or set a char from/to a string at a specific index. === Parameters === ==== expression ==== T...")
 
 
(9 intermediate revisions by one user not shown)
Line 17: Line 17:
 
Index of the string to get/set data to/from.
 
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.
+
If the index is a negative value the character position will work backwards from the length of the string.
 
+
So it will always do something.
+
  
 
==== value ====
 
==== value ====
Line 29: Line 27:
 
=== Return Value ===
 
=== Return Value ===
  
Success: Returns the char the given index IF only 2 params are used.
+
Success: Returns the char at the given index IF only 2 params are used.
Success: Returns nothing IF 3 params are used.
+
 
 +
Success: Returns TRUE if 3 params are used.
 +
 
 +
Failure: Returns NULL
 +
 
 +
=== Remarks ===
 +
 
 +
When setting a char at a given Index it modifies the string in place and does not create a new one.
 +
 
 +
Sputnik 0.10 added ability to set chars directly using the [] on strings.
  
 
=== Example ===
 
=== Example ===
Line 43: Line 50:
 
StrIndex($a, 1, "C"); // Actually changes the char in the string
 
StrIndex($a, 1, "C"); // Actually changes the char in the string
 
StrIndex($a, 2, "a"); // 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
+
StrIndex($a, 3, "t"); // Actually changes the char in the string
 
// Note - StrIndex with 3 parms does not return a value
 
// Note - StrIndex with 3 parms does not return a value
  
 
println($a); // Prints HCato
 
println($a); // Prints HCato
println($b); // Does not print anything
 
  
 
// Print all chars
 
// Print all chars
Line 55: Line 61:
 
// StrIndex with 2 params only READS the strings chars
 
// StrIndex with 2 params only READS the strings chars
 
// It does not modify them
 
// It does not modify them
}$result = isEmpty("");
+
}
Println("Result:" , $result);
+
 
 +
 
 +
// Print all chars using the []
 +
// The $a[$i] works to GET chars
 +
for($i = 0; $i < StrLen($a); $i++)
 +
{
 +
println(   $a[$i]  );
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Will return 0;
+
Example of directly using the [] instead of StrIndex
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
$result = isEmpty("The fuzzy blue sweaters...");
+
// Set a string
Println("Result:" , $result);
+
$a = "Hello";
 +
 
 +
$a[1] = "C"; // Actually changes the char in the string
 +
$a[2] = "a"; // Actually changes the char in the string
 +
$a[3] = "t"; // Actually changes the char in the string
 +
 
 +
println($a); // Prints HCato
 +
 
 +
 
 +
// Print all chars using the []
 +
// The $a[$i] works to GET chars too
 +
for($i = 0; $i < StrLen($a); $i++)
 +
{
 +
println(  $a[$i]  );
 +
}
 +
</syntaxhighlight>
 +
 
 +
Using a negative index
 +
 
 +
<syntaxhighlight lang="sputnik">
 +
// 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
 +
StrIndex($a, 3, "t"); // Actually changes the char in the string
 +
// Note - StrIndex with 3 parms does not return a value
 +
 +
println($a); // Prints HeatC
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Latest revision as of 09:49, 19 September 2015

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 the index is a negative value the character position will work backwards from the length of the string.

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 at the given index IF only 2 params are used.

Success: Returns TRUE if 3 params are used.

Failure: Returns NULL

Remarks

When setting a char at a given Index it modifies the string in place and does not create a new one.

Sputnik 0.10 added ability to set chars directly using the [] on strings.

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
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
 
// 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
for($i = 0; $i < StrLen($a); $i++)
{
	println(   $a[$i]  );
}

Example of directly using the [] instead of StrIndex

// Set a string
$a = "Hello";
 
$a[1] = "C"; // Actually changes the char in the string
$a[2] = "a"; // Actually changes the char in the string
$a[3] = "t"; // Actually changes the char in the string
 
println($a); // Prints HCato
 
 
// Print all chars using the []
// The $a[$i] works to GET chars too
for($i = 0; $i < StrLen($a); $i++)
{
	println(   $a[$i]  );
}

Using a negative index

// 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
StrIndex($a, 3, "t"); // Actually changes the char in the string
// Note - StrIndex with 3 parms does not return a value
 
println($a); // Prints HeatC
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox