Difference between revisions of "SHIP:Sail:subString"

From Serious Documentation
Jump to: navigation, search
(Detailed Description)
(Detailed Description)
Line 31: Line 31:
  
 
== Detailed Description ==
 
== Detailed Description ==
The {{SailFunc|subString}} function returns a number characters from within in a string, starting at a specified offset and ending at a specified offset.
+
The {{SailFunc|subString}} function is aligned with the [http://www.w3schools.com/jsref/jsref_substring.asp JavaScript subString], and returns a number characters from within in a string, starting at a specified offset and ending at a specified offset, not including the ending character.  The total number of characters is no more than (end - begin).
  
Both the character at the starting and ending offset will be included in the result.
+
If the end is specified less than the start, the arguments are swapped.
  
 
The resulting string will be empty (zero length) if any of the following conditions are true:
 
The resulting string will be empty (zero length) if any of the following conditions are true:
 
* the original string has no length
 
* the original string has no length
* the start index specified is beyond the end of the original string
+
* the start index is equal to the end index
* the end index is less than the start index
 
 
 
If the start or end index is negative, a start and end index of will be used respectively.
 
  
 
If the end index is unspecified, all characters in the original string from the start index onwards will be returned; this form is identical in operation to {{SailFunc|subStr}}<code>(start);</code>.
 
If the end index is unspecified, all characters in the original string from the start index onwards will be returned; this form is identical in operation to {{SailFunc|subStr}}<code>(start);</code>.

Revision as of 04:37, 18 June 2015

Function Returns Introduced Description
subString String v2.0 Returns the substring of a string starting at an index and (optionally) ending at an index.

See Also:

Prototype

String subString(String s, Integer start);

String subString(String s, Integer start, Integer end);

Parameters/Return Value

Parameter Data Type Description
s String Original string
start Integer Starting index (0 is the first character in a string)
end Integer Ending index (exclusive)
Return String the substring requested

Detailed Description

The subString function is aligned with the JavaScript subString, and returns a number characters from within in a string, starting at a specified offset and ending at a specified offset, not including the ending character. The total number of characters is no more than (end - begin).

If the end is specified less than the start, the arguments are swapped.

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

  • the original string has no length
  • the start index is equal to the end index

If the end index is unspecified, all characters in the original string from the start index onwards will be returned; this form is identical in operation to subStr(start);.

Examples

Example Result Notes
subString("hello", 1); "ello" unspecified length means return the rest of the string
subString("hello", 0, 1); "h"
subString("hello", 0, 0); "h"
subString("hello", 1, -1); "" end index is less than start index
subString("hello", 10, 2); "" start index is beyond the length of the original string
subString("hello", -1, 1); "h" start index negative is the same as start index 0