Before
before returns the part of a string before the first occurrence of a separator.
Overview
Use before when you want the text that appears before a marker:
filename | "adventure.gbln" name | filename.before(".") :say(name) /// adventure
filename | "adventure.gbln" name | filename.before(".") :say(name) /// adventure
The separator itself is not included in the result.
Method Style
Call before as a method on the string you want to search:
location | "caves/armory/chest" first_area | location.before("/") :say(first_area) /// caves
location | "caves/armory/chest" first_area | location.before("/") :say(first_area) /// caves
The equivalent free-call form is:
first_area | before("caves/armory/chest", "/")
first_area | before("caves/armory/chest", "/")
Both forms behave the same way.
First Occurrence
before stops at the first occurrence of the separator:
route | "caves/upper/armory" route.before("/") /// caves
route | "caves/upper/armory" route.before("/") /// caves
Use Before Last when you want everything before the final occurrence instead:
before(route, "/") /// caves before_last(route, "/") /// caves/upper
before(route, "/") /// caves before_last(route, "/") /// caves/upper
Missing Separator
If the separator does not appear, before returns the original string:
before("entrance.gbln", "/") /// entrance.gbln
before("entrance.gbln", "/") /// entrance.gbln
This means the result is never empty merely because the separator was not found.
Empty Separator
An empty separator is found at the beginning of every string, so the result is an empty string:
before("adventure", "") /// ""
before("adventure", "") /// ""
Signature
string.before(separator) before(string, separator)
string.before(separator) before(string, separator)
| Argument | Type | Description |
|---|---|---|
string |
string | The text to search. |
separator |
string | The marker that ends the returned part. |
before returns a string.
Errors
before requires a string and a separator:
before("maps/caves/entrance.gbln") /// error: R0301 wrong-arity
before("maps/caves/entrance.gbln") /// error: R0301 wrong-arity
Both values must be strings:
before("maps/caves/entrance.gbln", 2) /// error: T0205 type-mismatch
before("maps/caves/entrance.gbln", 2) /// error: T0205 type-mismatch