Working with Text
7 min · OCaml
Join strings, measure them, and print text with print_endline.
Enemy 1 of 5
Joining strings with ^
OCaml uses `^` to join (concatenate) two strings — a dedicated operator, distinct from the numeric `+`, following the same philosophy you saw with `+` vs `+.` for floats: OCaml prefers a separate, unambiguous operator for each type rather than overloading one symbol to mean different things depending on context. `greeting ^ name` builds one new string out of the two, in order.
Because `^` only works on strings, trying to concatenate a string with a number directly (`"Score: " ^ 10`) is a compile error — the number first needs converting to a string with `string_of_int` (covered more in a later lesson). This is the same "no silent conversions" philosophy as the integer/float split: OCaml would rather flag the mismatch than guess.
Loading editor...