如何在Sprintf中使用变量作为格式字符串? [英] How Do I Use A Variable As The Formatting String With Sprintf?

查看:245
本文介绍了如何在Sprintf中使用变量作为格式字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不得不问这个问题,真是个菜鸟,但这让我很沮丧.

I feel like a total noob for having to ask this but it's got me stumped.

我设置了这样的格式字符串:

I set a format string like this:

let fs = "This is my format test %s"

然后我尝试像这样使用它:

Then I attempt to use it like so:

let s = sprintf fs "testing"

这样做时,我会收到此错误:

When I do so I get this error:

//stdin(26,17): error FS0001: The type 'string' is not compatible with the type 'Printf.StringFormat<('a -> 'b)>'

所以我尝试了这个:

let s = sprintf (Printf.StringFormat fs) "test"

REPL回复的

:

to which the REPL responded:

//stdin(28,18): error FS1124: Multiple types exist called 'StringFormat', taking different numbers of generic parameters. Provide a type instantiation to disambiguate the type resolution, e.g. 'StringFormat<_>'.

所以我尝试了这个:

let s = sprintf (Printf.StringFormat<string> fs) "test" 

我明白了:

//stdin(29,18): error FS0001: The type ''a -> 'b' does not match the type 'string'

我缺少明显痛苦的东西吗?这是从Xamarin Studio F#交互式窗口在Mac上使用的F#3.0.

Am I missing something painfully obvious? This is using F# 3.0 on the Mac from the Xamarin Studio F# Interactive Window.

推荐答案

因此,您实际上需要创建一个具有以下功能类型的StringFormat

So you actually need to create a StringFormat which has a function type as follows

> sprintf (Printf.StringFormat<string->string>("Hello %s")) "World";;
  val it : string = "Hello World"

6.3.16 ,显示了一个示例.

这篇关于如何在Sprintf中使用变量作为格式字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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