Class StringHelper
A collection of helper functions for common string manipulation
Inheritance
Inherited Members
Namespace: LemonEdge.Utils
Assembly: Utils.dll
Syntax
public static class StringHelper
Fields
DataItemsSeparator
Used when seperating data items in a single string
Declaration
public const string DataItemsSeparator = ";;"
Field Value
Type | Description |
---|---|
System.String |
DateInternationalFormat
Formats a datetime as a single date in international, sortable format
Declaration
public const string DateInternationalFormat = "yyyy-MM-dd"
Field Value
Type | Description |
---|---|
System.String |
DateInternationalPreciseFormat
Formats a datetime as a date and time in international, sortable format complete with milliseconds
Declaration
public const string DateInternationalPreciseFormat = "yyyy-MM-dd HH:mm:ss.fff"
Field Value
Type | Description |
---|---|
System.String |
DateInternationalTimeNoSecondsFormat
Formats a datetime as a date and time in international, sortable format without the seconds component
Declaration
public const string DateInternationalTimeNoSecondsFormat = "yyyy-MM-dd HH:mm"
Field Value
Type | Description |
---|---|
System.String |
DateLongUserFriendlyFormat
Formats a datetime as a date in the long user friendly format using current culture
Declaration
public const string DateLongUserFriendlyFormat = "D"
Field Value
Type | Description |
---|---|
System.String |
DateShortUserFriendlyFormat
Formats a datetime as a date short user friendly format using current culture
Declaration
public const string DateShortUserFriendlyFormat = "d"
Field Value
Type | Description |
---|---|
System.String |
DateTimeFileFriendlyFormat
Formats a datetime as a date and time in international, sortable format without the seconds component or colon separators
Declaration
public const string DateTimeFileFriendlyFormat = "yyyy-MM-dd HHmm"
Field Value
Type | Description |
---|---|
System.String |
DateTimeInternationalFormat
Formats a datetime as a date and time in international, sortable format
Declaration
public const string DateTimeInternationalFormat = "yyyy-MM-dd HH:mm:ss"
Field Value
Type | Description |
---|---|
System.String |
DateTimeLongUserFriendlyFormat
Formats a datetime as a date and time in the long user friendly format using current culture
Declaration
public const string DateTimeLongUserFriendlyFormat = "F"
Field Value
Type | Description |
---|---|
System.String |
DateTimeOffsetPreciseFormat
Formats a datetimeoffset as a date and time in international, sortable format with precision
Declaration
public const string DateTimeOffsetPreciseFormat = "yyyy-MM-ddTHH:mm:ss.fffffffK"
Field Value
Type | Description |
---|---|
System.String |
DateTimeShortUserFriendlyFormat
Formats a datetime as a date and time in the short user friendly format using current culture
Declaration
public const string DateTimeShortUserFriendlyFormat = "g"
Field Value
Type | Description |
---|---|
System.String |
TimeLongUserFriendlyFormat
Formats a datetime as a in the long user friendly format using current culture
Declaration
public const string TimeLongUserFriendlyFormat = "T"
Field Value
Type | Description |
---|---|
System.String |
TimeShortUserFriendlyFormat
Formats a datetime as a time in the short user friendly format using current culture
Declaration
public const string TimeShortUserFriendlyFormat = "t"
Field Value
Type | Description |
---|---|
System.String |
Methods
FromSafeIISQueryString(String)
Replaces any instances of '&fs' with '/' for IIS integration with query strings
Declaration
public static string FromSafeIISQueryString(this string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | A string to be replaced |
Returns
Type | Description |
---|---|
System.String |
FromStringValue(Type, String)
Parses a string value and returns the item as an instance of the specified type t
if it can be converted as such
Declaration
public static (bool Valid, object Result) FromStringValue(Type t, string value)
Parameters
Type | Name | Description |
---|---|---|
System.Type | t | The type to convert the given |
System.String | value | The string representation of the type |
Returns
Type | Description |
---|---|
System.ValueTuple<System.Boolean, System.Object> | A Tuple indicating if the conversion was sucessfull and an instance of the type |
FromStringValue<T>(String)
Parses a string value and returns the item as an instance of the specified type T
if it can be converted as such
Declaration
public static (bool Valid, T Result) FromStringValue<T>(string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The string representation of the type |
Returns
Type | Description |
---|---|
System.ValueTuple<System.Boolean, T> | A Tuple indicating if the conversion was sucessfull and an instance of the type |
Type Parameters
Name | Description |
---|---|
T | The type to convert the given |
GetDefaultStandardFormat(Type)
Returns the default string formatting to apply to given types
Declaration
public static string GetDefaultStandardFormat(Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The type to see if there is a default format display for |
Returns
Type | Description |
---|---|
System.String | A default string formatting to apply to instances of the given type |
Remarks
For instance by default LemonEdge displays decimals to 3 decimal places unless otherwise supplied a specific format
GetRandomAlpha(StringHelper.CharLetterTypeCase)
Returns a random alphabet character according to the type
parameter
Declaration
public static char GetRandomAlpha(StringHelper.CharLetterTypeCase type)
Parameters
Type | Name | Description |
---|---|---|
StringHelper.CharLetterTypeCase | type | Indicates if the character returned should come from the set of characters that are lower case, upper case, or both |
Returns
Type | Description |
---|---|
System.Char | A single random alphabet character |
GetRandomTextString(Int32)
Returns a string of random alphabet characters (of upper and lower case) of the specified length
Declaration
public static string GetRandomTextString(int length)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | length | The length of the string to return |
Returns
Type | Description |
---|---|
System.String | A string of random alphabet characters (of upper and lower case) up to the specified |
IsSafeFileName(String, Boolean)
Ensures that the provided string can be used as a valid filename. In other words only contains valid alphanumeric characters and the underscore.
Declaration
public static bool IsSafeFileName(this string s, bool allowNullOrEmpty)
Parameters
Type | Name | Description |
---|---|---|
System.String | s | A string to be validated if it only contains akphanumeric characters and the underscore |
System.Boolean | allowNullOrEmpty | Indicates if a null or empty string is valid |
Returns
Type | Description |
---|---|
System.Boolean | True if the string only contains alphanumeric characters and the underscore, false otherwise |
IsSafeFileName(String, Boolean, IEnumerable<Char>)
Ensures that the provided string can be used as a valid filename. In other words only contains valid alphanumeric characters and specified validNonAlphaNumericalChars
characters
Declaration
public static bool IsSafeFileName(this string s, bool allowNullOrEmpty, IEnumerable<char> validNonAlphaNumericalChars = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | s | A string to be validated if it only contains akphanumeric characters and specified |
System.Boolean | allowNullOrEmpty | Indicates if a null or empty string is valid |
System.Collections.Generic.IEnumerable<System.Char> | validNonAlphaNumericalChars | A list of non alphanumerical characters that are also valid to be included in a filename, such as ' ' or '_' |
Returns
Type | Description |
---|---|
System.Boolean | True if the string only contains alphanumeric characters and specified |
OpenBrowser(String)
A process that takes a valid url and opens it in the default browser across possible .Net Core platforms
Declaration
public static void OpenBrowser(string url)
Parameters
Type | Name | Description |
---|---|---|
System.String | url | The url to open in a new browser |
Remarks
ToCSV(IEnumerable, String[], Type[], String[])
Returns a csv formatted StringBuilder of the specified items
with columns from the specified properties
Declaration
public static StringBuilder ToCSV(this IEnumerable items, string[] properties, Type[] propertyTypes, string[] friendlyExcelHeaders = null)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.IEnumerable | items | A collection of items to return as csv formatted rows |
System.String[] | properties | The properties on the items that should be returned as columns in the csv format |
System.Type[] | propertyTypes | The types of the properties to be returned as columns in the csv format |
System.String[] | friendlyExcelHeaders | The properties are always displayed as the column headers. This will be displayed as a second line of headers to be used in the way LemonEdge handles headers in excel, the second line (this one) is displayed whereas the first is tuend into named ranges |
Returns
Type | Description |
---|---|
System.Text.StringBuilder | A comma seperated list with a row for each item in the |
Remarks
For example the following:
class MyItem
{
public string Prop1 { get; set; }
public string Prop2 { get; set; }
}
public string DisplayAsCSV()
{
var items = new List<MyItem>() { new MyItem() { Prop1 = "Row1", Prop2 = "Value1" }, new MyItem() { Prop1 = "Row2", Prop2 = "Value2" }, };
return StringHelper.ToCSV(items, new string[] { nameof(MyItem.Prop1) }, new Type[] { typeof(String) }).ToString();
}
will return:
Prop1,Prop2
Row1,Value1
Row2,Value2
ToSafeFileName(String)
Strips out any characters that would be invalid in a filename. This would be any non alphanumeric characters that are not a space or underscore
Declaration
public static string ToSafeFileName(this string s)
Parameters
Type | Name | Description |
---|---|---|
System.String | s | The string to be returned as a safe filename string |
Returns
Type | Description |
---|---|
System.String | An instance of |
ToSafeFileName(String, IEnumerable<Char>)
Strips out any characters that would be invalid in a filename. This would be any non alphanumeric characters that are not a space or underscore
Declaration
public static string ToSafeFileName(this string s, IEnumerable<char> validNonAlphaNumericalChars = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | s | The string to be returned as a safe filename string |
System.Collections.Generic.IEnumerable<System.Char> | validNonAlphaNumericalChars | A list of non alphanumerical characters that are also valid to be included in a file name such as ' ' or '_' |
Returns
Type | Description |
---|---|
System.String | An instance of |
ToSafeIISQueryString(String)
Replaces '/' character with '&fs' for IIS integration with query strings
Declaration
public static string ToSafeIISQueryString(this string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | A string to be replaced |
Returns
Type | Description |
---|---|
System.String |
ToStringValue(Type, Object)
Formats the current object using its associated default string representation
Declaration
public static string ToStringValue(Type t, object value)
Parameters
Type | Name | Description |
---|---|---|
System.Type | t | The type of the value to be represented using a string format |
System.Object | value | The object instance of type T to be represented in a default string format |
Returns
Type | Description |
---|---|
System.String | A default string formatted representation of the given |
Remarks
For example the following types will return:
- Enum Values: Returns string representation of Enum value
- DateTimeOffsets: Returns string representation in DateTimeOffsetPreciseFormat
- DateTimes: Returns string representation in DateInternationalPreciseFormat
- Bytes: Returns hexadecimal string equivalent starting 0x
ToStringValue<T>(T)
Formats the current object using its associated default string representation
Declaration
public static string ToStringValue<T>(T value)
Parameters
Type | Name | Description |
---|---|---|
T | value | The object instance of |
Returns
Type | Description |
---|---|
System.String | A default string formatted representation of the given |
Type Parameters
Name | Description |
---|---|
T | The type of object to be represented using a string format |
Remarks
For example the following types will return:
- Enum Values: Returns string representation of Enum value
- DateTimeOffsets: Returns string representation in DateTimeOffsetPreciseFormat
- DateTimes: Returns string representation in DateInternationalPreciseFormat
- Bytes: Returns hexadecimal string equivalent starting 0x
TryParseFromString<T>(String, out T)
Parses a string value and returns true if it is valid. The item is returned as an instance of the specified type T
in the out parameter result
Declaration
public static bool TryParseFromString<T>(string value, out T result)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | A string value of type T |
T | result | If the string representation |
Returns
Type | Description |
---|---|
System.Boolean | True if the string representation can be parsed |
Type Parameters
Name | Description |
---|---|
T | The type of item to parse the string representation of |