Skip to content

GERD0GDU/Common

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ioCode.Common C# Library

A library that provides extended methods for the practical use of conversions.

Warning This sample requires Microsoft Visual Studio Community 2019 .NET Framework 4.5 or a later version (any SKU).

To get a copy of Visual Studio, go to Visual Studio Downloads.

Classes

ioCode.Common library contains the following classes.

Class Description
ConversionExtensions Converts the string representation of a number to sbyte, short, int, long, decimal, double, float, byte, ushort, uint, long.
Extensions Provides extended methods for general uses.
Macro A class that outputs strings by compiling specified custom date-time formats.
MathExtensions It provides extended methods for variables sbyte, short, int, long, decimal, double, float, byte, ushort, uint, long, TimeSpan, DateTime that enforce minimum and maximum range.

ioCode.Common.ConversionExtensions

Warning

  • All extended methods used for conversion operations do not throw any error exceptions.
  • For this reason, there is no guarantee of the method result.
  • The extended method returns '0' (zero) if the conversion fails.
  • When the conversion is successful, it returns a value in the minimum and maximum range of the converted type.

Methods

Method Description
ToDecimal(String, IFormatProvider) Converts the string representation of a number to its System.Decimal equivalent using the specified culture-specific format information.
ToDecimal(String) Converts the string representation of a number to its System.Decimal equivalent.
ToDouble(String, IFormatProvider) Converts the string representation of a number in a specified culture-specific format to its double-precision floating-point number equivalent.
ToDouble(String) Converts the string representation of a number to its double-precision floating-point number equivalent.
ToSingle(String, IFormatProvider) Converts the string representation of a number in a specified culture-specific format to its single-precision floating-point number equivalent.
ToSingle(String) Converts the string representation of a number to its single-precision floating-point number equivalent.
ToSByte(String, IFormatProvider) Converts the string representation of a number in a specified culture-specific format to its 8-bit signed integer equivalent.
ToSByte(String) Converts the string representation of a number to its 8-bit signed integer equivalent.
ToInt16(String, IFormatProvider) Converts the string representation of a number in a specified culture-specific format to its 16-bit signed integer equivalent.
ToInt16(String) Converts the string representation of a number to its 16-bit signed integer equivalent.
ToInt32(String, IFormatProvider) Converts the string representation of a number in a specified culture-specific format to its 32-bit signed integer equivalent.
ToInt32(String) Converts the string representation of a number to its 32-bit signed integer equivalent.
ToInt64(String, IFormatProvider) Converts the string representation of a number in a specified culture-specific format to its 64-bit signed integer equivalent.
ToInt64(String) Converts the string representation of a number to its 64-bit signed integer equivalent.
ToByte(String, IFormatProvider) Converts the string representation of a number in a specified culture-specific format to its System.Byte equivalent.
ToByte(String) Converts the string representation of a number to its System.Byte equivalent.
ToUInt16(String, IFormatProvider) Converts the string representation of a number in a specified culture-specific format to its 16-bit unsigned integer equivalent.
ToUInt16(String) Converts the string representation of a number to its 16-bit unsigned integer equivalent.
ToUInt32(String, IFormatProvider) Converts the string representation of a number in a specified culture-specific format to its 32-bit unsigned integer equivalent.
ToUInt32(String) Converts the string representation of a number to its 32-bit unsigned integer equivalent.
ToUInt64(String, IFormatProvider) Converts the string representation of a number in a specified culture-specific format to its 64-bit unsigned integer equivalent.
ToUInt64(String) Converts the string representation of a number to its 64-bit unsigned integer equivalent.
ToHexString(Byte, String) Converts an 8-bit unsigned integer to a hexadecimal string according to the format.
ToHexString(Byte) Converts an 8-bit unsigned integer to a hexadecimal string.
ToHexString(UInt16, String) Converts an 16-bit unsigned integer to a hexadecimal string according to the format.
ToHexString(UInt16) Converts an 16-bit unsigned integer to a hexadecimal string.
ToHexString(UInt32, String) Converts an 32-bit unsigned integer to a hexadecimal string according to the format.
ToHexString(UInt32) Converts an 32-bit unsigned integer to a hexadecimal string.
ToHexString(UInt64, String) Converts an 64-bit unsigned integer to a hexadecimal string according to the format.
ToHexString(UInt64) Converts an 64-bit unsigned integer to a hexadecimal string.

ToDecimal Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Overloads

Method Description
ToDecimal(String, IFormatProvider) Converts the string representation of a number to its System.Decimal equivalent using the specified culture-specific format information.
ToDecimal(String) Converts the string representation of a number to its System.Decimal equivalent.

Parameters

s: The string representation of the number to convert.
provider: An System.IFormatProvider that supplies culture-specific parsing information about s.

Return

Decimal
The System.Decimal number equivalent to the number contained in s as specified by provider.

Examples

