Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a list of characters (or array) to a string

Tags:

how does one convert from a list of characters to a string?

To put it another way, how do I reverse List.ofSeq "abcd"?

UPDATE: new System.String (List.ofSeq "abcd" |> List.toArray) |> printfn "%A" seems to work fine, with or without new, but List.ofSeq "abcd" |> List.toArray) |> new System.String |> printfn "%A" fails. Why?

like image 712
Miron Brezuleanu Avatar asked Feb 28 '10 17:02

Miron Brezuleanu


People also ask

Can character array be converted to string?

Use the valueOf() method in Java to copy char array to string. You can also use the copyValueOf() method, which represents the character sequence in the array specified. Here, you can specify the part of array to be copied.


1 Answers

I asked a similar question once before. It seems object constructors aren't composable so you can't pass them as a function.

List.ofSeq "abcd" |> List.toArray |> (fun s -> System.String s) |> printfn "%A" List.ofSeq "abcd" |> List.toArray |> (fun s -> new System.String(s)) |> printfn "%A" 

Update Constructors are first-class functions as of F# 4.0

List.ofSeq "abcd" |> List.toArray |> System.String |> printfn "%A" 
like image 154
gradbot Avatar answered Oct 21 '22 23:10

gradbot