如何提高"deleting all 'newline' except last one in one string"的代码效率?

how to improve the code efficiency of "deleting all 'newline' except last one in one string"?

我想通过清除每条日志消息中的所有换行符来过滤日志数据。下面是我的代码,但是好像效率不高,如何改进?

character_drop_test_b()->
    List = "AB\nC\nD\n",
    Result = re:replace(List, "[\n]", "", [global, {return, list}]) ++ "\n",
    Result.

由于您要替换的是固定字符串而不是模式,因此您不需要使用正则表达式。试试这个:

string:join(string:tokens(List, "\n"), "") ++ "\n"

根据我的测量,它比您在小型 List 上的方法快 3 倍,比您在由 1000 个 List 数据副本组成的列表上的方法快 6 倍。