Difference between revisions of "SHIP:Sail:subStr"

From Serious Documentation
Jump to: navigation, search
(Created page with "__NOTOC__ SHIP Sail Home SHIP Sail Function Home == subStr == Returns the N-character substring of a string starting at an index == Pro...")
 
(Examples)
Line 58: Line 58:
 
|-
 
|-
 
|<code>subStr ("hello", 3, 0);</code> || "" || zero length always returns an empty string
 
|<code>subStr ("hello", 3, 0);</code> || "" || zero length always returns an empty string
|-
 
|<code>length("");</code> || 0 ||
 
|-
 
|<code>length("&trade;");|| 1 || &trade; is three bytes in [[UTF-8]] but is only one character
 
 
|}
 
|}

Revision as of 10:38, 6 December 2012

SHIP Sail Home

SHIP Sail Function Home

subStr

Returns the N-character substring of a string starting at an index

Prototype

String subStr(String s, Integer index);

String subStr(String s, Integer index, Integer length);

Parameters/Return Value

Parameter Data Type Description
s String Original string
index Integer Starting index (0 is the first character in a string)
length Integer number of characters to get
Return String the substring requested

Detailed Description

The subStr () function returns a number characters from within in a string, starting at a specified offset.

The resulting string will be empty (zero length) if any of the following conditions are true:

  • the original string has no length
  • the index specified is beyond the end of the original string
  • the length parameter is zero

If the index is negative, an offset of 0 will be used.

If the length is negative or unspecified, all characters in the original string from the index onwards will be returned.

Examples

Example Result Notes
subStr ("hello", 1); "ello" unspecified length means return the rest of the string
subStr ("hello", 0, 1); "h"
subStr ("hello", 1, -1); "ello" -1 for length returns the rest of the string
subStr ("hello", 10, 2); "" index is beyond the length of the original string
subStr ("hello", -1, 2); "he" index negative is the same as index 0
subStr ("hello", 3, 0); "" zero length always returns an empty string