elixir-lang/elixir — pull request #1113 by eproxus (2013-05-24 23:53:45+00:00)
Title: Add String.start_with? and String.end_with?

Adds two new functions to the `String` module: `start_with?` and `end_with?`.

## start_with?

Returns true if `string` starts with any of the prefixes given, otherwise false. `prefixes` can be either a single prefix or a list of prefixes.

### Examples

iex> String.start_with? "elixir", "eli"
true
iex> String.start_with? "elixir", ["erlang", "elixir"]
true
iex> String.start_with? "elixir", ["erlang", "ruby"]
false

## end_with?

Returns true if `string` ends with any of the suffixes given, otherwise false. `suffixes` can be either a single suffix or a list of suffixes.

### Examples

iex> String.end_with? "language", "age"
true
iex)> String.end_with? "language", ["youth", "age"]
true
iex)> String.end_with? "language", ["youth", "elixir"]
false

## Notes

* I'm open to suggestions on names. For example, I always tend to guess "starts_with?" and "ends_with?" in new languages. In this case, I chose to go with the Ruby naming.
* Would it be possible to implement these functions on the level of `Enumerable`? It would be cool to be able to run them on any collection (although it is not the first place you would look for such functions).