System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
decimal decVal = "1.9876E+9".ToDecimal();
System.Console.WriteLine("\"1.9876E+9\" --> {0}", decVal);
decVal = "79228162514264337593543950334".ToDecimal();
System.Console.WriteLine("\"79228162514264337593543950334\" --> {0}", decVal);
decVal = "0x123456789ABCDEF0".ToDecimal();
System.Console.WriteLine("\"0x123456789ABCDEF0\" --> {0} (0x{1:X16})", decVal, (ulong)decVal);
decVal = "&H9ABCDEF012345678".ToDecimal();
System.Console.WriteLine("\"&H9ABCDEF012345678\" --> {0} (0x{1:X16})", decVal, (ulong)decVal);

Console output will be as follows.

"1.9876E+9" --> 1987600000
"79228162514264337593543950334" --> 79228162514264337593543950334
"0x123456789ABCDEF0" --> 1311768467463790320 (0x123456789ABCDEF0)
"&H9ABCDEF012345678" --> 11150031900141442680 (0x9ABCDEF012345678)

ToDouble Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Overloads

Method Description
ToDouble(String, IFormatProvider) Converts the string representation of a number in a specified culture-specific format to its double-precision floating-point number equivalent.
ToDouble(String) Converts the string representation of a number to its double-precision floating-point number equivalent.

Parameters

s: The string representation of the number to convert.
provider: An System.IFormatProvider that supplies culture-specific parsing information about s.

Return

Double
The System.Double number equivalent to the number contained in s as specified by provider.

Examples

// change current culture
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
double dblVal = "1.9876E+9".ToDouble();
System.Console.WriteLine("\"1.9876E+9\" --> {0}", dblVal);
dblVal = "79228162514264337593543950334".ToDouble();
System.Console.WriteLine("\"79228162514264337593543950334\" --> {0}", dblVal);
dblVal = "0x123456789ABCDEF0".ToDouble();
System.Console.WriteLine("\"0x123456789ABCDEF0\" --> {0} (0x{1:X16})", dblVal, (ulong)dblVal);
dblVal = "&H9ABCDEF012345678".ToDouble();
System.Console.WriteLine("\"&H9ABCDEF012345678\" --> {0} (0x{1:X16})", dblVal, (ulong)dblVal);

Console output will be as follows.

"1.9876E+9" --> 1987600000
"79228162514264337593543950334" --> 7.92281625142643E+28
"0x123456789ABCDEF0" --> 1.31176846746379E+18 (0x123456789ABCDF00)
"&H9ABCDEF012345678" --> 1.11500319001414E+19 (0x9ABCDEF012345800)

ToSingle Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Overloads

Method Description
ToSingle(String, IFormatProvider) Converts the string representation of a number in a specified culture-specific format to its single-precision floating-point number equivalent.
ToSingle(String) Converts the string representation of a number to its single-precision floating-point number equivalent.

Parameters

s: The string representation of the number to convert.
provider: An System.IFormatProvider that supplies culture-specific parsing information about s.

Return

Single
The System.Single number equivalent to the number contained in s as specified by provider.

ToSByte Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Overloads

Method Description
ToSByte(String, IFormatProvider) Converts the string representation of a number in a specified culture-specific format to its 8-bit signed integer equivalent.
ToSByte(String) Converts the string representation of a number to its 8-bit signed integer equivalent.

Parameters

s: The string representation of the number to convert.
provider: An System.IFormatProvider that supplies culture-specific parsing information about s.

Return

SByte
The System.SByte number equivalent to the number contained in s as specified by provider.

ToInt16 Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Overloads

Method Description
ToInt16(String, IFormatProvider) Converts the string representation of a number in a specified culture-specific format to its 16-bit signed integer equivalent.
ToInt16(String) Converts the string representation of a number to its 16-bit signed integer equivalent.

Parameters

s: The string representation of the number to convert.
provider: An System.IFormatProvider that supplies culture-specific parsing information about s.

Return

Int16
The System.Int16 number equivalent to the number contained in s as specified by provider.

ToInt32 Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Overloads

Method Description
ToInt32(String, IFormatProvider) Converts the string representation of a number in a specified culture-specific format to its 32-bit signed integer equivalent.
ToInt32(String) Converts the string representation of a number to its 32-bit signed integer equivalent.

Parameters

s: The string representation of the number to convert.
provider: An System.IFormatProvider that supplies culture-specific parsing information about s.

Return

Int32
The System.Int32 number equivalent to the number contained in s as specified by provider.

ToInt64 Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Overloads

Method Description
ToInt64(String, IFormatProvider) Converts the string representation of a number in a specified culture-specific format to its 64-bit signed integer equivalent.
ToInt64(String) Converts the string representation of a number to its 64-bit signed integer equivalent.

Parameters

s: The string representation of the number to convert.
provider: An System.IFormatProvider that supplies culture-specific parsing information about s.

Return

Int64
The System.Int64 number equivalent to the number contained in s as specified by provider.

ToByte Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Overloads

