Dot-Net

如何在 F# 中編寫內聯大字元串

  • August 1, 2012

在 C# 中,我可以使用:

string myBigString = @"

<someXmlForInstance>
 <someChild />
</someXmlForInstance>

";

如何在 F# 中執行此操作?

在 F# 3.0 VS 2012 中,添加了對三引號字元串的支持。

在三引號字元串中,三引號 ("""…""") 之間的所有內容都保持逐字記錄;根本沒有逃避。因此,如果我想將一些 XAML 作為字元串文字,這很容易:

let xaml = """ 
<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
           Name="mainPanel"> 
 <Border BorderThickness="15.0" BorderBrush="Black"> 
   <StackPanel Name="stackPanel1"> 
     <TextBlock Text="Super BreakAway!" FontSize="24" HorizontalAlignment="Center" /> 
     <TextBlock Text="written in F#, by Brian McNamara - press 'p' to pause" 
                FontSize="12" HorizontalAlignment="Center" /> 
     <Border BorderThickness="2.0" BorderBrush="Black"> 
       <Canvas Name="canvas" Background="White" /> 
     </Border> 
   </StackPanel> 
 </Border> 
</StackPanel>"""

引用自:https://stackoverflow.com/questions/11757699