#5 Razorblade - Text.After(), Text.AfterLast(), Text.Before(), Text.BeforeLast(), Text.Between()
RazorBlade .After(), .AfterLast(), .Before(), .BeforeLast(), .Between()
For this to work please install RazorBlade 3.09 or 2sxc 13.02.
These demos show how to split text properly. Scenarios might be:
Split Text after first key occurence
Using Text.After(text, key)
only the text after the first found occurence of the key
will be shown.
Result: this will be shown
@{
var afterExample = "This won't be shown @ this will be shown";
}
<strong>Result:</strong> @Text.After(afterExample, "@")
Split Text after last key occurence
Using Text.AfterLast(text, key)
only the text after the last found occurence of the key
will be shown.
Result: this will be shown
@{
var afterLastExample = "This won't be shown @ this won't be shown @ this will be shown";
}
<strong>Result:</strong> @Text.AfterLast(afterLastExample, "@")
Split Text before first key occurence
Using Text.Before(text, key)
only the text before the first found occurence of the key
will be shown.
Result: This will be shown
@{
var beforeExample = "This will be shown @ this won't be shown";
}
<strong>Result:</strong> @Text.Before(beforeExample, "@")
Split Text before last key occurence
Using Text.BeforeLast(text, key)
only the text before the last found occurence of the key
will be shown.
Result: This will be shown @ this will be shown
@{
var beforeLastExample = "This will be shown @ this will be shown @ this won't be shown";
}
<strong>Result:</strong> @Text.BeforeLast(beforeLastExample, "@")
Split Text between two key ocurrences
Using Text.Between(text, key1, key2, goToEndIfEndNotFound = false)
only the text between key1
and key2
will be shown.
With goToEndIfEndNotFound set to true, the text selection will grow till the end if the ending key can't be found.
Result: this will be shown
Result: this will be shown # this won't be shown
@{
var betweenExample = "This won't be shown @ this will be shown # this won't be shown";
}
<strong>Result:</strong> @Text.Between(betweenExample, "@", "#")
<br><strong>Result:</strong> @Text.Between(betweenExample, "@", "notInText", true)
#5 Razorblade - Text.After(), Text.AfterLast(), Text.Before(), Text.BeforeLast(), Text.Between()
@inherits Custom.Hybrid.Razor14
@using ToSic.Razor.Blade;
<!-- unimportant stuff, hidden -->
<div class="row">
<div class="col-md-7">
RazorBlade .After(), .AfterLast()... <!-- unimportant stuff, hidden -->
</div>
@Html.Partial("../shared/_DefaultInfoSection.cshtml")
</div>
Split Text after first key occurence... <!-- unimportant stuff, hidden -->
@{
var afterExample = "This won't be shown @ this will be shown";
}
<strong>Result:</strong> @Text.After(afterExample, "@")
Split Text after last key occurence... <!-- unimportant stuff, hidden -->
@{
var afterLastExample = "This won't be shown @ this won't be shown @ this will be shown";
}
<strong>Result:</strong> @Text.AfterLast(afterLastExample, "@")
Split Text before first key occurence... <!-- unimportant stuff, hidden -->
@{
var beforeExample = "This will be shown @ this won't be shown";
}
<strong>Result:</strong> @Text.Before(beforeExample, "@")
Split Text before last key occurence... <!-- unimportant stuff, hidden -->
@{
var beforeLastExample = "This will be shown @ this will be shown @ this won't be shown";
}
<strong>Result:</strong> @Text.BeforeLast(beforeLastExample, "@")
Split Text between two key ocurrences... <!-- unimportant stuff, hidden -->
@{
var betweenExample = "This won't be shown @ this will be shown # this won't be shown";
}
<strong>Result:</strong> @Text.Between(betweenExample, "@", "#")
<br><strong>Result:</strong> @Text.Between(betweenExample, "@", "notInText", true)
<!-- unimportant stuff, hidden -->