#2 RazorBlade - Text.Has() tells you if something really has content
Requirements
Resources
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
Note that these examples use functions, emojis and RazorBlade Fluent API so click on the links if you don't understand that.
Examples
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:
Output
False
#2 RazorBlade - Text.Has() tells you if something really has content
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. Click to expand the code
@inherits Custom.Hybrid.Razor14 @using ToSic.Razor.Blade; <!-- unimportant stuff, hidden --> <div @Sys.PageParts.InfoWrapper()> @Html.Partial("../shared/DefaultInfoSection.cshtml") <div @Sys.PageParts.InfoIntro()> <h2><em>RazorBlade</em> Text.Has(...) <em>v1.1</em></h2> <p> These demos show how to really check if a variable has text using Text.Has. This combines checks for... </p> <ul> <li>null</li> <li>empty</li> <li>only html-nbsp</li> <li>only html character #160 (also nbsp)</li> <li>only new-line</li> </ul> </div> </div> <div class="alert alert-success mt-2" role="alert"> Note that these examples use @Sys.TutLink("functions", "reuse=home"), @Sys.TutLink("emojis", "html210") and @Sys.TutLink("RazorBlade Fluent API", "blade800") so click on the links if you don't understand that. </div> @functions { // Quick helper to convert true/false into emojis string Boolmoji(bool value) { return value ? "✔️" : "❌"; } // Create a row (TR) containing data about a Text.Has example dynamic RowEmojified(string label, string value) { var valueForShowing = value == null ? "null" : "\"" + value.Replace("\n", "\\n").Replace("\t", "\\t") + "\""; return Tag.Tr( Tag.Td(label), Tag.Td("Text.Has(" + Tags.Encode(valueForShowing) + ")"), Tag.Td(Boolmoji(Text.Has(value))), Tag.Td(Boolmoji(Text.Has(value, false))) ); } } <h2>Examples</h2> <table class="demo table table-hover"> <tr> <th>Test</th> <th>Code</th> <th>Result</th> <th>...when html counts</th> </tr> @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", " ") @RowEmojified("char-code of nbsp characters", " ") @RowEmojified("real text", "real text") @RowEmojified("Real text with nbps etc.", "real\n text ") </table> <h2>Special case: <BR> Whitespace</h2> <ul> <li>If your string is like Text.Has("<br>") it will be: @Text.Has("<br>")</li> <li>If you want to ignore BRs, combine it with @Sys.TutLink("Tags.Br2Nl(...)", "blade220") </li> <li>...resulting in:</li> </ul> @Text.Has(Tags.Br2Nl("<br>")) <!-- unimportant stuff, hidden --> @* Footer *@ @Html.Partial("../Shared/Layout/FooterWithSource.cshtml", new { Sys = Sys })