Class StringExtensions
All String extensions.
Inherited Members
Namespace: Code.Library.Extensions
Assembly: Code.Library.dll
Syntax
public static class StringExtensions
Methods
| Improve this Doc View SourceClean(string, bool)
Clean the content by replacing special characters/HTML tags.
Declaration
public static string Clean(this string content, bool removeHtml = true)
Parameters
Returns
| Improve this Doc View SourceCleanForXss(string, params char[])
Cleans string to aid in preventing xss attacks.
Declaration
public static string CleanForXss(this string input, params char[] ignoreFromClean)
Parameters
Type | Name | Description |
---|---|---|
string | input | Input string. |
char[] | ignoreFromClean | Characters to ignore during clean. |
Returns
Type | Description |
---|---|
string | Cleaned string. |
EnsureEndsWith(string, char)
All String extensions.
Declaration
public static string EnsureEndsWith(this string input, char value)
Parameters
Returns
Type | Description |
---|---|
string |
EnsureEndsWith(string, string)
All String extensions.
Declaration
public static string EnsureEndsWith(this string input, string toEndWith)
Parameters
Returns
Type | Description |
---|---|
string |
EnsureStartsWith(string, char)
All String extensions.
Declaration
public static string EnsureStartsWith(this string input, char value)
Parameters
Returns
Type | Description |
---|---|
string |
ExceptChars(string, HashSet<char>)
All String extensions.
Declaration
public static string ExceptChars(this string str, HashSet<char> toExclude)
Parameters
Returns
Type | Description |
---|---|
string |
GetLast(string, int)
Get last N characters from a string
Declaration
public static string GetLast(this string source, int numberOfChars)
Parameters
Returns
Type | Description |
---|---|
string |
IfNullOrWhiteSpace(string, string)
Returns default value if string is null or whitespace.
Declaration
public static string IfNullOrWhiteSpace(this string str, string defaultValue)
Parameters
Returns
Type | Description |
---|---|
string | Returns default value if string is null or whitespace else return the string itself. |
IsLowerCase(char)
All String extensions.
Declaration
public static bool IsLowerCase(this char ch)
Parameters
Type | Name | Description |
---|---|---|
char | ch |
Returns
Type | Description |
---|---|
bool |
IsNumber(string, bool)
true, if the string can be parsed as Double respective Int32 Spaces are not considred.
Declaration
public static bool IsNumber(this string s, bool floatpoint)
Parameters
Type | Name | Description |
---|---|---|
string | s | input string |
bool | floatpoint | true, if Double is considered, otherwhise Int32 is considered. |
Returns
Type | Description |
---|---|
bool | true, if the string contains only digits or float-point |
IsNumberOnly(string, bool)
true, if float-point is considered
Declaration
public static bool IsNumberOnly(this string s, bool floatpoint)
Parameters
Returns
Type | Description |
---|---|
bool | true, if the string contains only digits or float-point |
IsRtl(string)
To check whether the given string is Arabic.
Declaration
public static bool IsRtl(this string input)
Parameters
Type | Name | Description |
---|---|---|
string | input |
Returns
Type | Description |
---|---|
bool | Returns True if Arabic |
IsUpperCase(char)
All String extensions.
Declaration
public static bool IsUpperCase(this char ch)
Parameters
Type | Name | Description |
---|---|---|
char | ch |
Returns
Type | Description |
---|---|
bool |
IsValidEmailAddress(string)
To check whether the string is a valid email.
Declaration
public static bool IsValidEmailAddress(this string email)
Parameters
Type | Name | Description |
---|---|---|
string |
Returns
Type | Description |
---|---|
bool |
IsValidUrl(string)
Validate if the given string is URL or not.
Declaration
public static bool IsValidUrl(this string url)
Parameters
Type | Name | Description |
---|---|---|
string | url | URL. |
Returns
Type | Description |
---|---|
bool | True or False. |
MD5(string)
generates MD5.
Declaration
public static string MD5(this string s)
Parameters
Type | Name | Description |
---|---|---|
string | s |
Returns
Type | Description |
---|---|
string |
Mask(string, int, string)
Mask the string.
Declaration
public static string Mask(this string value, int startIndex, string mask)
Parameters
Type | Name | Description |
---|---|---|
string | value | String that need to be masked |
int | startIndex | zero index indicating mask start position |
string | mask | mask that need to be applied, eg. *** |
Returns
Type | Description |
---|---|
string | Usage: "123456789".Mask(3, "") => "12389" |
Nl2Br(string)
Replace \r\n or \n by
Declaration
public static string Nl2Br(this string s)
Parameters
Type | Name | Description |
---|---|---|
string | s |
Returns
Type | Description |
---|---|
string |
OrIfNullOrWhiteSpace(string, string)
All String extensions.
Declaration
public static string OrIfNullOrWhiteSpace(this string input, string alternative)
Parameters
Returns
Type | Description |
---|---|
string |
RemoveDiacritics(string)
All String extensions.
Declaration
public static string RemoveDiacritics(this string s)
Parameters
Type | Name | Description |
---|---|---|
string | s |
Returns
Type | Description |
---|---|
string |
RemoveSpaces(string)
remove white space, not line end. Useful when parsing user input such phone, price int.Parse("1 000 000".RemoveSpaces(),.....
Declaration
public static string RemoveSpaces(this string s)
Parameters
Type | Name | Description |
---|---|---|
string | s |
Returns
Type | Description |
---|---|
string |
ReplaceLastOccurrence(string, string, string)
Replace last occurance in a string
Declaration
public static string ReplaceLastOccurrence(this string source, string find, string replace)
Parameters
Returns
Type | Description |
---|---|
string |
Reverse(string)
Reverse the string.
Declaration
public static string Reverse(this string input)
Parameters
Type | Name | Description |
---|---|---|
string | input |
Returns
Type | Description |
---|---|
string |
SanitizeURL(string)
returns "safe" URL, stripping anything outside normal charsets for URL
Declaration
public static string SanitizeURL(this string url)
Parameters
Type | Name | Description |
---|---|---|
string | url | Input URL string. |
Returns
Type | Description |
---|---|
string | Safe URL. |
StripHtml(string)
Strips all html from a string.
Declaration
public static string StripHtml(this string text)
Parameters
Type | Name | Description |
---|---|---|
string | text | The text. |
Returns
Type | Description |
---|---|
string | Returns the string without any html tags. |
ToFirstLower(string)
Returns a copy of the string with the first character converted to lowercase.
Declaration
public static string ToFirstLower(this string input)
Parameters
Type | Name | Description |
---|---|---|
string | input | The string. |
Returns
Type | Description |
---|---|
string | The converted string. |
ToFirstLower(string, CultureInfo)
Returns a copy of the string with the first character converted to lowercase using the casing rules of the specified culture.
Declaration
public static string ToFirstLower(this string input, CultureInfo culture)
Parameters
Type | Name | Description |
---|---|---|
string | input | The string. |
Culture |
culture | The culture. |
Returns
Type | Description |
---|---|
string | The converted string. |
ToFirstLowerInvariant(string)
Returns a copy of the string with the first character converted to lowercase using the casing rules of the invariant culture.
Declaration
public static string ToFirstLowerInvariant(this string input)
Parameters
Type | Name | Description |
---|---|---|
string | input | The string. |
Returns
Type | Description |
---|---|
string | The converted string. |
ToFirstUpper(string)
Returns a copy of the string with the first character converted to uppercase.
Declaration
public static string ToFirstUpper(this string input)
Parameters
Type | Name | Description |
---|---|---|
string | input | The string. |
Returns
Type | Description |
---|---|
string | The converted string. |
ToFirstUpper(string, CultureInfo)
Returns a copy of the string with the first character converted to uppercase using the casing rules of the specified culture.
Declaration
public static string ToFirstUpper(this string input, CultureInfo culture)
Parameters
Type | Name | Description |
---|---|---|
string | input | The string. |
Culture |
culture | The culture. |
Returns
Type | Description |
---|---|
string | The converted string. |
ToFirstUpperInvariant(string)
Returns a copy of the string with the first character converted to uppercase using the casing rules of the invariant culture.
Declaration
public static string ToFirstUpperInvariant(this string input)
Parameters
Type | Name | Description |
---|---|---|
string | input | The string. |
Returns
Type | Description |
---|---|
string | The converted string. |
ToFriendlyUrl(string)
The get friendly url.
Declaration
public static string ToFriendlyUrl(this string title)
Parameters
Type | Name | Description |
---|---|---|
string | title | The title. |
Returns
| Improve this Doc View SourceToNullableBool(string)
string to nullable bool.
Declaration
public static bool? ToNullableBool(this string s)
Parameters
Type | Name | Description |
---|---|---|
string | s | Input string. |
Returns
Type | Description |
---|---|
bool? | Null or bool. |
ToNullableInt32(string)
string to nullable int.
Declaration
public static int? ToNullableInt32(this string s)
Parameters
Type | Name | Description |
---|---|---|
string | s | string. |
Returns
Type | Description |
---|---|
int? | integer or null. |
TruncateAtWord(string, int, bool)
Truncate string at a word near to the limit specified. Avoid word split.
Declaration
public static string TruncateAtWord(this string input, int length, bool appendDots = false)
Parameters
Returns
Type | Description |
---|---|
string |
UppercaseWords(string)
Uppercase words. This program defines a method named UppercaseWords that is equivalent to the ucwords function in scripting languages such as PHP. The UppercaseWords method internally converts the string to a character array buffer.
Declaration
public static string UppercaseWords(string value)
Parameters
Type | Name | Description |
---|---|---|
string | value |
Returns
Type | Description |
---|---|
string |
UrlAvailable(string)
Checks if URL is valid.
Declaration
public static bool UrlAvailable(this string httpUrl)
Parameters
Type | Name | Description |
---|---|---|
string | httpUrl |
Returns
Type | Description |
---|---|
bool | true if available. |
Examples
string url = "www.codeproject.com; if( !url.UrlAvailable()) ...codeproject is not available.