Class StringExtension
Extensions for string.
Inheritance
Inherited Members
Namespace: StoneCo.Framework.Extension
Assembly: cs.temp.dll.dll
Syntax
public static class StringExtension
Methods
Extract(String, String, String, Boolean, Boolean)
Extract a content between two delimiters.
Declaration
public static string Extract(this string source, string beginDelim, string endDelim, bool caseSensitive = false, bool allowMissingEndDelimiter = false)
Parameters
Type | Name | Description |
---|---|---|
System.String | source | Origin content. |
System.String | beginDelim | First string delimiter. |
System.String | endDelim | Last string delimiter. |
System.Boolean | caseSensitive | If consider uppercase or lowercase to delimiter. |
System.Boolean | allowMissingEndDelimiter | If not found endDelimin, return the value, since begindelim. |
Returns
Type | Description |
---|---|
System.String | First string between both delimiters . |
Examples
note
This is an initial sample that will be used for the following examples.
string appSettings = @"
{
""AppSettings"": {
""HostingSettings"": {
""HostAddress"": {
""Addresses"": {
""ApiAddress"": ""#{api_address}#""
}
}
}
}
}
";
Example 1 - Defining first and last Delimiters:
string result = appSettings.Extract("#{", "}#");
Returns first value founded: api_address
Example 2 - Defining first and last Delimiters, desconsidering sensitive case:
string result = appSettings.Extract("app", "\":");
Returns first value founded: Settings
Example 3 - Defining first and last Delimiters, considering sensitive case:
string result = appSettings.Extract("app", "\":", true);
Returns null
Example 4 - Defining first and last Delimiters, considering sensitive case and not allow missing end delimiter:
string appSettings = "[ApiAddress = stone.com.br]"
string result = appSettings.Extract("ApiAddress = ", "!", true, false);
Returns null
Example 5 - Defining first and last Delimiters, considering sensitive case and allow missing end delimiter:
string appSettings = "ApiAddress=stone.com.br"
string result = appSettings.Extract("ApiAddress=", "!", true, true);
Returns stone.com.br
GetStandardSpaces(String)
Get words with the standardized spaces.
If the value is empty, null, or has only spaces, the same value that was provided is returned.
Declaration
public static string GetStandardSpaces(this string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | Value with the words that will be standardized. |
Returns
Type | Description |
---|---|
System.String | Words with standard spaces. |
Examples
var value = "A sentence with spaces."
var formatedValue = value.ToStandardSpaces();
The result is A sentence with spaces.
GetWithoutSpecialCharacter(String)
Get the string with no special characters.
If the value is empty, null, or has only spaces, the same value that was provided is returned.
Declaration
public static string GetWithoutSpecialCharacter(this string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | Value that will be removed from the special characters. |
Returns
Type | Description |
---|---|
System.String | Value without special characters or value provided. |
Examples
var value = "21.000-000";
var valueWithoutSpecialCharacters = value.GetWithoutSpecialCharacter();
The result is 21000000.
IsDecimal(String)
Checks whether all characters of a string are decimal characters.
Declaration
public static bool IsDecimal(this string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | String to check. |
Returns
Type | Description |
---|---|
System.Boolean | 'true' if all characters are decimal characters. |
Examples
var stringValue = "25";
var isDecimal = stringValue.IsDecimal();
IsDecimal(String, Int32)
Checks whether all characters of a string are decimal characters.
Declaration
public static bool IsDecimal(this string value, int offset)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | String to check. |
System.Int32 | offset | Offset to start reading. |
Returns
Type | Description |
---|---|
System.Boolean | 'true' if all characters are decimal characters. |
Examples
var stringValue = "25";
var offset = 2;
var isDecimal = stringValue.IsDecimal(offset);
IsDecimal(String, Int32, Int32)
Checks whether all characters of a string are decimal characters.
Declaration
public static bool IsDecimal(this string value, int offset, int count)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | String to check. |
System.Int32 | offset | Offset to start reading. |
System.Int32 | count | Number fo characters to check. |
Returns
Type | Description |
---|---|
System.Boolean | 'true' if all characters are decimal characters. |
Examples
var stringValue = "2501";
var offset = 2;
var isDecimal = stringValue.IsDecimal(offset, 2);
Exceptions
Type | Condition |
---|---|
System.IndexOutOfRangeException | If offset is negative, an exception is thrown with message |
System.IndexOutOfRangeException | If count is negative, an exception is thrown with message |
System.IndexOutOfRangeException | If offset and count combination less than string length, an exception is thrown with message |
TryParseBase64OrNullable(String)
Try parse string base 64 value, if not parsed the null value is returned.
Declaration
public static byte[] TryParseBase64OrNullable(this string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | String value to try parse. |
Returns
Type | Description |
---|---|
System.Byte[] | Byte array or null |
Examples
var base64 = "U3RvbmUgQ28=";
var byteArray = base64.TryParseBase64OrNullable();
TryParseDateTimeOrNullable(String)
Try parse string date value, if not parsed the null value is returned.
Declaration
public static DateTime? TryParseDateTimeOrNullable(this string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | String value to try parse. |
Returns
Type | Description |
---|---|
System.Nullable<System.DateTime> | DateTime value or null. |
Examples
var date = "01/16/2018";
var dateParse = date.TryParseDateTimeOrNullable();
TryParseDecimalOrNullable(String)
Try parse string decimal value, if not parsed the null value is returned. This approach uses the invariant culture type and the numeric string can have a decimal point.
Declaration
public static decimal? TryParseDecimalOrNullable(this string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | String value to try parse. |
Returns
Type | Description |
---|---|
System.Nullable<System.Decimal> | Decimal or null |
Examples
var number = "15.02";
var numberDecimal = number.TryParseDecimalOrNullable();
TryParseEnumOrNullable<TEnum>(String)
Try parse string enum name, if not parsed the null value is returned.
Declaration
public static TEnum? TryParseEnumOrNullable<TEnum>(this string value)
where TEnum : struct
Parameters
Type | Name | Description |
---|---|---|
System.String | value | String value to try parse. |
Returns
Type | Description |
---|---|
System.Nullable<TEnum> | Enum or null |
Type Parameters
Name | Description |
---|---|
TEnum | Enum type provider. |
Examples
Brand enum used in this example.
public enum Brand
{
Visa = 1,
Master = 2,
Amex = 3,
Elo = 4,
Hiper = 5
}
Example for try parse to the Brand enum.
public class EnumTryParseSample
{
public void GetEnum()
{
var nameMaster = "Master";
var enumMaster = nameMaster.TryParseEnumOrNullable<Brand>();
}
}
Exceptions
Type | Condition |
---|---|
System.ArgumentException | If typeof TEnum is different of Enum struct, an exception is thrown with message |
TryParseIntOrNullable(String)
Try parse string int value, if not parsed the null value is returned.
Declaration
public static int? TryParseIntOrNullable(this string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | String value to try parse. |
Returns
Type | Description |
---|---|
System.Nullable<System.Int32> | Int or null |
Examples
var number = "15";
var numberInt = number.TryParseIntOrNullable();