Class StringHelper
Provides helper functions for working with strings.
Inheritance
Inherited Members
Namespace: Nevatech.Vsb.Repository
Assembly: Nevatech.Vsb.Repository.dll
Syntax
public static class StringHelper
Methods
GetTagSize(Int32, String, Boolean)
Returns approximate size of XML tag containing a string value. Assumption is that UTF-8 encoding and English language are used.
Declaration
public static int GetTagSize(int tagNameLength, string tagValue, bool emitDefaultValue)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | tagNameLength | Length of the tag name. |
System.String | tagValue | Tag value. |
System.Boolean | emitDefaultValue | Indicates that default (nil) value needs to be emitted, if tag value is Null. |
Returns
Type | Description |
---|---|
System.Int32 | Approximate size of the tag. |
Remarks
Generally tag size with value is tag name length multiplied by two plus 5 plus value size. Tag size without value is tag name plus 3. If that value is nullable and should be emitted then 13 needs to be added.
GetTagSize(Int32, String, Boolean, Boolean)
Returns approximate size of XML tag containing a string value. Assumption is that UTF-8 encoding and English language are used.
Declaration
public static int GetTagSize(int tagNameLength, string tagValue, bool emitDefaultValue, bool applyXmlEncoding)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | tagNameLength | Length of the tag name. |
System.String | tagValue | Tag value. |
System.Boolean | emitDefaultValue | Indicates that default (nil) value needs to be emitted, if tag value is Null. |
System.Boolean | applyXmlEncoding | Indicated that string will be XML-endoded (like > symbol will be replaced with ">" characters), so its length must be adjusted for that. |
Returns
Type | Description |
---|---|
System.Int32 | Approximate size of the tag. |
Remarks
Generally tag size with value is tag name length multiplied by two plus 5 plus value size. Tag size without value is tag name plus 3. If that value is nullable and should be emitted then 13 needs to be added.
GetTagSize<T>(Int32, T, Boolean)
Returns approximate size of XML tag containing a value-typed value. Assumption is that UTF-8 encoding and English language are used.
Declaration
public static int GetTagSize<T>(int tagNameLength, T tagValue, bool emitDefaultValue)
where T : struct, IComparable
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | tagNameLength | Length of the tag name. |
T | tagValue | Tag value. |
System.Boolean | emitDefaultValue | Indicates that default value needs to be emitted. |
Returns
Type | Description |
---|---|
System.Int32 | Approximate size of the tag. |
Type Parameters
Name | Description |
---|---|
T | Type of the tag value. |
GetTagSize<T>(Int32, Nullable<T>, Boolean)
Returns approximate size of XML tag containing a nullable value-typed value. Assumption is that UTF-8 encoding and English language are used.
Declaration
public static int GetTagSize<T>(int tagNameLength, T? tagValue, bool emitDefaultValue)
where T : struct, IComparable
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | tagNameLength | Length of the tag name. |
System.Nullable<T> | tagValue | Nullable tag value. |
System.Boolean | emitDefaultValue | Indicates that default value needs to be emitted. |
Returns
Type | Description |
---|---|
System.Int32 | Approximate size of the tag. |
Type Parameters
Name | Description |
---|---|
T | Type of the tag value. |
IndexOfAny(String, String[], StringComparison)
Reports the zero-based index of the first occurrence of any of the specified strings in this instance.
Declaration
public static int IndexOfAny(this string value, string[] values, StringComparison comparison)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The string to be searched. |
System.String[] | values | The strings to seek. |
System.StringComparison | comparison | One of the enumeration values that specifies the rules for the search. |
Returns
Type | Description |
---|---|
System.Int32 | The zero-based index position of value if that string is found, or -1 if it is not. |
IsEmail(String)
Checks if provided string represents a valid e-mail address.
Declaration
public static bool IsEmail(this string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | String to be examined. |
Returns
Type | Description |
---|---|
System.Boolean | True, if provided string is not empty and represents a valid e-mail address. False, otherwise. |
NormalizeNewLines(String)
Replaces all single new line characters (\n) with pairs of carriage return + new line characters (\r\n).
Declaration
public static string NormalizeNewLines(this string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | A string that needs to be normalized. |
Returns
Type | Description |
---|---|
System.String | A normalized string. |
Replace(String, IEnumerable<Token>)
Returns a new string in which all occurrences of a specified tokens are replaced with their values.
Declaration
public static string Replace(this string source, IEnumerable<Token> values)
Parameters
Type | Name | Description |
---|---|---|
System.String | source | A string where replacements must occur. |
System.Collections.Generic.IEnumerable<Token> | values | Collection of tokens containing old and new values to be used for string replacement. |
Returns
Type | Description |
---|---|
System.String | New string in which all occurrences of a specified tokens are replaced with their values. |
Replace(String, String, String, StringComparison)
Returns a new string in which all occurrences of a specified string in the provided source string are replaced with another specified string.
Declaration
public static string Replace(this string source, string oldValue, string newValue, StringComparison comparisonType)
Parameters
Type | Name | Description |
---|---|---|
System.String | source | A string where replacements must occur. |
System.String | oldValue | A string to be replaced. |
System.String | newValue | A string to replace all occurrences of old value. |
System.StringComparison | comparisonType | Specifies the rules of string comparison. |
Returns
Type | Description |
---|---|
System.String | New string in which all occurrences of a specified string in the provided source string are replaced with another specified string. |
ToString(DateTime)
Converts System.DateTime value into string value.
Declaration
public static string ToString(DateTime value)
Parameters
Type | Name | Description |
---|---|---|
System.DateTime | value | Date and time value to be converted. |
Returns
Type | Description |
---|---|
System.String | String value that represents serialized System.DateTime value. |
ToStringAsync(HttpRequestMessage, Boolean)
Writes HTTP request message to a string that can be used for logging purposes.
Declaration
public static Task<string> ToStringAsync(this HttpRequestMessage message, bool appendNewLines = false)
Parameters
Type | Name | Description |
---|---|---|
System.Net.Http.HttpRequestMessage | message | HTTP request message to be logged. |
System.Boolean | appendNewLines | Optional flag indicating that empty lines must be added in the beginning and at the end of the result string. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.String> | String containing the provided HTTP request message. |
ToStringAsync(HttpResponseMessage, Boolean)
Writes HTTP response message to a string that can be used for logging purposes.
Declaration
public static Task<string> ToStringAsync(this HttpResponseMessage message, bool appendNewLines = false)
Parameters
Type | Name | Description |
---|---|---|
System.Net.Http.HttpResponseMessage | message | HTTP response message to be logged. |
System.Boolean | appendNewLines | Optional flag indicating that empty lines must be added in the beginning and at the end of the result string. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.String> | String containing the provided HTTP response message. |
Truncate(String, Int32, String)
Truncates provided string up to specified number of characters.
Declaration
public static string Truncate(this string value, int count, string replacementSuffix)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | String that needs to be truncated. |
System.Int32 | count | Maximum number of characters in the string. |
System.String | replacementSuffix | String to append to the truncated string to indicate that truncation happened. |
Returns
Type | Description |
---|---|
System.String | Provided string truncated up to specified number of characters. |