Skip to content

Commit ce1b124

Browse files
committed
Add String.starts_with? and String.ends_with?
1 parent e08bcbe commit ce1b124

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# v0.9.1.dev
22

33
* enhancements
4+
* [Kernel] Add `String.start_with?` and `String.end_with?`
45

56
* bug fix
67
* [Dict] `Enum.to_list` and `Dict.to_list` now return the same results for dicts

lib/elixir/lib/string.ex

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,4 +795,50 @@ defmodule String do
795795
end
796796
end
797797

798+
@doc """
799+
Returns true if `string` starts with any of the prefixes given, otherwise
800+
false. `prefixes` can be either a single prefix or a list of prefixes.
801+
802+
## Examples
803+
804+
iex> String.starts_with? "elixir", "eli"
805+
true
806+
iex> String.starts_with? "elixir", ["erlang", "elixir"]
807+
true
808+
iex> String.starts_with? "elixir", ["erlang", "ruby"]
809+
false
810+
811+
"""
812+
@spec starts_with?(t, t | [t]) :: boolean
813+
814+
def starts_with?(string, prefixes) do
815+
case :binary.match(string, prefixes) do
816+
:nomatch -> false
817+
{0, _} -> true
818+
_ -> false
819+
end
820+
end
821+
822+
@doc """
823+
Returns true if `string` ends with any of the suffixes given, otherwise
824+
false. `suffixes` can be either a single suffix or a list of suffixes.
825+
826+
## Examples
827+
828+
iex> String.ends_with? "language", "age"
829+
true
830+
iex> String.ends_with? "language", ["youth", "age"]
831+
true
832+
iex> String.ends_with? "language", ["youth", "elixir"]
833+
false
834+
835+
"""
836+
@spec ends_with?(t, t | [t]) :: boolean
837+
838+
def ends_with?(string, suffixes) do
839+
string_size = size(string)
840+
matches = :binary.matches(string, suffixes)
841+
Enum.any? matches, fn({pos, len})-> pos + len == string_size end
842+
end
843+
798844
end

lib/elixir/test/elixir/string_test.exs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,5 +280,26 @@ defmodule StringTest do
280280
assert String.to_float("pi") === :error
281281
end
282282

283-
283+
test :starts_with? do
284+
assert String.starts_with? "hello", "he"
285+
assert String.starts_with? "hello", "hello"
286+
assert String.starts_with? "hello", ["hellö", "hell"]
287+
assert String.starts_with? "エリクシア", "エリ"
288+
refute String.starts_with? "hello", "lo"
289+
refute String.starts_with? "hello", "hellö"
290+
refute String.starts_with? "hello", ["hellö", "goodbye"]
291+
refute String.starts_with? "エリクシア", "仙丹"
292+
end
293+
294+
test :ends_with? do
295+
assert String.ends_with? "hello", "lo"
296+
assert String.ends_with? "hello", "hello"
297+
assert String.ends_with? "hello", ["hellö", "lo"]
298+
assert String.ends_with? "エリクシア", "シア"
299+
refute String.ends_with? "hello", "he"
300+
refute String.ends_with? "hello", "hellö"
301+
refute String.ends_with? "hello", ["hel", "goodbye"]
302+
refute String.ends_with? "エリクシア", "仙丹"
303+
end
304+
284305
end

0 commit comments

Comments
 (0)