Convert char to string in OCaml

I want to convert char to string, but I did not find the string_of_char function. I want to do this using only functions from Pervasives.

+7
string char type-conversion ocaml
source share
4 answers

If you use Core, you can write

 open Core.Std let s = Char.to_string 's' 
+6
source share

You can use String.make :)

 String.make 1 mychar 
+23
source share

Another one you can use:

 Char.escaped 'a' 
+1
source share

If you do not need any dependencies outside the standard library shipped with OCaml *, you can also convert the character to a string using the format string.

Create a 1 digit string from the character

 Printf.sprintf "%c" ch 

(note that capital C ) Create a string containing the OCaml notation for this character

 Printf.sprintf "%c" ch 

eg:

 # Printf.sprintf "%C" '\\';; - : string = "'\\\\'" # Printf.sprintf "%C" 'a';; - : string = "'a'" 

* Printf not part of Pervasives

+1
source share

All Articles