Elixir - find sub-bitstring within larger bitstring

久未见 提交于 2021-01-28 20:34:05

问题


How would I go about finding if a sub-bitstring is present with another bitstring in Elixir? I need to verify that bitstring X exists somewhere within bitstring Y for testing purposes. Is there a simple way to do this with existing functions?

x = "bar"
y = "foo bar baz"
some_substring_function(x, y)

with some_substring_function returning a truthy value.

Thanks in advance!


回答1:


You can use the =~ operator:

iex> "hello world" =~ "hello"
true

The String module also has some convenience functions.



来源:https://stackoverflow.com/questions/19636547/elixir-find-sub-bitstring-within-larger-bitstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!