stdlib icon indicating copy to clipboard operation
stdlib copied to clipboard

Add `string.strip_prefix` and `string.strip_suffix`

Open BrewingWeasel opened this issue 1 year ago • 2 comments
trafficstars

These functions would return an Ok value with the prefix or suffix stripped, or an Error(Nil) value when the prefix or suffix does not exist in the string.

case string.strip_prefix("https://gleam.run", "https://") {
  Ok(site) -> "found site " <> site
  Error(Nil) -> "no site found" 
}
// -> "found site gleam.run"

(I know this can be done with adding strings in pattern matching, but this would be useful in pipes and there is no way to handle strip_suffix with that approach)

You can already achieve this through combining string.starts_with and string.drop_start, but that's rather clunky and this feels much more gleam-y to me.

I find myself using them a decent amount when I write rust, and I noticed they're even used several times in the gleam compiler!

BrewingWeasel avatar Nov 17 '24 20:11 BrewingWeasel