ocaml - 如何在OCaml中获取格式化的字符串?

标签 ocaml

在OCaml中,我可以使用Printf.printf输出格式化的字符串,例如

Printf.printf "Hello %s %d\n" world 123


但是,printf是一种输出。



我想要的不是输出,而是字符串。例如我想要

let s = something "Hello %s %d\n" "world" 123


然后我可以得到s = "Hello World 123"



我怎样才能做到这一点?

最佳答案

您可以使用Printf.sprintf:

# Printf.sprintf "Hello %s %d\n" "world" 123;;
- : string = "Hello world 123\n"

关于ocaml - 如何在OCaml中获取格式化的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17122850/

相关文章:

ocaml - 绘图自动机的工具

algorithm - 将此算法转换为不使用递归的过程

regex - (ReasonML) 正则表达式\b 不匹配

bit-manipulation - OCaml 中高效的 8 位和 16 位计算

ocaml - 如何将构造函数作为参数传递,然后对其进行模式匹配?

string - OCaml 测试字符串是否几乎为空或包含关键字

ocaml - Ocaml 中函数之间的循环依赖

ocaml - Jason Hickey 的 Ocaml 书有公开答案吗?

types - 使用具有高阶函数的 GADT

c++ - 如何将 ocaml 代码编译为 native 以供 C++ 使用