Method Description
ToByte(String, IFormatProvider) Converts the string representation of a number in a specified culture-specific format to its System.Byte equivalent.
ToByte(String) Converts the string representation of a number to its System.Byte equivalent.

Parameters

s: The string representation of the number to convert.
provider: An System.IFormatProvider that supplies culture-specific parsing information about s.

Return

Byte
The System.Byte number equivalent to the number contained in s as specified by provider.

ToUInt16 Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Overloads

Method Description
ToUInt16(String, IFormatProvider) Converts the string representation of a number in a specified culture-specific format to its 16-bit unsigned integer equivalent.
ToUInt16(String) Converts the string representation of a number to its 16-bit unsigned integer equivalent.

Parameters

s: The string representation of the number to convert.
provider: An System.IFormatProvider that supplies culture-specific parsing information about s.

Return

UInt16
The System.UInt16 number equivalent to the number contained in s as specified by provider.

ToUInt32 Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Overloads

Method Description
ToUInt32(String, IFormatProvider) Converts the string representation of a number in a specified culture-specific format to its 32-bit unsigned integer equivalent.
ToUInt32(String) Converts the string representation of a number to its 32-bit unsigned integer equivalent.

Parameters

s: The string representation of the number to convert.
provider: An System.IFormatProvider that supplies culture-specific parsing information about s.

Return

UInt32
The System.UInt32 number equivalent to the number contained in s as specified by provider.

ToUInt64 Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Overloads

Method Description
ToUInt64(IFormatProvider provider) Converts the string representation of a number in a specified culture-specific format to its 64-bit unsigned integer equivalent.
ToUInt64() Converts the string representation of a number to its 64-bit unsigned integer equivalent.

Parameters

s: The string representation of the number to convert.
provider: An System.IFormatProvider that supplies culture-specific parsing information about s.

Return

UInt64
The System.UInt64 number equivalent to the number contained in s as specified by provider.

ToHexString Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Extended method that converts byte, ushort, uint or ulong types to hexadecimal strings.

Overloads

Method Description
ToHexString(Byte, String) Converts an 8-bit unsigned integer to a hexadecimal string according to the format.
ToHexString(Byte) Converts an 8-bit unsigned integer to a hexadecimal string.
ToHexString(UInt16, String) Converts an 16-bit unsigned integer to a hexadecimal string according to the format.
ToHexString(UInt16) Converts an 16-bit unsigned integer to a hexadecimal string.
ToHexString(UInt32, String) Converts an 32-bit unsigned integer to a hexadecimal string according to the format.
ToHexString(UInt32) Converts an 32-bit unsigned integer to a hexadecimal string.
ToHexString(UInt64, String) Converts an 64-bit unsigned integer to a hexadecimal string according to the format.
ToHexString(UInt64) Converts an 64-bit unsigned integer to a hexadecimal string.

Parameters

n: A byte, ushort, uint or ulong that represents the number to convert.
format: A string value in the form of "0" or "H" can be specified.

Return

String
Returns a hexadecimal string value based on the format value.

Examples

System.Console.WriteLine("(ulong.MaxValue >> 9).ToHexString() --> {0}", (ulong.MaxValue >> 9).ToHexString());
System.Console.WriteLine("(ulong.MaxValue >> 9).ToHexString(\"0\") --> {0}", (ulong.MaxValue >> 9).ToHexString("0"));
System.Console.WriteLine("(ulong.MaxValue >> 9).ToHexString(\"H\") --> {0}", (ulong.MaxValue >> 9).ToHexString("H"));

Console output will be as follows.

(ulong.MaxValue >> 9).ToHexString() --> 7FFFFFFFFFFFFF
(ulong.MaxValue >> 9).ToHexString("0") --> 0x007FFFFFFFFFFFFF
(ulong.MaxValue >> 9).ToHexString("H") --> &H007FFFFFFFFFFFFF

ioCode.Common.Extensions

Provides extended methods for general uses.

Methods

Method Description
IsNull<T>(T) Tests whether any object is null. Same as "obj is null".
IsNotNull<T>(T) Tests whether any object is not null. Same as "!(obj is null)".
IsNullOrEmpty(IEnumerable) Tests whether any object derived from the "System.Collections.IEnumerable" interface is null or empty.
ToSafeString(Object) It's like ToString() but doesn't throw an exception even if the object is null.
Left(String, Int32) Returns a string of the specified length from the left of the string. It's like "System.String.Substring" but it doesn't throw an exception.
Right(String, Int32) Returns a string of the specified length from the rigth of the string.
Mid(String, Int32, Int32) Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length.
RemoveNewLines(String) Replace '\r\n' characters in string with spaces.
Replace(String, String[], String[], StringComparison) A comparison option is provided for replacement operations.
Replace(String, String[], String[], bool) A case sensitivity option is provided for replacement operations.
Replace(String, String[], String[]) It does a case sensitive replacement.
ToMessages(Exception) Returns all exception messages. Including internal exceptions.
SafeClone<T>(T) It is like the 'Clone' method. It will not throw an exception for an object that is 'null'.
IndexOf<T>(IEnumerable, T) Searches for the specified object and returns the index of the first occurrence within the entire one-dimensional IEnumerable.

