2016-07-24 28 views
1

我試圖拼接位串二郎 - 如何連接位串

cowboy_req:reply(

       200, #{<<"content-type">> => <<"text/html">>}, 

       <<"<div style='color:#FF0'>">> ++ cowboy_req:host(Req) ++ <<"</div>">> , 

       Req 
    ) 

但它提供了運行時錯誤,因爲++運營商。我如何連接兩個位串?

+1

請看看下面的線程:http://stackoverflow.com/questions/10963359/concatenating-bitstrings-not-binaries-in-erlang – Yaron

+0

我看着那個問題,但不能低估和。有一些像這樣的數字''<<1,2>>',但我只是在'<< >>' – Paramore

+0

之間的字符串這個怎麼樣? http://stackoverflow.com/a/601482/1835621 – Yaron

回答

4

你在這裏有什麼是正常的二進制文件,而不是具體的比特串。

如果你真的想將它們連接起來,存儲cowboy_req:host(Req)在一個變量,然後串聯的3個二進制代碼:

Host = cowboy_req:host(Req), 
cowboy_req:reply(
    200, 
    #{<<"content-type">> => <<"text/html">>}, 
    <<"<div style='color:#FF0'>", Host/binary, "</div>">>, 
    Req 
) 

注意,因爲cowboy_req:reply接受iodata(),它通常是更有效的恢復像這樣的列表:

cowboy_req:reply(
    200, 
    #{<<"content-type">> => <<"text/html">>}, 
    [<<"<div style='color:#FF0'>">>, cowboy_req:host(Req), <<"</div>">>], 
    Req 
) 
+0

非常感謝。這解決了我的問題 – Paramore