如何向OCaml中的字符串追加一个字符? [英] How do you append a char to a string in OCaml?

查看:174
本文介绍了如何向OCaml中的字符串追加一个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎在 char - >类型的标准库中没有函数。 string - > string ,它在字符串的前面(或结尾)插入 char >。有一些解决方法,例如。通过使用 String.make String.blit

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) a string. There are workarounds, e.g. by using String.make or String.blit. Is there an elegant way to do this?

推荐答案

String.make String.blit 是一个很好的方法,但它们似乎是必要的。我个人更喜欢使用 Char.escaped 和字符串连接制作中缀函数:

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 *)

这篇关于如何向OCaml中的字符串追加一个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