ddonche/goblin-lang
0.46.24
1
0
docs reference
[[before-last]]

Before_last


before_last returns the part of a string before the final occurrence of a separator.


Overview

Use before_last when a separator appears more than once and you want everything before the final one:

path | "maps/caves/entrance.gbln"
directory | path.before_last("/")

:say(directory)  /// maps/caves
path | "maps/caves/entrance.gbln"
directory | path.before_last("/")

:say(directory)  /// maps/caves

The separator itself is not included in the result.


Method Style

Call before_last as a method on the string you want to search:

route | "caves/upper/armory"
parent | route.before_last("/")

:say(parent)  /// caves/upper
route | "caves/upper/armory"
parent | route.before_last("/")

:say(parent)  /// caves/upper

The equivalent free-call form is:

parent | before_last("caves/upper/armory", "/")
parent | before_last("caves/upper/armory", "/")

Both forms behave the same way.


Final Occurrence

before_last searches from the end of the string:

route | "caves/upper/armory"

route.before_last("/")  /// caves/upper
route | "caves/upper/armory"

route.before_last("/")  /// caves/upper

Compare this with Before, which stops at the first occurrence:

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_last returns the original string:

before_last("entrance.gbln", "/")  /// entrance.gbln
before_last("entrance.gbln", "/")  /// entrance.gbln


Empty Separator

An empty separator is found at the end of the string, so before_last returns the whole string:

before_last("adventure", "")  /// adventure
before_last("adventure", "")  /// adventure


Signature

string.before_last(separator)
before_last(string, separator)
string.before_last(separator)
before_last(string, separator)

Argument Type Description
string string The text to search.
separator string The marker whose final occurrence ends the returned part.

before_last returns a string.


Errors

before_last requires a string and a separator:

before_last("maps/caves/entrance.gbln")
/// error: R0301 wrong-arity
before_last("maps/caves/entrance.gbln")
/// error: R0301 wrong-arity

Both values must be strings:

before_last("maps/caves/entrance.gbln", 2)
/// error: T0205 type-mismatch
before_last("maps/caves/entrance.gbln", 2)
/// error: T0205 type-mismatch