IsNull<T> Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Tests whether any object is null. Same as "obj is null".

public static bool IsNull<T>(this T obj) where T : class

Examples

string s = null;
System.Console.WriteLine("string s = null;");
System.Console.WriteLine(">> s.IsNull() --> {0}\r\n", s.IsNull());
s = "";
System.Console.WriteLine("s = \"\";");
System.Console.WriteLine(">> s.IsNull() --> {0}\r\n", s.IsNull());
s = "blabla";
System.Console.WriteLine("s = \"blabla\";");
System.Console.WriteLine(">> s.IsNull() --> {0}\r\n", s.IsNull());

Console output will be as follows.

string s = null;
>> s.IsNull() --> True

s = "";
>> s.IsNull() --> False

s = "blabla";
>> s.IsNull() --> False

IsNotNull<T> Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Tests whether any object is not null. Same as "!(obj is null)".

public static bool IsNotNull<T>(this T obj) where T : class

Examples

string s = null;
System.Console.WriteLine("string s = null;");
System.Console.WriteLine(">> s.IsNotNull() --> {0}\r\n", s.IsNotNull());
s = "";
System.Console.WriteLine("s = \"\";");
System.Console.WriteLine(">> s.IsNotNull() --> {0}\r\n", s.IsNotNull());
s = "blabla";
System.Console.WriteLine("s = \"blabla\";");
System.Console.WriteLine(">> s.IsNotNull() --> {0}\r\n", s.IsNotNull());

Console output will be as follows.

string s = null;
>> s.IsNotNull() --> False

s = "";
>> s.IsNotNull() --> True

s = "blabla";
>> s.IsNotNull() --> True

IsNullOrEmpty Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Tests whether any object derived from the "System.Collections.IEnumerable" interface is null or empty.

public static bool IsNullOrEmpty(this System.Collections.IEnumerable items)

Examples

string s = "";
System.Console.WriteLine("string s = \"\";");
System.Console.WriteLine(">> s.IsNullOrEmpty() --> {0}\r\n", s.IsNullOrEmpty());
s = "blabla";
System.Console.WriteLine("s = \"blabla\";");
System.Console.WriteLine(">> s.IsNullOrEmpty() --> {0}\r\n", s.IsNullOrEmpty());
object[] objArray = new object[0];
System.Console.WriteLine("object[] objArray = new object[0];");
System.Console.WriteLine(">> objArray.IsNullOrEmpty() --> {0}\r\n", objArray.IsNullOrEmpty());
objArray = new object[] { null };
System.Console.WriteLine("objArray = new object[] { null };");
System.Console.WriteLine(">> objArray.IsNullOrEmpty() --> {0}\r\n", objArray.IsNullOrEmpty());

Console output will be as follows.

s = "";
>> s.IsNullOrEmpty() --> True

s = "blabla";
>> s.IsNullOrEmpty() --> False

objArray = new object[0];
>> objArray.IsNullOrEmpty() --> True

objArray = new object[] { null };
>> objArray.IsNullOrEmpty() --> False

ToSafeString Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

It's like ToString() but doesn't throw an exception even if the object is null.

public static string ToSafeString(this object obj)

Examples

string s = null;
System.Console.WriteLine("string s = null;");
System.Console.WriteLine(">> s.ToSafeString() --> {0}\r\n", s.ToSafeString() ?? "null");
s = "";
System.Console.WriteLine("s = \"\";");
System.Console.WriteLine(">> s.ToSafeString() --> \"{0}\"\r\n", s.ToSafeString());
s = "blabla";
System.Console.WriteLine("s = \"blabla\";");
System.Console.WriteLine(">> s.ToSafeString() --> \"{0}\"\r\n", s.ToSafeString());

Console output will be as follows.

string s = null;
>> s.ToSafeString() --> null

s = "";
>> s.ToSafeString() --> ""

s = "blabla";
>> s.ToSafeString() --> "blabla"

Left Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Returns a string of the specified length from the left of the string. It's like "System.String.Substring" but it doesn't throw an exception.

public static string Left(this string s, int length)

Examples

string s = null;
System.Console.WriteLine("string s = null;");
System.Console.WriteLine(">> s.Left(3) --> {0}\r\n", s.Left(3) ?? "null");
s = "";
System.Console.WriteLine("s = \"\";");
System.Console.WriteLine(">> s.Left(3) --> \"{0}\"\r\n", s.Left(3));
s = "blabla";
System.Console.WriteLine("s = \"blabla\";");
System.Console.WriteLine(">> s.Left(3) --> \"{0}\"\r\n", s.Left(3));

Console output will be as follows.

string s = null;
>> s.Left(3) --> null

s = "";
>> s.Left(3) --> ""

