Class Text
Many simple commands to work with Text / Strings
Inheritance
Namespace: ToSic.Razor.Blade
Assembly: ToSic.Razor.dll
Syntax
public static class Text
Methods
| Improve this Doc View SourceAfter(String, String, Boolean)
Get string value after the (first occurrence) key. Case insensitive by default. Will return null in various cases which don't make sense or if not found. Very safe and robust, fully tested.
Declaration
public static string After(this string value, string key, bool caseSensitive = false)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The initial string. If null, will always return null |
System.String | key | The key to find. If null, will always return null |
System.Boolean | caseSensitive | Set to true if you need case-sensitive compare |
Returns
Type | Description |
---|---|
System.String |
Remarks
New in v3.09
AfterLast(String, String, Boolean)
Get string value after the last occurrence of a key. Will return null in various cases which don't make sense or if not found. Case insensitive by default. Very safe and robust, fully tested.
Declaration
public static string AfterLast(this string value, string key, bool caseSensitive = false)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The initial string. If null, will always return null |
System.String | key | The key to find. If null, will always return null |
System.Boolean | caseSensitive | Set to true if you need case-sensitive compare |
Returns
Type | Description |
---|---|
System.String |
Remarks
New in v3.09
Before(String, String, Boolean)
Get string value before the key. Case insensitive by default. Will return null in various cases which don't make sense or if not found. Very safe and robust, fully tested.
Declaration
public static string Before(this string value, string key, bool caseSensitive = false)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The initial string. If null, will always return null |
System.String | key | The key to find. If null, will always return null |
System.Boolean | caseSensitive | Set to true if you need case-sensitive compare |
Returns
Type | Description |
---|---|
System.String |
Remarks
New in v3.09
BeforeLast(String, String, Boolean)
Get string value before the last occurrence of a key. Case insensitive by default. Will return null in various cases which don't make sense or if not found. Very safe and robust, fully tested.
Declaration
public static string BeforeLast(this string value, string key, bool caseSensitive = false)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The initial string. If null, will always return null |
System.String | key | The key to find. If null, will always return null |
System.Boolean | caseSensitive | Set to true if you need case-sensitive compare |
Returns
Type | Description |
---|---|
System.String |
Remarks
New in v3.09
Between(String, String, String, Boolean, Boolean)
Get the text between two keys
Declaration
public static string Between(this string value, string before, string after, bool goToEndIfEndNotFound = false, bool caseSensitive = false)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The initial string. If null, will always return null |
System.String | before | The key to find in front of what we're looking for. If null, will always return null |
System.String | after | The key to find in front of what we're looking for. If null, will always return null |
System.Boolean | goToEndIfEndNotFound | If true and the end isn't found, it will return the rest of the string |
System.Boolean | caseSensitive | Set to true if you need case-sensitive compare |
Returns
Type | Description |
---|---|
System.String |
Remarks
New in v3.09
Crop(String, Int32)
Cut off a text at the best possible place with a max-length. This will count html-entities like &, or umlauts as 1 character, and will try to cut off between words if possible. So it will backtrack to the previous space.
Declaration
public static string Crop(this string value, int length)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | String to cut off. Can contain umlauts and html-entities, but should not contain html-tags as there are not treated properly. |
System.Int32 | length | length to cut off at |
Returns
Type | Description |
---|---|
System.String |
Ellipsis(String, Int32, String)
Crop a text if too long, add in that case, also add an ellipsis … or a custom suffix (optional)
Declaration
public static string Ellipsis(this string value, int length, string suffix = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | Value to maybe truncate (using safe-truncate - see Text.Crop) |
System.Int32 | length | Max length to keep |
System.String | suffix | optional suffix, defaults to … |
Returns
Type | Description |
---|---|
System.String |
Remarks
If you don't need the suffix, use CropText(...) instead
First(Boolean, String[])
Try to return the first possible value, but if it's empty, return null
Declaration
public static string First(bool handleHtmlWhitespaces = true, params string[] values)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | handleHtmlWhitespaces | if true (default) will treat html-whitespace as a space |
System.String[] | values | array of values to check consecutively |
Returns
Type | Description |
---|---|
System.String |
First(String[])
Try to return the first possible value, but if it's empty, return null
Declaration
public static string First(params string[] values)
Parameters
Type | Name | Description |
---|---|---|
System.String[] | values | array of values to check consecutively |
Returns
Type | Description |
---|---|
System.String |
Has(Object, Boolean)
Returns true if it has real text, false if it's null, not a string, an empty string
or a string containing just whitespace and/or html-whitespaces like  
or  
Declaration
public static bool Has(object value, bool handleHtmlWhitespaces = true)
Parameters
Type | Name | Description |
---|---|---|
System.Object | value | |
System.Boolean | handleHtmlWhitespaces | if true (default) will treat html-whitespace as a space |
Returns
Type | Description |
---|---|
System.Boolean | true, if it's a string with real content |
Remarks
will try to cast the object as string first, so it will be null if not a real string
Has(String, Boolean)
Will check if a string actually has contents (not null, empty or just spaces)
Declaration
public static bool Has(this string value, bool handleHtmlWhitespaces = true)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | |
System.Boolean | handleHtmlWhitespaces | if true (default) will treat html-whitespace as a space |
Returns
Type | Description |
---|---|
System.Boolean | true, if the string has real contents |
Zip(String)
Will remove all new-lines from a string and merge multiple spaces together.
Declaration
public static string Zip(this string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value |
Returns
Type | Description |
---|---|
System.String |