Skip to main content

Razor Blade Tutorials

Tutorial HomeRazor.Blade

RazorBlade Text.Has(...) v1.1

These demos show how to really check if a variable has text using Text.Has. This combines checks for...

  • null
  • empty
  • only html-nbsp
  • only html character #160 (also nbsp)
  • only new-line

RazorBlade API Docs


Test Code Result ...when html counts
Null value Text.Has(null)
Just spaces Text.Has(" ")
text with only line breaks Text.Has("\n\n")
tabs, spaces and line breaks Text.Has("\n\t \n")
only nbsp characters Text.Has("   ") ✔️
char-code of nbsp characters Text.Has(" ") ✔️
real text Text.Has("real text") ✔️ ✔️
Real text with nbps etc. Text.Has("real\n text  ") ✔️ ✔️

Special case: <BR> Whitespace

  • If your string is like Text.Has("<br>") it will be: True
  • If you want to ignore BRs, combine it with Tags.Br2Nl(...)
  • ...resulting in: False

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
@using ToSic.Razor.Blade;
<!-- unimportant stuff, hidden -->
RazorBlade Text.Has(...) v1.1 These... <!-- unimportant stuff, hidden -->

<table class="demo table table-hover">
    <th>...when html counts</th>
  @RowEmojified("Null value", null)
  @RowEmojified("Just spaces", "     ")
  @RowEmojified("text with only line breaks", "\n\n")
  @RowEmojified("tabs, spaces and line breaks", "\n\t  \n")
  @RowEmojified("only nbsp characters", "&nbsp; &nbsp;")
  @RowEmojified("char-code of nbsp characters", "&#160;")
  @RowEmojified("real text", "real text")
  @RowEmojified("Real text with nbps etc.", "real\n text &nbsp;")

@helper RowEmojified(string label, string value) {
  var valueForShowing = value == null 
    ? "null" 
    : "\"" + value.Replace("\n", "\\n").Replace("\t", "\\t") + "\"";
    <td>@Boolmoji(Text.Has(value, false))</td>

@helper Boolmoji(bool value) {
  @(value ? "✔️" : "❌")

<h2>Special case: &lt;BR&gt; Whitespace</h2>

  <li>If your string is like Text.Has("&lt;br&gt;") it will be: @Text.Has("<br>")</li>
  <li>If you want to ignore BRs, combine it with @hlp.TutLink("Tags.Br2Nl(...)", "blade220") </li>
  <li>...resulting in: @Text.Has(Tags.Br2Nl("<br>"))</li>

<!-- unimportant stuff, hidden -->