s = "blabla";
>> s.Left(3) --> "bla"

Right Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Returns a string of the specified length from the rigth of the string.

public static string Right(this string s, int length)

Examples

string s = null;
System.Console.WriteLine("string s = null;");
System.Console.WriteLine(">> s.Right(3) --> {0}\r\n", s.Right(3) ?? "null");
s = "";
System.Console.WriteLine("s = \"\";");
System.Console.WriteLine(">> s.Right(3) --> \"{0}\"\r\n", s.Right(3));
s = "abcdefg";
System.Console.WriteLine("s = \"abcdefg\";");
System.Console.WriteLine(">> s.Right(3) --> \"{0}\"\r\n", s.Right(3));

Console output will be as follows.

string s = null;
>> s.Right(3) --> null

s = "";
>> s.Right(3) --> ""

s = "abcdefg";
>> s.Right(3) --> "efg"

Mid Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length.

public static string Mid(this string s, int startIndex, int length)

Examples

string s = null;
System.Console.WriteLine("string s = null;");
System.Console.WriteLine(">> s.Mid(-1, 4) --> {0}\r\n", s.Mid(-1, 4) ?? "null");
s = "";
System.Console.WriteLine("s = \"\"; // Len:0");
System.Console.WriteLine(">> s.Mid(-1, 4) --> \"{0}\"", s.Mid(-1, 4));
System.Console.WriteLine(">> s.Mid(-1, -3) --> \"{0}\"", s.Mid(-1, -3));
System.Console.WriteLine(">> s.Mid(4, -3) --> \"{0}\"", s.Mid(4, -3));
System.Console.WriteLine(">> s.Mid(1, 0) --> \"{0}\"", s.Mid(1, 0));
System.Console.WriteLine(">> s.Mid(1, 40) --> \"{0}\"", s.Mid(1, 40));
System.Console.WriteLine(">> s.Mid(2, 3) --> \"{0}\"", s.Mid(2, 3));
System.Console.WriteLine(">> s.Mid(7, 1) --> \"{0}\"", s.Mid(7, 1));
System.Console.WriteLine(">> s.Mid(8, -3) --> \"{0}\"", s.Mid(8, -3));
System.Console.WriteLine(">> s.Mid(8, 1) --> \"{0}\"\r\n", s.Mid(8, 1));
s = "ABCDEFGH";
System.Console.WriteLine("s = \"ABCDEFGH\"; // Len:{0}", s.Length);
System.Console.WriteLine(">> s.Mid(-1, 4) --> \"{0}\"", s.Mid(-1, 4));
System.Console.WriteLine(">> s.Mid(-1, -3) --> \"{0}\"", s.Mid(-1, -3));
System.Console.WriteLine(">> s.Mid(4, -3) --> \"{0}\"", s.Mid(4, -3));
System.Console.WriteLine(">> s.Mid(1, 0) --> \"{0}\"", s.Mid(1, 0));
System.Console.WriteLine(">> s.Mid(1, 40) --> \"{0}\"", s.Mid(1, 40));
System.Console.WriteLine(">> s.Mid(2, 3) --> \"{0}\"", s.Mid(2, 3));
System.Console.WriteLine(">> s.Mid(7, 1) --> \"{0}\"", s.Mid(7, 1));
System.Console.WriteLine(">> s.Mid(8, -3) --> \"{0}\"", s.Mid(8, -3));
System.Console.WriteLine(">> s.Mid(8, 1) --> \"{0}\"\r\n", s.Mid(8, 1));

Console output will be as follows.

string s = null;
>> s.Mid(-1, 4) --> null

s = ""; // Len:0
>> s.Mid(-1, 4) --> ""
>> s.Mid(-1, -3) --> ""
>> s.Mid(4, -3) --> ""
>> s.Mid(1, 0) --> ""
>> s.Mid(1, 40) --> ""
>> s.Mid(2, 3) --> ""
>> s.Mid(7, 1) --> ""
>> s.Mid(8, -3) --> ""
>> s.Mid(8, 1) --> ""

s = "ABCDEFGH"; // Len:8
>> s.Mid(-1, 4) --> "ABC"
>> s.Mid(-1, -3) --> ""
>> s.Mid(4, -3) --> "BCD"
>> s.Mid(1, 0) --> ""
>> s.Mid(1, 40) --> "BCDEFGH"
>> s.Mid(2, 3) --> "CDE"
>> s.Mid(7, 1) --> "H"
>> s.Mid(8, -3) --> "FGH"
>> s.Mid(8, 1) --> ""

RemoveNewLines Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Replace '\r\n' characters in string with spaces.

public static string RemoveNewLines(this string s)

Examples

