Class StringExtensions
Provides a collection of extension methods for string manipulation and validation.
Inherited Members
Namespace: Innovt.Core.Utilities
Assembly: Innovt.Core.dll
Syntax
public static class StringExtensions
Methods
| Edit this page View SourceApplyQuotes(string)
Encloses a string in double quotes if it contains spaces.
Declaration
public static string ApplyQuotes(this string value)
Parameters
Type | Name | Description |
---|---|---|
string | value | The string to enclose in double quotes. |
Returns
Type | Description |
---|---|
string | The original string if it does not contain spaces; otherwise, the string enclosed in double quotes. |
ClearMask(string)
Removes specified special characters from a string.
Declaration
public static string ClearMask(this string value)
Parameters
Type | Name | Description |
---|---|---|
string | value | The string to remove special characters from. |
Returns
Type | Description |
---|---|
string | The string with specified special characters removed. |
CreateSlug(string, int)
Create a safe and SEO friendly slug from a string.
Declaration
public static string CreateSlug(this string str, int maxLength = 100)
Parameters
Type | Name | Description |
---|---|---|
string | str | |
int | maxLength |
Returns
Type | Description |
---|---|
string |
FormatCelPhone(string)
Formats a string as a cell phone number with a mask (e.g., "(00)00000-0000").
Declaration
public static string FormatCelPhone(this string celPhone)
Parameters
Type | Name | Description |
---|---|---|
string | celPhone | The string to format as a cell phone number. |
Returns
Type | Description |
---|---|
string | The formatted cell phone number string or the original string if it is null or empty. |
FormatCnpj(string)
Formats a string as a CNPJ (Cadastro Nacional da Pessoa Jurídica) number with a mask (e.g., "00.000.000/0000-00").
Declaration
public static string FormatCnpj(this string cnpj)
Parameters
Type | Name | Description |
---|---|---|
string | cnpj | The string to format as a CNPJ number. |
Returns
Type | Description |
---|---|
string | The formatted CNPJ string or null if the input is null or empty. |
FormatCoordinate(long)
You can use this method to forma DD stardand Latitude or Longitude
Declaration
public static string FormatCoordinate(this long value)
Parameters
Type | Name | Description |
---|---|---|
long | value |
Returns
Type | Description |
---|---|
string |
FormatCpf(string)
Formats a string as a CPF (Cadastro de Pessoas Físicas) number with a mask (e.g., "000.000.000-00").
Declaration
public static string FormatCpf(this string cpf)
Parameters
Type | Name | Description |
---|---|---|
string | cpf | The string to format as a CPF. |
Returns
Type | Description |
---|---|
string | The formatted CPF string or the original string if it is null or empty. |
FormatPhoneNumber(string)
Formats a string as a phone number with a mask (e.g., "(00)000-0000").
Declaration
public static string FormatPhoneNumber(this string phoneNumber)
Parameters
Type | Name | Description |
---|---|---|
string | phoneNumber | The string to format as a phone number. |
Returns
Type | Description |
---|---|
string | The formatted phone number string or the original string if it is null or empty. |
FormatZipCode(string)
Formats a string as a zip code with a mask (e.g., "00000-000").
Declaration
public static string FormatZipCode(this string cep)
Parameters
Type | Name | Description |
---|---|---|
string | cep | The string to format as a zip code. |
Returns
Type | Description |
---|---|
string | The formatted zip code string or the original string if it is null or empty. |
FromBase64(string, Encoding)
Decode your string from Base64
Declaration
public static string FromBase64(this string toDecode, Encoding encoding = null)
Parameters
Type | Name | Description |
---|---|---|
string | toDecode | The string to decode |
Encoding | encoding | Optional and will be ASCII if null |
Returns
Type | Description |
---|---|
string |
GetValueOrDefault(string, string)
Gets the value of a string or a default value if the string is null or empty.
Declaration
public static string GetValueOrDefault(this string str, string defaultValue = null)
Parameters
Type | Name | Description |
---|---|---|
string | str | The string to check. |
string | defaultValue | The default value to return if the string is null or empty (optional). |
Returns
Type | Description |
---|---|
string | The original string if it is not null or empty; otherwise, the default value if provided; otherwise, an empty string. |
HasSpecialCharacter(string)
Check is a string has special characters
Declaration
public static bool HasSpecialCharacter(this string str)
Parameters
Type | Name | Description |
---|---|---|
string | str |
Returns
Type | Description |
---|---|
bool |
IsCnpj(string)
Determines whether the string is a valid CNPJ (Cadastro Nacional da Pessoa Jurídica) number.
Declaration
public static bool IsCnpj(this string cnpj)
Parameters
Type | Name | Description |
---|---|---|
string | cnpj | The string to validate as a CNPJ number. |
Returns
Type | Description |
---|---|
bool | True if the string is a valid CNPJ number; otherwise, false. |
IsCpf(string)
Determines whether the string is a valid CPF (Cadastro de Pessoas Físicas) number.
Declaration
public static bool IsCpf(this string value)
Parameters
Type | Name | Description |
---|---|---|
string | value | The string to validate as a CPF number. |
Returns
Type | Description |
---|---|
bool | True if the string is a valid CPF number; otherwise, false. |
IsEmail(string)
Determines whether the string represents a valid email address.
Declaration
public static bool IsEmail(this string value)
Parameters
Type | Name | Description |
---|---|---|
string | value | The string to validate as an email address. |
Returns
Type | Description |
---|---|
bool | True if the string is a valid email address; otherwise, false. |
IsEmpty(Guid)
Determines whether a Guid is empty (all zeros).
Declaration
public static bool IsEmpty(this Guid yourGuid)
Parameters
Type | Name | Description |
---|---|---|
Guid | yourGuid | The Guid to check. |
Returns
Type | Description |
---|---|
bool | True if the Guid is empty; otherwise, false. |
IsNotEmpty(Guid)
Determines whether a Guid is not empty (contains non-zero values).
Declaration
public static bool IsNotEmpty(this Guid yourGuid)
Parameters
Type | Name | Description |
---|---|---|
Guid | yourGuid | The Guid to check. |
Returns
Type | Description |
---|---|
bool | True if the Guid is not empty; otherwise, false. |
IsNotNullOrEmpty(Guid?)
Determines whether a nullable Guid is not null or empty.
Declaration
public static bool IsNotNullOrEmpty(this Guid? yourGuid)
Parameters
Type | Name | Description |
---|---|---|
Guid? | yourGuid | The nullable Guid to check. |
Returns
Type | Description |
---|---|
bool | True if the nullable Guid is not null or empty; otherwise, false. |
IsNotNullOrEmpty(string)
Determines whether a string is not null, empty, or consists only of white-space characters.
Declaration
public static bool IsNotNullOrEmpty(this string str)
Parameters
Type | Name | Description |
---|---|---|
string | str | The string to check. |
Returns
Type | Description |
---|---|
bool | True if the string is not null, not empty, and not consisting only of white-space characters; otherwise, false. |
IsNullOrEmpty(Guid?)
Determines whether a nullable Guid is null or empty.
Declaration
public static bool IsNullOrEmpty(this Guid? yourGuid)
Parameters
Type | Name | Description |
---|---|---|
Guid? | yourGuid | The nullable Guid to check. |
Returns
Type | Description |
---|---|
bool | True if the nullable Guid is null or empty; otherwise, false. |
IsNullOrEmpty(string)
Determines whether a string is null, empty, or consists only of white-space characters.
Declaration
public static bool IsNullOrEmpty(this string str)
Parameters
Type | Name | Description |
---|---|---|
string | str | The string to check. |
Returns
Type | Description |
---|---|
bool | True if the string is null, empty, or consists only of white-space characters; otherwise, false. |
IsNumber(string)
Determines whether the string consists of numeric characters only.
Declaration
public static bool IsNumber(this string value)
Parameters
Type | Name | Description |
---|---|---|
string | value | The string to validate as a number. |
Returns
Type | Description |
---|---|
bool | True if the string consists of numeric characters only; otherwise, false. |
MaskEmail(string)
Will mask an email using this format mich***@gmail.com
Declaration
public static string MaskEmail(this string email)
Parameters
Type | Name | Description |
---|---|---|
string | The email that you want to mask |
Returns
Type | Description |
---|---|
string | mich***@gmail.com |
NormalizeText(string)
Normalizes a string by removing accents, special characters, and trimming whitespace.
Declaration
public static string NormalizeText(this string str)
Parameters
Type | Name | Description |
---|---|---|
string | str | The string to normalize. |
Returns
Type | Description |
---|---|
string | The normalized string. |
OnlyNumber(string)
Removes all non-numeric characters from a string.
Declaration
public static string OnlyNumber(this string value)
Parameters
Type | Name | Description |
---|---|---|
string | value | The string to remove non-numeric characters from. |
Returns
Type | Description |
---|---|
string | The string containing only numeric characters. |
RemoveAccents(string)
Removes accents from a string.
Declaration
public static string RemoveAccents(this string str)
Parameters
Type | Name | Description |
---|---|---|
string | str | The string to remove accents from. |
Returns
Type | Description |
---|---|
string | The string with accents removed. |
RemoveSpecialCharacter(string)
Removes all special characters from a string.
Declaration
public static string RemoveSpecialCharacter(this string str)
Parameters
Type | Name | Description |
---|---|---|
string | str | The string to remove special characters from. |
Returns
Type | Description |
---|---|
string | The string with special characters replaced by spaces. |
SmartTrim(string)
Trims leading and trailing white-space characters from a string.
Declaration
public static string SmartTrim(this string str)
Parameters
Type | Name | Description |
---|---|---|
string | str | The string to trim. |
Returns
Type | Description |
---|---|
string | The trimmed string or null if the input string is null. |
ToBase64(string, Encoding)
Encode your string to Base64
Declaration
public static string ToBase64(this string toEncode, Encoding encoding = null)
Parameters
Type | Name | Description |
---|---|---|
string | toEncode | The String to encode |
Encoding | encoding | Optional and will be ASCII if null |
Returns
Type | Description |
---|---|
string |
ToBoolean(string, bool)
Converts a string to a boolean, using a specified default value if the string is null or empty.
Declaration
public static bool ToBoolean(this string str, bool defaultValue = false)
Parameters
Type | Name | Description |
---|---|---|
string | str | The string to convert to a boolean. |
bool | defaultValue | The default value to return if the string is null or empty (optional). |
Returns
Type | Description |
---|---|
bool | The boolean value of the string, or the default value if the string is null or empty. |
ToBoolean(string, bool?)
Converts a string to a boolean value, using a specified default value if the string is null or empty.
Declaration
public static bool? ToBoolean(this string str, bool? defaultValue = null)
Parameters
Type | Name | Description |
---|---|---|
string | str | The string to convert to a boolean. |
bool? | defaultValue | The default value to return if the string is null or empty (optional). |
Returns
Type | Description |
---|---|
bool? | The boolean value of the string if it represents a valid boolean; otherwise, the default value. |
ToCamelCase(string)
Converts a string to camel case by applying title case.
Declaration
public static string ToCamelCase(this string str)
Parameters
Type | Name | Description |
---|---|---|
string | str | The string to convert to camel case. |
Returns
Type | Description |
---|---|
string | The string in camel case format. |
ToDateTime(string, DateTime)
Converts a string to a DateTime, using a specified default value if the string is null or empty.
Declaration
public static DateTime ToDateTime(this string str, DateTime defaultValue = default)
Parameters
Type | Name | Description |
---|---|---|
string | str | The string to convert to a DateTime. |
DateTime | defaultValue | The default value to return if the string is null or empty (optional). |
Returns
Type | Description |
---|---|
DateTime | The DateTime value of the string, or the default value if the string is null or empty. |
ToDateTime(string, DateTime?)
Converts a string to a nullable DateTime, using a specified default value if the string is null or empty.
Declaration
public static DateTime? ToDateTime(this string str, DateTime? defaultValue = null)
Parameters
Type | Name | Description |
---|---|---|
string | str | The string to convert to a nullable DateTime. |
DateTime? | defaultValue | The default value to return if the string is null or empty (optional). |
Returns
Type | Description |
---|---|
DateTime? | The nullable DateTime value of the string, or the default value if the string is null or empty. |
ToDateTimeOffset(string, DateTimeOffset)
Converts a string to a DateTimeOffset, using a specified default value if the string is null or empty.
Declaration
public static DateTimeOffset ToDateTimeOffset(this string str, DateTimeOffset defaultValue = default)
Parameters
Type | Name | Description |
---|---|---|
string | str | The string to convert to a DateTimeOffset. |
DateTimeOffset | defaultValue | The default value to return if the string is null or empty (optional). |
Returns
Type | Description |
---|---|
DateTimeOffset | The DateTimeOffset value of the string, or the default value if the string is null or empty. |
ToDateTimeOffset(string, DateTimeOffset?)
Converts a string to a nullable DateTimeOffset, using a specified default value if the string is null or empty.
Declaration
public static DateTimeOffset? ToDateTimeOffset(this string str, DateTimeOffset? defaultValue = null)
Parameters
Type | Name | Description |
---|---|---|
string | str | The string to convert to a nullable DateTimeOffset. |
DateTimeOffset? | defaultValue | The default value to return if the string is null or empty (optional). |
Returns
Type | Description |
---|---|
DateTimeOffset? | The nullable DateTimeOffset value of the string, or the default value if the string is null or empty. |
ToDecimal(string, decimal)
Converts a string to a decimal, using a specified default value if the string is null or empty.
Declaration
public static decimal ToDecimal(this string str, decimal defaultValue = 0)
Parameters
Type | Name | Description |
---|---|---|
string | str | The string to convert to a decimal. |
decimal | defaultValue | The default value to return if the string is null or empty (optional). |
Returns
Type | Description |
---|---|
decimal | The decimal value of the string, or the default value if the string is null or empty. |
ToDecimal(string, decimal?)
Converts a string to a nullable decimal, using a specified default value if the string is null or empty.
Declaration
public static decimal? ToDecimal(this string str, decimal? defaultValue = null)
Parameters
Type | Name | Description |
---|---|---|
string | str | The string to convert to a nullable decimal. |
decimal? | defaultValue | The default value to return if the string is null or empty (optional). |
Returns
Type | Description |
---|---|
decimal? | The nullable decimal value of the string, or the default value if the string is null or empty. |
ToDouble(string, double)
Converts a string to a double, using a specified default value if the string is null or empty.
Declaration
public static double ToDouble(this string str, double defaultValue = 0)
Parameters
Type | Name | Description |
---|---|---|
string | str | The string to convert to a double. |
double | defaultValue | The default value to return if the string is null or empty (optional). |
Returns
Type | Description |
---|---|
double | The double value of the string, or the default value if the string is null or empty. |
ToDouble(string, double?)
Converts a string to a nullable double, using a specified default value if the string is null or empty.
Declaration
public static double? ToDouble(this string str, double? defaultValue = null)
Parameters
Type | Name | Description |
---|---|---|
string | str | The string to convert to a nullable double. |
double? | defaultValue | The default value to return if the string is null or empty (optional). |
Returns
Type | Description |
---|---|
double? | The nullable double value of the string, or the default value if the string is null or empty. |
ToFloat(string, float?)
Converts a string to a nullable float, using a specified default value if the string is null or empty.
Declaration
public static float? ToFloat(this string str, float? defaultValue = null)
Parameters
Type | Name | Description |
---|---|---|
string | str | The string to convert to a nullable float. |
float? | defaultValue | The default value to return if the string is null or empty (optional). |
Returns
Type | Description |
---|---|
float? | The nullable float value of the string, or the default value if the string is null or empty. |
ToFloat(string, float)
Converts a string to a float, using a specified default value if the string is null or empty.
Declaration
public static float ToFloat(this string str, float defaultValue = 0)
Parameters
Type | Name | Description |
---|---|---|
string | str | The string to convert to a float. |
float | defaultValue | The default value to return if the string is null or empty (optional). |
Returns
Type | Description |
---|---|
float | The float value of the string, or the default value if the string is null or empty. |
ToGuid(string)
Converts a string to a Guid.
Declaration
public static Guid ToGuid(this string str)
Parameters
Type | Name | Description |
---|---|---|
string | str | The string to convert to a Guid. |
Returns
Type | Description |
---|---|
Guid | The Guid value of the string. |
ToGuidOrEmpty(string)
Converts a string to a Guid or returns Guid.Empty if the conversion fails.
Declaration
public static Guid? ToGuidOrEmpty(this string str)
Parameters
Type | Name | Description |
---|---|---|
string | str | The string to convert to a Guid. |
Returns
Type | Description |
---|---|
Guid? | The Guid value of the string or Guid.Empty if the conversion fails. |
ToInt(string, int)
Converts a string to an integer, using a specified default value if the string is null or empty.
Declaration
public static int ToInt(this string str, int defaultValue = 0)
Parameters
Type | Name | Description |
---|---|---|
string | str | The string to convert to an integer. |
int | defaultValue | The default value to return if the string is null or empty (optional). |
Returns
Type | Description |
---|---|
int | The integer value of the string, or the default value if the string is null or empty. |
ToInt(string, int?)
Converts a string to a nullable integer, using a specified default value if the string is null or empty.
Declaration
public static int? ToInt(this string str, int? defaultValue = null)
Parameters
Type | Name | Description |
---|---|---|
string | str | The string to convert to a nullable integer. |
int? | defaultValue | The default value to return if the string is null or empty (optional). |
Returns
Type | Description |
---|---|
int? | The nullable integer value of the string, or the default value if the string is null or empty. |
ToList(string, char)
Converts a delimited string into a list of strings using the specified separator character.
Declaration
public static List<string> ToList(this string str, char separator)
Parameters
Type | Name | Description |
---|---|---|
string | str | The delimited string to convert. |
char | separator | The character used to separate values in the string. |
Returns
Type | Description |
---|---|
List<string> | A list of strings containing the individual values from the delimited string. |
ToTitleCase(string)
Converts a string to title case using the current culture's rules.
Declaration
public static string ToTitleCase(this string str)
Parameters
Type | Name | Description |
---|---|---|
string | str | The string to convert to title case. |
Returns
Type | Description |
---|---|
string | The string in title case format. |
ToUpperFirstLetter(string)
Converts the first letter of a string to uppercase.
Declaration
public static string ToUpperFirstLetter(this string value)
Parameters
Type | Name | Description |
---|---|---|
string | value | The string to convert. |
Returns
Type | Description |
---|---|
string | The string with its first letter in uppercase or the original string if it is null or empty. |
Truncate(string, int)
Truncates the string to a specified maximum length.
Declaration
public static string Truncate(this string value, int maxLength)
Parameters
Type | Name | Description |
---|---|---|
string | value | The string to truncate. |
int | maxLength | The maximum length of the truncated string. |
Returns
Type | Description |
---|---|
string | The truncated string, or the original string if its length is less than or equal to the specified maximum length. |
UrlDecode(string)
Decodes a URL-encoded string using UTF-8 encoding.
Declaration
public static string UrlDecode(this string str)
Parameters
Type | Name | Description |
---|---|---|
string | str | The URL-encoded string to decode. |
Returns
Type | Description |
---|---|
string | The decoded string, or the original string if it is null or empty. |
UrlEncode(string)
Encodes a string for safe use in a URL, using UTF-8 encoding.
Declaration
public static string UrlEncode(this string str)
Parameters
Type | Name | Description |
---|---|---|
string | str | The string to encode. |
Returns
Type | Description |
---|---|
string | The URL-encoded string, or the original string if it is null or empty. |