polerto
polerto

Reputation: 1790

Concatenate string to integer in Ocaml

I would like a working version of this:

let x = "a" ^ 0;;

Upvotes: 10

Views: 8119

Answers (2)

Thomas
Thomas

Reputation: 5048

You can also use the usual printf functions but it is much slower:

let x = Printf.sprintf "a%d" my_integer

Upvotes: 6

sepp2k
sepp2k

Reputation: 370162

As you undoubtedly noticed, you can only concatenate strings with other strings - not integers. So you'll have to convert your integer to a string before you can concatenate it. If the integer is really hard coded like in your example, you can just write "0" instead of 0 (in fact in your example you can just write "a0" and not concatenate anything at all).

If the integer is not a constant, you can use string_of_int to convert it to a string. So this will work:

let x = "a" ^ string_of_int my_integer

Upvotes: 14

Related Questions