string s = null;
System.Console.WriteLine("string s = null;");
System.Console.WriteLine(">> s.RemoveNewLines() --> {0}\r\n", s.RemoveNewLines() ?? "null");
s = "";
System.Console.WriteLine("s = \"\";");
System.Console.WriteLine(">> s.RemoveNewLines() --> \"{0}\"\r\n", s.RemoveNewLines());
s = "abc\rdefg\n1234\r\n5678";
System.Console.WriteLine("s = \"abc\\rdefg\\n1234\\r\\n5678\";");
System.Console.WriteLine(">> s.RemoveNewLines() --> \"{0}\"\r\n", s.RemoveNewLines());

Console output will be as follows.

string s = null;
>> s.RemoveNewLines() --> null

s = "";
>> s.RemoveNewLines() --> ""

s = "abc\rdefg\n1234\r\n5678";
>> s.RemoveNewLines() --> "abc defg 1234 5678"

Replace Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.
This method replace multiple strings at once.

Overloads

Method Description
Replace(String, String[], String[], StringComparison) A comparison option is provided for replacement operations.
Replace(String, String[], String[], bool) A case sensitivity option is provided for replacement operations.
Replace(String, String[], String[]) It does a case sensitive replacement.

Parameters

Name Type Description
s String The string to replace.
oldValues String[] The string array to be replaced.
newValues String[] The string to replace all occurrences of oldValues.
comparisonType StringComparison One of the enumeration values that determines how this string and value are compared.
ignoreCase Boolean true to ignore case during the comparison; otherwise, false.

Return

String
Returns an instance of the replaced string as a string type.

Examples

string[] aryFind = new string[] { "one", "two", "1", "2" };
string[] aryReplace = new string[] { "1", "2", "one", "Two" };
string s = null;            
System.Console.WriteLine("string[] aryFind = new string[] { \"one\", \"two\", \"1\", \"2\" };");
System.Console.WriteLine("string[] aryReplace = new string[] { \"1\", \"2\", \"one\", \"Two\" };");
System.Console.WriteLine();
System.Console.WriteLine("string s = null;");
System.Console.WriteLine(">> s.Replace(aryFind, aryReplace) --> {0}\r\n", s.Replace(aryFind, aryReplace) ?? "null");
s = "One test, two test, 1 test, 2 test";
System.Console.WriteLine("s = \"One test, two test, 1 test, 2 test\";");
System.Console.WriteLine(">> s.Replace(aryFind, aryReplace) --> \"{0}\"\r\n", s.Replace(aryFind, aryReplace));
s = "One test, two test, 1 test, 2 test";
System.Console.WriteLine("s = \"One test, two test, 1 test, 2 test\"; // ignoreCase");
System.Console.WriteLine(">> s.Replace(aryFind, aryReplace, true) --> \"{0}\"\r\n", s.Replace(aryFind, aryReplace, true));

Console output will be as follows.

string[] aryFind = new string[] { "one", "two", "1", "2" };
string[] aryReplace = new string[] { "1", "2", "one", "Two" };

string s = null;
>> s.Replace(aryFind, aryReplace) --> null

s = "One test, two test, 1 test, 2 test";
>> s.Replace(aryFind, aryReplace) --> "One test, 2 test, one test, Two test"

s = "One test, two test, 1 test, 2 test"; // ignoreCase
>> s.Replace(aryFind, aryReplace, true) --> "1 test, 2 test, one test, Two test"

ToMessages Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Returns all exception messages.

public static string ToMessages(this Exception error)

Parameters

error: A parameter of type System.Exception.

Return

String
Returns all messages as string, including internal exceptions.

Examples

Exception error = new System.IO.FileNotFoundException(
    new System.IO.FileNotFoundException().Message, /* default message */
    new ArgumentNullException(
        new ArgumentNullException("filePath").Message /* default message */,
        new ArgumentException("Invalid file path.")));
Console.WriteLine(">> {0}", error.ToMessages());

Console output will be as follows.

>> FileNotFoundException: Unable to find the specified file. --> ArgumentNullException: Value cannot be null. Parameter name: filePath --> ArgumentException: Invalid file path.

SafeClone<T> Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

It is like the 'Clone' method. It will not throw an exception for an object that is 'null'.
The object must be derived from the System.ICloneable interface.

public static T SafeClone<T>(this T obj) where T : ICloneable

Parameters

obj: An object of type 'T'.

Return

T
'null' or an instance of the object is returned.

IndexOf<T> Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Searches for the specified object and returns the index of the first occurrence within the entire one-dimensional IEnumerable.

public static int IndexOf<T>(this System.Collections.IEnumerable source, T item)

Parameters

source: An object of type System.Collections.IEnumerable.
item: The element in the list whose index number is requested.

Return

Int32
The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.

ioCode.Common.Macro

A class that outputs strings by compiling specified custom date-time formats.

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Properties

Name Type Description
Current Macro A property that presents a public static instance of the current class.
CultureInfo System.Globalization.CultureInfo The culture property that will be used for format conversions.

Methods

