【问题标题】:Embedded Strings in Interpolated Strings in F#F# 中插值字符串中的嵌入字符串
【发布时间】:2021-04-23 09:46:22
【问题描述】:

我似乎无法弄清楚如何将字符串嵌入到 F# 中的插值字符串中。从表面上看,它与 C# 非常相似。例如,在 C# 中,我会写类似

Console.WriteLine($"Truly, it is {(string.IsNullOrEmpty("") ? "" : "not ")}empty.");

//Result: Truly, it is empty.   OR   Truly, it is not empty.

大括号内的括号允许我在插值表达式中嵌入额外的字符串文字。

我已经尝试在 F# 中复制这个

printfn $"Truly it is {(match (String.IsNullOrEmpty "l") with | true -> "" | false -> "not ")}empty."

包裹内插表达式的括号似乎并没有完成同样的事情。它建议对插值匹配表达式使用显式 let 绑定,但出于学习目的,我想在一行中完成。

有没有办法在 F# 中做到这一点,还是我坚持为嵌入式三元定义 let

【问题讨论】:

  • 附带说明:F# 中的惯用方式是使用 if then else 而不是在布尔值上匹配。
  • 谢谢。我刚刚开始 F#,但我的腿还在继续。

标签: f# string-interpolation


【解决方案1】:

使用三引号字符串:

printfn $"""Truly it is {match (String.IsNullOrEmpty "l") with | true -> "" | false -> "not "}empty."""

【讨论】:

  • 太棒了!谢谢!
猜你喜欢
  • 2020-05-24
  • 1970-01-01
  • 1970-01-01
  • 2020-02-02
  • 2017-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多