Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# basics: turning NameValueCollection into nice string

Learning F# while trying to do something useful at the same time, so this is kind of basic question:

I have req, which is a HttpListenerRequest, which has QueryString property, with type System.Collections.Specialized.NameValueCollection. So, for sake of clarity let's say, I have

let queryString = req.QueryString

Now I want to produce nice string (not printf to console) from contents of that, but queryString.ToString() is not overriden apparently, so it just gives string "System.Collections.Specialized.NameValueCollection".

So, what's the F# one-liner to get a nice string out of that, like "key1=value1\nkey2=value2\n..."?

like image 553
hyde Avatar asked Jan 16 '23 04:01

hyde


2 Answers

nvc.AllKeys
|> Seq.map (fun key -> sprintf "%s=%s" key nvc.[key])
|> String.concat "\n"
like image 140
Daniel Avatar answered Jan 27 '23 22:01

Daniel


Something like this ought to work:

let nvcToString (nvc:System.Collections.Specialized.NameValueCollection) =
    System.String.Join("\n", 
                       seq { for key in nvc -> sprintf "%s=%s" key nvc.[key] })
like image 23
kvb Avatar answered Jan 27 '23 21:01

kvb