Skip to main content

Razor Blade Tutorials

Tutorial HomeRazor.Blade

RazorBlade Text.Zip(...) v1.1

These demos show how to Zip (clean/compress) text properly, because Razor Blade handles a lot of issues you'll usually have cleaning up text. Scenarios might be:

  • Multi-line text
  • Cleane-up html, which may have more spaces and line-breaks than expected
  • Just any text pasted from somewhere, which could even contain surprise white-space

RazorBlade API Docs

Example Text

The example texts which have invisible problem characters:
  1. This        contains multi-spaces and ↦↦↦ tabs
  2. This has ↵  ↵ line-breaks

Correct and incorrect Character Counts

Html output hides the problems Whitespace output showing problems Length Output using Text.Zip(...) Zip Length
This contains multi-spaces and tabs This contains multi-spaces and tabs 46 This contains multi-spaces and tabs 35
This has line-breaks This has line-breaks 25 This has line-breaks 20

Source Code of this file

Below you'll see the source code of the file. Note that we're just showing the main part, and hiding some parts of the file which are not relevant for understanding the essentials.

@inherits ToSic.Sxc.Dnn.RazorComponent
<!-- unimportant stuff, hidden -->
@using ToSic.Razor.Blade;
@{
  var samples = new string[] {
    "This        contains multi-spaces and \t\t\t tabs",
    "This has \n  \n line-breaks"
  };
}
RazorBlade Text.Zip(...) v1.1 These... <!-- unimportant stuff, hidden -->

<h2>Correct and incorrect Character Counts</h2>

<table class="table table-hover" width="100%">
    <!-- table header -->
    <tr>
        <th>Html output hides the problems</th>
        <th class="table-warning">Whitespace output showing problems</th>
        <th>Length</th>
        <th class="table-success">Output using Text.Zip(...)</th>
        <th>Zip Length</th>
    </tr>
    <!-- the real code -->
    @foreach(var s in samples) {
      <tr>
        <td>@s </td>
        <td  class="table-warning" style="white-space: pre-wrap;">@s</td>
        <td>@s.Length </td>
        <td class="table-success" style="white-space: pre-wrap;">@Text.Zip(s)</td>
        <td>@Text.Zip(s).Length</td>
      </tr>
    }
</table>

<!-- unimportant stuff, hidden -->