RazorBlade Text Operations
RazorBlade helps with many helper functions to optimize working with string variables, also when they contain html.
RazorBlade Text.Crop(...) and Text.Ellipsis(...) v1.1
These demos show how to crop text properly, because Razor Blade handles a lot of issues you'll usually have cropping text.
Example Text
Original which visually has 31 characters:
- The human view - how it looks for users:
We love Tokyo, Zürich & München
123456789-123456789-123456789-1 - The truth - how the string looks internally:
We love Tokyo, Zürich & München
123456789-1234567.....89-123....456.....789-1
Using C# Method | Razor Blade | ||
---|---|---|---|
Len | C# Substring | Text.Crop(...) | Text.Ellipsis(...) |
1 | W... | W | W… |
2 | We... | We | We… |
3 | We ... | We | We… |
4 | We l... | We | We… |
5 | We lo... | We | We… |
6 | We lov... | We | We… |
7 | We love... | We love | We love… |
8 | We love ... | We love | We love… |
9 | We love T... | We love | We love… |
10 | We love To... | We love | We love… |
11 | We love Tok... | We love | We love… |
12 | We love Toky... | We love | We love… |
13 | We love Tokyo... | We love Tokyo | We love Tokyo… |
14 | We love Tokyo,... | We love Tokyo | We love Tokyo… |
15 | We love Tokyo, ... | We love Tokyo | We love Tokyo… |
16 | We love Tokyo, Z... | We love Tokyo | We love Tokyo… |
17 | We love Tokyo, Z&... | We love Tokyo | We love Tokyo… |
18 | We love Tokyo, Z&u... | We love Tokyo | We love Tokyo… |
19 | We love Tokyo, Z&uu... | We love Tokyo | We love Tokyo… |
20 | We love Tokyo, Z&uum... | We love Tokyo | We love Tokyo… |
21 | We love Tokyo, Zü... | We love Tokyo, Zürich | We love Tokyo, Zürich… |
22 | We love Tokyo, Zü... | We love Tokyo, Zürich | We love Tokyo, Zürich… |
23 | We love Tokyo, Zür... | We love Tokyo, Zürich & | We love Tokyo, Zürich &… |
24 | We love Tokyo, Züri... | We love Tokyo, Zürich & | We love Tokyo, Zürich &… |
25 | We love Tokyo, Züric... | We love Tokyo, Zürich & | We love Tokyo, Zürich &… |
26 | We love Tokyo, Zürich... | We love Tokyo, Zürich & | We love Tokyo, Zürich &… |
27 | We love Tokyo, Zürich ... | We love Tokyo, Zürich & | We love Tokyo, Zürich &… |
28 | We love Tokyo, Zürich &... | We love Tokyo, Zürich & | We love Tokyo, Zürich &… |
29 | We love Tokyo, Zürich &a... | We love Tokyo, Zürich & | We love Tokyo, Zürich &… |
30 | We love Tokyo, Zürich &am... | We love Tokyo, Zürich & | We love Tokyo, Zürich &… |
31 | We love Tokyo, Zürich &... | We love Tokyo, Zürich & München | We love Tokyo, Zürich & München |
32 | We love Tokyo, Zürich &... | We love Tokyo, Zürich & München | We love Tokyo, Zürich & München |
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.
Result
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 ") | ✔️ | ✔️ |
- 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:
⬇️ Result | Source ➡️
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.
⬇️ Result | Source ➡️
-
Text.First(null, John)
⇒ John -
Text.First(null, null)
⇒ -
Text.First(John, Michael)
⇒ John -
Text.First(" " = 3 spaces, John)
⇒ John -
Text.First(null, please-enter-name)
⇒ please-enter-name -
Text.First( , please-enter-name)
⇒ please-enter-name -
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
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.
⬇️ Result | Source ➡️
-
Text.First(null, John, please-enter-name)
⇒ John -
Text.First(null, John, Michael, please-enter-name)
⇒ John -
Text.First(null, , John, Michael, please-enter-name)
⇒ John -
Text.First(false, null, , John, Michael, please-enter-name)
⇒ -
Text.First(null, null, , John, Michael, " ", please-enter-name)
⇒ John -
Text.First(false, null, null, , John, Michael, " ", please-enter-name)
⇒
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
The example texts which have invisible problem characters:
⬇️ Result | Source ➡️
-
This contains multi-spaces and ↦↦↦ tabs
-
This has ↵ ↵ line-breaks
Result
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 |
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:
Using Text.After(text, key)
only the text after the first found occurence of the key
will be shown.
⬇️ Result | Source ➡️
Using Text.AfterLast(text, key)
only the text after the last found occurence of the key
will be shown.
⬇️ Result | Source ➡️
Using Text.Before(text, key)
only the text before the first found occurence of the key
will be shown.
⬇️ Result | Source ➡️
Using Text.BeforeLast(text, key)
only the text before the last found occurence of the key
will be shown.
⬇️ Result | Source ➡️
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 | Source ➡️
Result: this will be shown # this won't be shown
RazorBlade Tags BR/NewLine Conversions v1.1
These demos show how to convert BR tags to spaces, New-Lines or New-Lines to BRs.
⬇️ Result | Source ➡️
This is... | Shown as plain text | Shown as HTML |
---|---|---|
Original | This is a text. Second line. Third line. | |
Converting with Tags.Nl2Br(...) | This is a text. Second line. Third line. |
|
Convert the br back with Tags.Br2Nl | This is a text. Second line. Third line. | |
Cleaning BR as Space with Tags.Br2Space | This is a text. Second line. Third line. |