Core Function SubStr

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(count)
Line 1: Line 1:
 
<pre>
 
<pre>
SubStr( <expression>, <start>, <end> )
+
SubStr( <expression>, <start>, <end>, <replacement> )
 
</pre>
 
</pre>
  
Line 26: Line 26:
  
 
If count is given and is negative, then that many characters will be omitted from the end of string (after the start position has been calculated when a start is negative). If start denotes the position of this truncation or beyond, empty string will be returned.
 
If count is given and is negative, then that many characters will be omitted from the end of string (after the start position has been calculated when a start is negative). If start denotes the position of this truncation or beyond, empty string will be returned.
 +
 +
==== replacement ====
 +
 +
Optional; If you set this parameter the substring will be replaced instead of returned so you will receive a new string with the substring replaced with the replacement text.
  
 
=== Return Value ===
 
=== Return Value ===
Line 45: Line 49:
  
 
Example using Start as a negative number
 
Example using Start as a negative number
 +
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
 
say substr("UberFoX", -3); // Prints FoX
 
say substr("UberFoX", -3); // Prints FoX
 
say substr("UberFoX", -4, 2); // Prints rF
 
say substr("UberFoX", -4, 2); // Prints rF
 +
</syntaxhighlight>
 +
 +
Using replacement
 +
 +
<syntaxhighlight lang="sputnik">
 +
$str = "UberCat!";
 +
print substr($str, 4, 3, "FoX");
 +
# Prints
 +
# UberFoX!
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Revision as of 22:56, 28 September 2013

SubStr( <expression>, <start>, <end>, <replacement> )

Contents

Description

Return part of a string

Parameters

expression

The expression to evaluate.

start

The character position to start. (0 = first character)

OR

If the start is a negative value the character position will work backwards from the length of the string.

count

Optional; The number of characters to extract. By default the entire remainder of the string.

If count is given and is negative, then that many characters will be omitted from the end of string (after the start position has been calculated when a start is negative). If start denotes the position of this truncation or beyond, empty string will be returned.

replacement

Optional; If you set this parameter the substring will be replaced instead of returned so you will receive a new string with the substring replaced with the replacement text.

Return Value

Success: Returns the extracted string.

Failure: Returns an empty string.

Remarks

None.

Example

$var = Substr("I am a string", 2, 2);
MsgBox("2 chars extracted from position 2 are: $var");

Example using Start as a negative number

say substr("UberFoX", -3); // Prints FoX
say substr("UberFoX", -4, 2); // Prints rF

Using replacement

$str = "UberCat!";
print substr($str, 4, 3, "FoX");
# Prints
# UberFoX!
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox