Skip to main content

Razor Blade Tutorials

Tutorial Home

RazorBlade Text.First(...) v3

These demos show how to get the first real text of a series of values. Basically it will ignore nulls, empty texts, space-strings and even combinations containing empty lines, html-nbsps and more. Internally it uses Text.Has(...).

Demos

Basic 2-Value Demos

  1. Text.First(null, John) ⇒ John
  2. Text.First(null, null)
  3. Text.First(John, Michael) ⇒ John
  4. Text.First(" " = 3 spaces, John) ⇒ John
  5. Text.First(null, please-enter-name) ⇒ please-enter-name
  6. Text.First( , please-enter-name) ⇒ please-enter-name
  7. Text.First(false,  , please-enter-name) ⇒  
    "false" at the end means don't treat html-whitespace as whitespace, so   will be treated as a real value

3-5 Value Demos

Text.First has overloads (multiple signatures) for up to 5 values, and another one accepting an array of string objects so you can even use it for 10 values if you really need it.

  1. Text.First(null, John, please-enter-name) ⇒ John
  2. Text.First(null, John, Michael, please-enter-name) ⇒ John
  3. Text.First(null,  , John, Michael, please-enter-name) ⇒ John
  4. Text.First(false, null,  , John, Michael, please-enter-name) ⇒  
  5. Text.First(null, null,  , John, Michael, " ", please-enter-name) ⇒ John
  6. Text.First(false, null, null,  , John, Michael, " ", please-enter-name) ⇒  

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 -->

RazorBlade Text.First(...) v3 These... <!-- unimportant stuff, hidden -->

@{
  var val1 = "John";
  var val2 = "Michael";
  var spc = "   ";
  var fallback = "please-enter-name";
  var nbsp = "&nbsp;";
}

Demos Basic 2-Value Demos
<ol>
    <li>
      <code>Text.First(null, @val1)</code> 
      ⇒ @Text.First(null, val1)
    </li>
    <li>
      <code>Text.First(null, null)</code> 
      ⇒ @Text.First(null, null)
    </li>
    <li>
      <code>Text.First(@val1, @val2)</code> 
      ⇒ @Text.First(val1, val2)
    </li>
    <li>
      <code>Text.First("@spc" = 3 spaces, @val1)</code> 
      ⇒ @Text.First(spc, val1)
    </li>
    <li>
      <code>Text.First(null, @fallback)</code> 
      ⇒ @Text.First(null, fallback)
    </li>
    <li>
      <code>Text.First(@nbsp, @fallback)</code>
      ⇒ @Text.First(nbsp, fallback)
    </li>
    <li>
      <code>Text.First(false, @nbsp, @fallback)</code> 
      ⇒ @Text.First(false, nbsp, fallback) <br>
        <em></em>"false" at the end means don't treat html-whitespace as whitespace, so @nbsp will be treated as a real value</em>
    </li>
</ol>

3-5 Value Demos Text.First has overloads... <!-- unimportant stuff, hidden -->
<ol>
    <li>
      <code>Text.First(null, @val1, @fallback)</code> 
      ⇒ @Text.First(null, val1, fallback)
    </li>
    <li>
      <code>Text.First(null, @val1, @val2, @fallback)</code> 
      ⇒ @Text.First(null, val1, val2, fallback)
    </li>
    <li>
      <code>Text.First(null, @nbsp, @val1, @val2, @fallback)</code> 
      ⇒ @Text.First(null, nbsp, val1, val2, fallback)
    </li>
    <li>
      <code>Text.First(false, null, @nbsp, @val1, @val2, @fallback)</code> 
      ⇒ @Text.First(false, null, nbsp, val1, val2, fallback)
    </li>
    <li>
      <code>Text.First(null, null, @nbsp, @val1, @val2, "@spc", @fallback)</code> 
      ⇒ @Text.First(null, null, nbsp, val1, val2, spc, fallback)
    </li>
    <li>
      <code>Text.First(false, null, null, @nbsp, @val1, @val2, "@spc", @fallback)</code> 
      ⇒ @Text.First(false, null, null, nbsp, val1, val2, spc, fallback)
    </li>
</ol>

<!-- unimportant stuff, hidden -->