我如何连接last_name
和honorific_prefix
并添加full_name
,如果它defmodule MyApp.UserView do
use MyApp.Web, :view
alias MyApp.User
def full_name(%User{first_name: first_name,
last_name: last_name,
honorific_prefix: honorific_prefix}) do
first_name
|> String.upcase
end
end
中没有空?
<div>
Search by name:
<input type="text" name="txtSearch" />
@Html.ActionLink("Search", "SearchName")
</div>
答案 0 :(得分:2)
您可以将这些元素视为集合,拒绝空元素并加入它们:
[honorific_prefix, first_name, last_name]
|> Enum.reject(&(&1 == ""))
|> Enum.join(" ")
答案 1 :(得分:1)
我只使用if
和<>
:
def full_name(%User{first_name: first_name,
last_name: last_name,
honorific_prefix: honorific_prefix}) do
if honorific_prefix == "" do
""
else
honorific_prefix <> " "
end <> first_name <> " " <> last_name
end