F # trim null chars

I have a string obtained from a web socket:

let websocket = new ClientWebSocket()
let source = new CancellationTokenSource()
let buffer = ArraySegment(Array.zeroCreate 32)  

do! websocket.ConnectAsync(url, source.Token) 
do! websocket.ReceiveAsync(buffer, source.Token) 

let str = Encoding.ASCII.GetString buffer.Array

let trim1 = Regex.replace str "\0" String.Empty  // result is empty string
let trim2 = Regex.replace str "\\0" String.Empty // result is empty string
let trim3 = str.TrimEnd [| '\\'; '0' |]          // result is untouched

I'm obviously trying to trim extra null characters

In the debugger, the value for str is "{\" type \ ": \" hello \ "} \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 "

When printed, it looks like this: {"type": "hello"}, which makes sense, the characters are interpreted properly.

I can't seem to complete this simple task in f #, what am I doing wrong?

+4
source share
1 answer

Using ASCII escaping will do the trick:

let trim3 = str.TrimEnd [| '\x00' |]

You can also avoid this with the unicode escape sequence:

let trim3 = str.TrimEnd [| '\u0000' |]  

, - ASCII- "\x00" unicode "\u0000".

+6

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


All Articles