Name Description
Compile(String, DateTime) A method that compiles a string specified with a string parameter and a date-time parameter.
- macroString: The format string to compile. It is listed in the 'Formats' table.
- dtValue: The date-time value for the format string to be compiled.
Compile(String) A method that compiles a specified string using a string parameter and local date-time information.
- macroString: The format string to compile. It is listed in the 'Formats' table.

Formats

Name Description
${h}: 12-hour clock hour (e.g. 4)
${hh}: 12-hour clock, with a leading 0 (e.g. 06)
${H}: 24-hour clock hour (e.g. 15)
${HH}: 24-hour clock hour, with a leading 0 (e.g. 22)
${m}: Minutes
${mm}: Minutes with a leading zero
${s}: Seconds
${ss}: Seconds with a leading zero
${f}: Represents the tenths of a second
${ff}: Represents the two most significant digits of the seconds' fraction in date and time
${fff}: Milliseconds
${t}: Abbreviated AM / PM (e.g. A or P)
${tt}: AM / PM (e.g. AM or PM)
${d}: Represents the day of the month as a number from 1 through 31
${dd}: Represents the day of the month as a number from 01 through 31
${ddd}: Represents the abbreviated name of the day (Mon, Tues, Wed, etc)
${dddd}: Represents the full name of the day (Monday, Tuesday, etc)
${wd}: Represents the day of the week (e.g. 7 for Sunday)
${M}: Month number (eg. 3)
${MM}: Month number with leading zero (eg. 04)
${MMM}: Abbreviated Month Name (e.g. Dec)
${MMMM}: Full month name (e.g. December)
${y}: Year, no leading zero (e.g. 2015 would be 15)
${yy}: Year, leading zero (e.g. 2015 would be 015)
${yyy}: Year, (e.g. 2015)
${yyyy}: Year, (e.g. 2015)
${z}: With DateTime values represents the signed offset of the local operating system's time zone from Coordinated Universal Time (UTC), measured in hours. (e.g. +6)
${zz}: As z, but with leading zero (e.g. +06)
${zzz}: With DateTime values represents the signed offset of the local operating system's time zone from UTC, measured in hours and minutes. (e.g. +06:00)

Examples

Macro.Current.CultureInfo = System.Globalization.CultureInfo.InvariantCulture;
string s = @".\${yyyy}\${MMMM}\${dddd}\log_${yyyy.MM.dd HH.mm.ss}.log";
Console.WriteLine("Format: \"{0}\"", s);
Console.WriteLine();
Console.WriteLine(">> \"{0}\"", Macro.Current.Compile(s));
Console.WriteLine(">> \"{0}\"", Macro.Current.Compile(s, DateTime.UtcNow));
Console.WriteLine();
Macro.Current.CultureInfo = System.Globalization.CultureInfo.GetCultureInfo("tr-TR");
Console.WriteLine(">> \"{0}\"", Macro.Current.Compile(s));
Console.WriteLine(">> \"{0}\"", Macro.Current.Compile(s, DateTime.UtcNow));

Console output will be as follows.

Format: ".\${yyyy}\${MMMM}\${dddd}\log_${yyyy.MM.dd HH.mm.ss}.log"

>> ".\2022\May\Thursday\log_2022.05.12 20.24.44.log"
>> ".\2022\May\Thursday\log_2022.05.12 17.24.44.log"

>> ".\2022\Mayis\Persembe\log_2022.05.12 20.24.44.log"
>> ".\2022\Mayis\Persembe\log_2022.05.12 17.24.44.log"

ioCode.Common.MathExtensions

It provides extended methods for variables sbyte, short, int, long, decimal, double, float, byte, ushort, uint, long, TimeSpan, DateTime that enforce minimum and maximum range.

Overload Methods

Method Description
Range(Decimal, Decimal, Decimal) Forces the specified decimal value between two limits.
Range(Double, Double, Double) Forces the specified double value between two limits.
Range(Single, Single, Single) Forces the specified float value between two limits.
Range(SByte, SByte, SByte) Forces the specified sbyte value between two limits.
Range(Int16, Int16, Int16) Forces the specified short value between two limits.
Range(Int32, Int32, Int32) Forces the specified int value between two limits.
Range(Int64, Int64, Int64) Forces the specified long value between two limits.
Range(Byte, Byte, Byte) Forces the specified byte value between two limits.
Range(UInt16, UInt16, UInt16) Forces the specified ushort value between two limits.
Range(UInt32, UInt32, UInt32) Forces the specified uint value between two limits.
Range(UInt64, UInt64, UInt64) Forces the specified ulong value between two limits.
Range(TimeSpan, TimeSpan, TimeSpan) Forces the specified TimeSpan value between two limits.
Range(DateTime, DateTime, DateTime) Forces the specified DateTime value between two limits.

Examples

int nVal, nFirst, nLast;
Random rnd = new Random(Environment.TickCount);
for (int i = 0; i < 10; i++)
{
    nVal = (rnd.Next() - (int.MaxValue >> 1)) % 10000;
    nFirst = (rnd.Next() - (int.MaxValue >> 1)) % 10000;
    nLast = (rnd.Next() - (int.MaxValue >> 1)) % 10000;
    Console.WriteLine("({0}).Range({1}, {2}) --> {3}", nVal, nFirst, nLast, nVal.Range(nFirst, nLast));
}

