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

前端 未结 3 1157
伪装坚强ぢ
伪装坚强ぢ 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-20 17:10

    The code from @pad is what I would use, because I like to treat strings as immutable if possible. But I wouldn't use Char.escaped; it's specialized for when you want the OCaml lexical representation of a character. So here's what you get if you make that change:

    let prefix_char s c = String.make 1 c ^ s
    
    let suffix_char s c = s ^ String.make 1 c
    

    Update

    In the years since this question was asked, OCaml has changed so that strings are immutable. Excellent.

提交回复
热议问题