How do you append a char to a string in OCaml?

前端 未结 3 1093
伪装坚强ぢ
伪装坚强ぢ 2021-02-20 16:42

It seems as if there is no function in the standard library of type char -> string -> string, which insert a char in front of (or at the end of)

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-20 17:29

    String.make and String.blit is a good way to do so, but they seem to be imperative. Personally I prefer to make infix functions using Char.escaped and string concatenation:

    let (^$) c s = s ^ Char.escaped c (* append *)
    let ($^) c s = Char.escaped c ^ s (* prepend *)
    

提交回复
热议问题