Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add string.strip_prefix and string.strip_suffix #743

Open
BrewingWeasel opened this issue Nov 17, 2024 · 2 comments
Open

Add string.strip_prefix and string.strip_suffix #743

BrewingWeasel opened this issue Nov 17, 2024 · 2 comments
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@BrewingWeasel
Copy link

BrewingWeasel commented Nov 17, 2024

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!

@lpil
Copy link
Member

lpil commented Dec 17, 2024

Sounds good to me!

Do we want to consider any of the other designs for this? Elixir has replace functions instead I believe.

@silby
Copy link

silby commented Dec 27, 2024

Haskell text also has this API, it's good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants