开发者

How to work with string?

I have a string with emails (name, surname, email):

@emails = "Nina Beeu luda@hotmail.com, Vasilina Korute valaj@kos.co.uk, Hikote Ewefs valaj@kos.co.uk, 
Egert Erm papa@sasee.ee, Sambuka Ioas valaj@kprivet.com, Vanish Kiki sasa@sas.com, Inoke Xxx saop@hot.ee"

I need to substring from this string: name, surname and email and paste them into table:

<table border=1>
  <tr>
    <td>
        Name
    </td>
    <td>
        Surname
    </开发者_运维百科td>
    <td>
        Email
    </td>
  </tr>
</table>

How i can do it?


<table>
  <% @emails.split(", ").each do |chunk| %>
    <tr>
      <% ["Name", "Surname", "Email"].zip(chunk.split(" ")).each do |data| %>
        <td><%= data.join(": ")</td>
      <% end %>
    </tr>
  <% end %>
</table>


@emails.split(/,\s+/).each do |details|
  name, surname, email = details.split(" ")
  # do your html creaty thing here
end

More explicitly, you could do this in erb:

<table border=1>
  <% @emails.split(/,\s+/).each do |details| %>
    <% name, surname, email = details.split(/\s+/) %>
    <tr>
      <td><%= name %></td>
      <td><%= surname %></td>
      <td><%= email %></td>
    </tr>
  <% end %>
</table>

And a variant in haml:

%table(border=1)
  - @emails.split(/,\s+/).each do |details|
    %tr
      - details.split(/\s+/) do |detail|
        %td= detail
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