Console Output:

(8306).Range(3500, -2369) --> 3500
(3024).Range(-2364, 3872) --> 3024
(-6756).Range(1801, -9488) --> -6756
(-4222).Range(-3961, 6258) --> -3961
(7346).Range(2885, -2777) --> 2885
(2705).Range(5300, -250) --> 2705
(-2100).Range(-5751, -8946) --> -5751
(-4440).Range(2352, -9501) --> -4440
(-2876).Range(9931, -1875) --> -1875
(-8038).Range(-6138, -1224) --> -6138

Range(Decimal, Decimal, Decimal) Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Forces the specified System.Decimal value between two limits.

public static decimal Range(this decimal n, decimal first, decimal last)

Parameters

n: The value to compare with the specified range.
first: The first value to compare.
last: The last value to compare.

Return

Decimal
Returns a value in the specified range.

Range(Double, Double, Double) Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Forces the specified System.Double value between two limits.

public static double Range(this double n, double first, double last)

Parameters

n: The value to compare with the specified range.
first: The first value to compare.
last: The last value to compare.

Return

Double
Returns a value in the specified range.

Range(Single, Single, Single) Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Forces the specified System.Single value between two limits.

public static float Range(this float n, float first, float last)

Parameters

n: The value to compare with the specified range.
first: The first value to compare.
last: The last value to compare.

Return

Single
Returns a value in the specified range.

Range(SByte, SByte, SByte) Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Forces the specified System.SByte value between two limits.

public static sbyte Range(this sbyte n, sbyte first, sbyte last)

Parameters

n: The value to compare with the specified range.
first: The first value to compare.
last: The last value to compare.

Return

SByte
Returns a value in the specified range.

Range(Int16, Int16, Int16) Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Forces the specified System.Int16 value between two limits.

public static short Range(this short n, short first, short last)

Parameters

n: The value to compare with the specified range.
first: The first value to compare.
last: The last value to compare.

Return

Int16
Returns a value in the specified range.

Range(Int32, Int32, Int32) Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Forces the specified System.Int32 value between two limits.

public static int Range(this int n, int first, int last)

Parameters

n: The value to compare with the specified range.
first: The first value to compare.
last: The last value to compare.

Return

Int32
Returns a value in the specified range.

Range(Int64, Int64, Int64) Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Forces the specified System.Int64 value between two limits.

public static long Range(this long n, long first, long last)

Parameters

n: The value to compare with the specified range.
first: The first value to compare.
last: The last value to compare.

Return

Int64
Returns a value in the specified range.

Range(Byte, Byte, Byte) Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Forces the specified System.Byte value between two limits.

public static byte Range(this byte n, byte first, byte last)

Parameters

n: The value to compare with the specified range.
first: The first value to compare.
last: The last value to compare.

Return

Byte
Returns a value in the specified range.

Range(UInt16, UInt16, UInt16) Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Forces the specified System.UInt16 value between two limits.

public static ushort Range(this ushort n, ushort first, ushort last)

Parameters

n: The value to compare with the specified range.
first: The first value to compare.
last: The last value to compare.

Return

UInt16
Returns a value in the specified range.

Range(UInt32, UInt32, UInt32) Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Forces the specified System.UInt32 value between two limits.

public static uint Range(this uint n, uint first, uint last)

Parameters

n: The value to compare with the specified range.
first: The first value to compare.
last: The last value to compare.

Return

UInt32
Returns a value in the specified range.

Range(UInt64, UInt64, UInt64) Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Forces the specified System.UInt64 value between two limits.

public static ulong Range(this ulong n, ulong first, ulong last)

Parameters

n: The value to compare with the specified range.
first: The first value to compare.
last: The last value to compare.

Return

UInt64
Returns a value in the specified range.

Range(TimeSpan, TimeSpan, TimeSpan) Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Forces the specified System.TimeSpan value between two limits.

public static TimeSpan Range(this TimeSpan n, TimeSpan first, TimeSpan last)

Parameters

n: The value to compare with the specified range.
first: The first value to compare.
last: The last value to compare.

Return

TimeSpan
Returns a value in the specified range.

Range(DateTime, DateTime, DateTime) Method

Definition

Namespace: ioCode.Common
Assembly: ioCode.Common.dll

Forces the specified System.DateTime value between two limits.

public static DateTime Range(this DateTime n, DateTime first, DateTime last)

Parameters

n: The value to compare with the specified range.
first: The first value to compare.
last: The last value to compare.

Return

DateTime
Returns a value in the specified range.

Copyright & License

Copyright (c) 2022, ioCode

The code is under MIT License. (see LICENSE)

About

A library that provides extended methods for practical use of conversions and extended methods for general uses.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages