StringHelper Class
Namespace: PTS
Assembly: PTSframework.dll
Provides a set of static methods for string
type.
Syntax
public static class StringHelper
Inheritance
Methods
After(String, String)
Gets a substring after the last occurence of delimiter.
Declaration
public static string After(this string input, string delimiter)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to get the substring from. |
System.String | delimiter | Regular expression with the pattern that will be used as leftmost boundary for new the substring. |
Returns
Type | Description |
---|---|
System.String | String remainder of the original input after the delimiter. |
Append(String, String[])
Appends instances of string tokens to this instance.
Declaration
public static string Append(this string input, params string[] tokens)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The instance to append the tokens to. |
System.String[] | tokens | The string tokens to append. |
Returns
Type | Description |
---|---|
System.String | A new instance of string with the original input joined by specified tokens. |
Base64Decode(String)
Converts the specified string, which encodes binary data as base-64 digits, to an equivalent utf-8 string.
Declaration
public static string Base64Decode(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The base-64 encoded string to convert. |
Returns
Type | Description |
---|---|
System.String | A string that contains the results of decoding the specified base-64 string. |
Base64Encode(String)
Converts the specified utf-8 string to its equivalent string representation that is encoded with base-64 digits.
Declaration
public static string Base64Encode(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to encode. |
Returns
Type | Description |
---|---|
System.String | The string representation, in base 64, of the original string input. |
Before(String, String)
Gets a substring before the first occurence of delimiter.
Declaration
public static string Before(this string input, string delimiter)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to get the substring from. |
System.String | delimiter | Regular expression with the pattern that will be used as rightmost boundary for new the substring. |
Returns
Type | Description |
---|---|
System.String | String remainder of the original input first the first occurence of specified delimiter. |
Chunk(String, Int32, Int32)
Retrieves a substring from this instance at specified length and starting position.
Declaration
public static string Chunk(this string input, int length, int startIndex = 0)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string instance to get a part of. |
System.Int32 | length | The number of characters in the substring. |
System.Int32 | startIndex | The zero-based starting character position of a substring in this instance. |
Returns
Type | Description |
---|---|
System.String | A string that is equivalent to the substring of length that begins at startIndex in this instance. |
CompactWhitespace(String)
Replaces muliple sequential white space characters with single white space.
Declaration
public static string CompactWhitespace(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to remove extra white space characters from. |
Returns
Type | Description |
---|---|
System.String | A new string instance with extra white spaces removed. |
ContainsEmails(String)
Checks whether this instance of string contains an email address.
Declaration
public static bool ContainsEmails(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to match the email address pattern against. |
Returns
Type | Description |
---|---|
System.Boolean | If the string contains an email address then |
ContainsHtml(String)
Checks whether this instance of string contains html code.
Declaration
public static bool ContainsHtml(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to match the html code patterns against. |
Returns
Type | Description |
---|---|
System.Boolean | If the string contains html code then |
ContainsNumber(String)
Checks whether this instance of string contains a number.
Declaration
public static bool ContainsNumber(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to match the number pattern against. |
Returns
Type | Description |
---|---|
System.Boolean | If the string contains a number then |
ContainsUrl(String)
Checks whether this instance of string contains an url address.
Declaration
public static bool ContainsUrl(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to match the url address pattern against. |
Returns
Type | Description |
---|---|
System.Boolean | If the string contains an url address then |
Count(String, Char)
Gets the number of specified character occurences in this instance of a string.
Declaration
public static int Count(this string input, char ch)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to get the number of character occurrences from. |
System.Char | ch | The character to look for. |
Returns
Type | Description |
---|---|
System.Int32 | An integer input representing the number of occurrences of the specified character in given string instance. |
Ending(String, Int32)
Gets a substring after the specified character position.
Declaration
public static string Ending(this string input, int start)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to get the substring from. |
System.Int32 | start | Integer input representing the starting position of the substring. |
Returns
Type | Description |
---|---|
System.String | String remainder of the original input after the specified position. |
EscapeHtml(String)
Replaces html tag charcters with html encoded equivalents.
Declaration
public static string EscapeHtml(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to escape the html code in. |
Returns
Type | Description |
---|---|
System.String | A new string instance with escaped html tags. |
FileExtension(String)
Parses out the file extension input out of this string instance.
Declaration
public static string FileExtension(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to parse the file extension from. |
Returns
Type | Description |
---|---|
System.String | File extension if present, otherwise |
FirstMatchPosition(String, String)
Returns the position of the first regular expression pattern match in this string instance.
Declaration
public static int? FirstMatchPosition(this string input, string pattern)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to match the pattern against. |
System.String | pattern | Regular expression pattern to look for. |
Returns
Type | Description |
---|---|
System.Nullable<System.Int32> | If the pattern has match then the postion of the first occurence, otherwise |
FirstWord(String)
Gets the first contiguous character set before the first white space character in this string instance.
Declaration
public static string FirstWord(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to get the first word from. |
Returns
Type | Description |
---|---|
System.String | A new string instance with the first word from the original string. |
HtmlToPlaintext(String)
Replaces html line breaks and whitespace characters with their plain text equivalents.
Declaration
public static string HtmlToPlaintext(string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to replace the html text flow elements with plaintext characters in. |
Returns
Type | Description |
---|---|
System.String | A new instance of string with plain text version of line breaks and white spaces. |
IsBoolean(String)
Checks whether this instance of string represents boolean input.
Declaration
public static bool IsBoolean(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to match against boolean input patterns. |
Returns
Type | Description |
---|---|
System.Boolean | If the string represents boolean input then |
IsDecimal(String)
Checks whether this instance of string represents decimal number.
Declaration
public static bool IsDecimal(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to match against decimal number patterns. |
Returns
Type | Description |
---|---|
System.Boolean | If the string represents decimal number then |
IsEmail(String)
Checks whether this instance of string represents email address.
Declaration
public static bool IsEmail(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to match against email address patterns. |
Returns
Type | Description |
---|---|
System.Boolean | If the string represents email address then |
IsEmpty(String)
Checks whether this instance of string is null or empty.
Declaration
public static bool IsEmpty(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to check if empty or null. |
Returns
Type | Description |
---|---|
System.Boolean | If the string is null or empty then |
IsFilePath(String)
Checks whether this instance of string represents system file path.
Declaration
public static bool IsFilePath(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to match against system file path patterns. |
Returns
Type | Description |
---|---|
System.Boolean | If the string represents system file path then |
IsIntegral(String)
Checks whether this instance of string represents a signed integer number.
Declaration
public static bool IsIntegral(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to match against signed integer number patterns. |
Returns
Type | Description |
---|---|
System.Boolean | If the string represents a signed integer number then |
IsLongerThan(String, Int32)
Checks whether this instance of string is longer than specified length.
Declaration
public static bool IsLongerThan(this string input, int length)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to check the length of. |
System.Int32 | length | Lenght the string should be longer than. |
Returns
Type | Description |
---|---|
System.Boolean | If the string is longer than specified length then |
IsMatch(String, String)
Checks whether this instance of string matches the specified pattern.
Declaration
public static bool IsMatch(this string input, string pattern)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to match against the pattern. |
System.String | pattern | Regular expression pattern to look for. |
Returns
Type | Description |
---|---|
System.Boolean | If the string matches the specified pattern then |
IsPhoneNumber(String)
Checks whether this instance of string represents a phone number.
Declaration
public static bool IsPhoneNumber(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to match against phone number patterns. |
Returns
Type | Description |
---|---|
System.Boolean | If the string represents phone number then |
IsText(String)
Checks whether this instance of string represents general text content.
Declaration
public static bool IsText(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to match against general text content patterns. |
Returns
Type | Description |
---|---|
System.Boolean | If the string represents general text content then |
IsUnsignedDecimal(String)
Checks whether this instance of string represents an unsigned decimal number.
Declaration
public static bool IsUnsignedDecimal(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to match against unsigned decimal number patterns. |
Returns
Type | Description |
---|---|
System.Boolean | If the string represents unsigned decimal number then |
IsUnsignedIntegral(String)
Checks whether this instance of string represents an unsigned integer number.
Declaration
public static bool IsUnsignedIntegral(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to match against unsigned integer number patterns. |
Returns
Type | Description |
---|---|
System.Boolean | If the string represents unsigned integer number then |
IsUrl(String)
Checks whether this instance of string represents an url address.
Declaration
public static bool IsUrl(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to match against url address patterns. |
Returns
Type | Description |
---|---|
System.Boolean | If the string represents an url address then |
LastMatchPosition(String, String)
Returns the position of the last regular expression pattern match in this string instance.
Declaration
public static int? LastMatchPosition(this string input, string pattern)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to match the pattern against. |
System.String | pattern | Regular expression pattern to look for. |
Returns
Type | Description |
---|---|
System.Nullable<System.Int32> | If the pattern has match then the postion of the last occurence, otherwise |
NotEmpty(String)
Checks whether this instance of string is not null nor empty.
Declaration
public static bool NotEmpty(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to check if not empty nor null. |
Returns
Type | Description |
---|---|
System.Boolean | If the string is null or empty then |
Remove(String, String)
Retrieves a new string instance with removed parts matching the specified pattern.
Declaration
public static string Remove(this string input, string pattern)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to remove the matches from. |
System.String | pattern | Regular expression pattern to look for. |
Returns
Type | Description |
---|---|
System.String | A new string that with removed parts matching the pattarn specified as parameter. |
RemoveHtml(String)
Returns a new string with all occurences of html tags removed.
Declaration
public static string RemoveHtml(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to remove the html tags from. |
Returns
Type | Description |
---|---|
System.String | A new string without any html tags. |
RemoveWhitespace(String)
Returns a new string with all occurences of white space characters removed.
Declaration
public static string RemoveWhitespace(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to remove the white space characters from. |
Returns
Type | Description |
---|---|
System.String | A new string without any white space characters. |
Repeat(String, Int32)
Returns a new string with specified number of repeated occurences of the original string.
Declaration
public static string Repeat(this string input, int count)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to be repeated. |
System.Int32 | count | The number of repetitions. |
Returns
Type | Description |
---|---|
System.String | A new string with specified number of repetitions of the original string. |
ReplaceMatch(String, String, String)
Declaration
public static string ReplaceMatch(this string input, string pattern, string replacement)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | |
System.String | pattern | |
System.String | replacement |
Returns
Type | Description |
---|---|
System.String |
Reverse(String)
Returns a new string with reversed character sequence.
Declaration
public static string Reverse(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to reverse the characters for. |
Returns
Type | Description |
---|---|
System.String | A new string with reversed order of characters. |
Sha256(String)
Returns SHA256 hash representation of given string.
Declaration
public static string Sha256(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | Value to represent as SHA256 hash. |
Returns
Type | Description |
---|---|
System.String | A new string representing SHA256 equivalent of the original string. |
Snippet(String, Int32, String)
Returns a new substring at specified lenght with specified suffix at the end if the substring is shorter than the original.
Declaration
public static string Snippet(this string input, int length, string suffix = "...")
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string instance to get a snippet from. |
System.Int32 | length | Character count of the output snippet. |
System.String | suffix | Will be appended at the end if the substring is shorter than the original string. |
Returns
Type | Description |
---|---|
System.String | Returns a new substring at specified length with specified suffix at the if the substring is shorter than the original string. |
SplitFill(String, ICollection<String>, String, Boolean)
Splits the string into chunks with specified delimiter and fills the provided collection with the results.
Declaration
public static void SplitFill(this string input, ICollection<string> collection, string delimiter, bool skipEmpty = true)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to split into substrings. |
System.Collections.Generic.ICollection<System.String> | collection | Collection to add the resulting substrings to. |
System.String | delimiter | Regular expression with the pattern that will be used as the boundary to identify new substrings. |
System.Boolean | skipEmpty | If set to |
ToChunks(String, String, Boolean)
Splits the string into chunks with specified delimiter and returns the results as array.
Declaration
public static string[] ToChunks(this string input, string delimiter, bool skipEmpty = true)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to split into substrings. |
System.String | delimiter | Regular expression with the pattern that will be used as the boundary to identify new substrings. |
System.Boolean | skipEmpty | If set to |
Returns
Type | Description |
---|---|
System.String[] | Array of substrings that were separated from the original string by specified delimiter. |
ToEnum<TEnum>(String, Boolean)
Converts the specified string to an enumeration member.
Declaration
public static TEnum ToEnum<TEnum>(this string input, bool ignoreCase = true)
where TEnum : struct, Enum
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The value convert to an enumeration member. |
System.Boolean | ignoreCase | If set to |
Returns
Type | Description |
---|---|
TEnum | An enumeration object with equivalent value if present, othewise default value of given |
Type Parameters
Name | Description |
---|---|
TEnum | The enumeration type to return. |
ToHtml(String)
Declaration
public static string ToHtml(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input |
Returns
Type | Description |
---|---|
System.String |
ToLines(String, Boolean)
Splits the string into chunks with line breaks and returns the results as array.
Declaration
public static string[] ToLines(this string input, bool skipEmpty = true)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to split into substrings. |
System.Boolean | skipEmpty | If set to |
Returns
Type | Description |
---|---|
System.String[] | Array of substrings that were separated from the original string by line breaks. |
ToMatches(String, String)
Searches the specified input string for all occurrences of a regular expression and returns matched values as array.
Declaration
public static string[] ToMatches(this string input, string pattern)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to search for a match. |
System.String | pattern | The regular expression pattern to match. |
Returns
Type | Description |
---|---|
System.String[] | Strings found by the search. If no matches are found, the method returns an empty string array. |
ToNullableEnum<TEnum>(String, Boolean)
Converts the specified string to a nullable enumeration member.
Declaration
public static TEnum? ToNullableEnum<TEnum>(this string input, bool ignoreCase = true)
where TEnum : struct, Enum
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The value convert to an enumeration member. |
System.Boolean | ignoreCase | If set to |
Returns
Type | Description |
---|---|
System.Nullable<TEnum> | A nullable enumeration object with equivalent value if present, othewise |
Type Parameters
Name | Description |
---|---|
TEnum | The enumeration type to return as nullable. |
ToPhoneNumber(String)
Declaration
public static string ToPhoneNumber(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input |
Returns
Type | Description |
---|---|
System.String |
ToStream(String)
Declaration
public static Stream ToStream(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input |
Returns
Type | Description |
---|---|
System.IO.Stream |
ToWords(String, Boolean)
Splits the string into chunks with white space delimiter and returns the results as array.
Declaration
public static string[] ToWords(this string input, bool skipEmpty = true)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to split into substrings. |
System.Boolean | skipEmpty | If set to |
Returns
Type | Description |
---|---|
System.String[] | Array of substrings that were separated from the original string by white space characters. |
UrlEncode(String)
Declaration
public static string UrlEncode(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input |
Returns
Type | Description |
---|---|
System.String |
UrlEscape(String)
Declaration
public static string UrlEscape(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input |
Returns
Type | Description |
---|---|
System.String |
Whoa2(String)
Declaration
public static void Whoa2(this string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input |
WriteVariables(String, Object[])
Declaration
public static string WriteVariables(this string input, params object[] values)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | |
System.Object[] | values |
Returns
Type | Description |
---|---|
System.String |