substr - Pull a substring from a string
The substr
function takes a string and a position and, optionally, a length, and produces a substring from it. It is especially useful for simple value extraction that doesn't warrant regex capture groups.
Syntax
Arguments
Name | Type | Required | Description |
---|---|---|---|
value | string | true | The main string |
from | number | true | The index from which we should capture the substring |
length | number | false | The number of characters we should capture after from . Defaults to the remainder of the string. |
Example - Using substring to grab a value at the end of a string
Before we can decide how much of a string we want to pull out, we first need to work out our index. indexOf
is perfect for this. Consider this document:
We want to get just the value, so we can use indexOf
to work out the position of =
:
Now we have our index, we're able to take our substring:
This will pull everything from the position after the =
, to the end of the string, resulting in the following document:
Theme
Light