Adds C# Raw String Literal style functionality to multiline strings.
$ dotnet add package TSBSoftware.TextBlockMakes F# multiline strings work like C# Raw String Literals, or Java Text Blocks.
This project was inspired by an article related to Java Text Blocks. This functionality felt like a missing feature in the F# language, and I wanted to make it available to everyone.
F# developers who would like to utilize multiline strings to format content, such as: html, xml, sql, markdown, templates, etc.
TextBlock provides a robust set of tools to simplify working with multiline strings in F#:
\) at the end of a line to suppress newlines, allowing long content to be split across lines in source code without affecting the output.|) marker at the end of a line, ideal for maintaining intentional spacing in templates or formatted text.Environment.NewLine), ensuring accurate rendering of dynamic content like multi-line SQL queries or email templates.Environment.NewLine for line endings, ensuring consistent behavior across Windows (\r\n) and other platforms (\n).| Feature | F# Multiline Strings | TextBlock | C# Raw String Literals |
|---|
| Java Text Blocks |
|---|
| Auto-indent stripping | No | Yes | Yes | Yes |
| Newline escaping | No | Yes | Yes | Yes |
| Trailing space control | No | Yes | Partial | Yes |
To start using TextBlock, install the NuGet package into your project file.
dotnet add package TSBSoftware.TextBlockOpen the TextBlock namespace to use the extension method.
open TextBlock
let myText =
"""
<div>
<p>Hello</p>
</div>
"""
.TextBlock()This will produce the following string:
<div>
<p>Hello</p>
</div>Without using TextBlock, there would be unwanted leading indentation because of how F# processes multiline strings:
<div>
<p>Hello</p>
</div>Strings can contain embedded newline characters.
let embeddedNewlines =
let nl = System.Environment.NewLine
$"""
This content has {nl}several lines
in {nl}the text.
"""
.TextBlock()This will produce
This content has
several lines
in
the text.Newlines can be escaped with a backslash.
let myText =
"""
Hello \
World!
"""
.TextBlock()This removes the newline character and combines the text into a single line:
Hello World!Spaces at the end of each line can be preserved.
let blockedLines =
"""
BlockLine |
Text |
"""
.TextBlock()This will produce the following. Trailing spaces are shown as dots for clarity.
BlockLine���
Text��������You can apply additional indentation with a specified level and an optional character (default is a space).
let someHtml =
"""
<div>
<p>Hello</p>
</div>
"""
.TextBlock(indent = 4, indentChar = '�')This will produce the following:
����<div>
���� <p>Hello</p>
����</div>Repeated Application: TextBlock is designed for one-shot processing of multiline strings. Applying it multiple times (e.g., myText.TextBlock().TextBlock()) is not recommended, as it may alter indentation or formatting unexpectedly.
Platform Newlines: Output uses Environment.NewLine, which may produce \r\n on Windows or \n on other platforms, ensuring compatibility with your target systems.
The source code and tests are available on GitHub.