String.strip...">

How to remove new string characters from a string?

I want to delete all newlines:

aaa = """ fdsfds fdsfds fdsfdsfds fdsfdsfds """ |> String.strip("\r\n") 

And I get:

 argument error 

What happened to this?

+8
source share
2 answers

What happened to this?

String.strip only supports one character. This error occurs when Elixir tries to convert "\r\n" to a single character ( source ):

 iex(1)> s = "\r\n" "\r\n" iex(2)> <<s::utf8>> ** (ArgumentError) argument error 

Additionally, String.strip deprecated in favor of String.trim , which supports the string as the second argument, but this function will only remove the exact sequence \r\n from the beginning and end of the string:

 iex(1)> aaa = """ ...(1)> fdsfds fdsfds ...(1)> fdsfdsfds ...(1)> fdsfdsfds ...(1)> """ "fdsfds fdsfds\n fdsfdsfds\nfdsfdsfds\n" iex(2)> String.trim(aaa, "\r\n") "fdsfds fdsfds\n fdsfdsfds\nfdsfdsfds\n" iex(3)> String.trim(aaa, "\r\n") == aaa true 

which I doubt is what you want as you said: "I want to delete all newline characters." To remove all \r and \n , you can use String.replace twice:

 iex(4)> aaa |> String.replace("\r", "") |> String.replace("\n", "") "fdsfds fdsfds fdsfdsfdsfdsfdsfds" 
+10
source

New line escape

 """ fdsfds fdsfds \ fdsfdsfds \ fdsfdsfds """ 
0
source

Source: https://habr.com/ru/post/1011680/


All Articles