# Extensions

# Introduction

Since the version 2.5.0.2020 of LeoCorpLibrary, a new namespcae has been added: LeoCorpLibrary.Extensions. In this namespace, there is extensions for common types such as string, int, arrays...

If you wanna use those extensions, import the namespace:

C#

using LeoCorpLibrary.Extensions;

VB

Import LeoCorpLibrary.Extensions

# String Extensions

# a. CountWords

This function is available in version 2.5 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The CountWords() method allows you to get the number of words in a string. Returns an int value.

It's in:

LeoCorpLibrary.Extensions.StringExtensions.CountWords()

This method has two variation:

Variation 1

This first variation doesn't take any argument.

Variation 2 This method has one argument:

Value Argument Description
string[] wordSeparator The separator of the words. Example: ", ; : ! ? . "

Here's an example of usage:

C#

// Variation 1
int numberOfWords = "Hello, this is a test sentence!".CountWords();

// Variation 2
string[] wordSeparator = { " ", ",", ":", "!", "?", ";", "." };
int numberOfWords = "Hello, this is a test sentence!".CountWords(wordSeparator);

VB

' Variation 1
Dim numberOfWords As Integer = "Hello, this is a test sentence!".CountWords()

' Variation 2
Dim wordSeparator As String() = {" ", ",", ":", "!", "?", ";", "."}
Dim numberOfWords As Integer = "Hello, this is a test sentence!".CountWords(wordSeparator)

Go to top

# b. Encrypt

This function is available in version 2.6 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The Encrypt() method allows you to encrypt a string. It returns a string value.

This method has a few arguments:

Type Argument Description
string source The string to encrypt
string key The key that will be used to encrypt and decrypt the string

Here's an example of usage:

C#

string s = "Hello!";
string encrypted = LeoCorpLibrary.Crypt.Encrypt(s, "ABC123");

// Output: jqPW4uxt8hk=

VB

Dim s As String = "Hello!"
Dim encrypted As String = LeoCorpLibrary.Crypt.Encrypt(s, "ABC123")

' Output: jqPW4uxt8hk=

Go to top

# c. Decrypt

This function is available in version 2.6 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The Decrypt() method allows you to decrypt an encrypted string. It returns a string value.

This method has a few arguments:

Type Argument Description
string encrypt The encrypted string
string key The key that will be used to encrypt and decrypt the string

Here's an example of usage:

C#

string encrypted = "jqPW4uxt8hk=";
string decrypted = LeoCorpLibrary.Crypt.Decrypt(encrypted, "ABC123");

// Output: Hello!

VB

Dim encrypted As String = "jqPW4uxt8hk="
Dim decrypted As String = LeoCorpLibrary.Crypt.Decrypt(encrypted, "ABC123")

' Output: Hello!

Go to top

# d. UpperFirstLetter

This function is available in version 3.2 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The UpperFirstLetter() method allows you to upper the first letter of a string. It returns a string value.

Here's an example of usage:

C#

string helloMsg = "hello, this is a test"; // String
Console.WriteLine(helloMsg.UpperFirstLetter());

// Output:
// Hello, this is a test

VB

Dim helloMsg As String = "hello, this is a test" ' String
Console.WriteLine(helloMsg.UpperFirstLetter())

' Output:
' Hello, this is a test

Go to top

# e. UpperLettersAt

This function is available in version 3.4 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

This method allows you to upper letters from a starting index, and an end index. It returns a string value.

This method has a few arguments:

Type Argument Description
int startIndex The position where letters should start be in upper case
int length The position where letters should stop be in upper case

Here's an example of usage:

C#

string s = "I want leocorplibrary in upper case".UpperLettersAt(7, 20);
// Value of s:
// "I want LEOCORPLIBRARY in upper case"

VB

Dim s As String = "I want leocorplibrary in upper case".UpperLettersAt(7, 20)
' Value of s:
' "I want LEOCORPLIBRARY in upper case"

Go to top

# f. IsEndingWithSamePunctuation

This function is available in version 3.4 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

This method allows you to compare if two string values are ending with the same punctuation. It returns a bool value.

Variation 1

IsEndingWithSamePunctuation(string stringToCheck) {...}

This first variation has one argument:

Type Argument Description
string stringToCheck The string to compare

Variation 2

IsEndingWithSamePunctuation(string stringToCheck, string punctuationToCheck) {...}

This second variation has two arguments:

Type Argument Description
string stringToCheck The string to compare
string punctuationToCheck The punctuation sign to check for

Here's an example of usage:

C#

// Variation 1

string a = "This is a test sentence.";
string b = "This is an another test sentence.";

a.IsEndingWithSamePunctuation(b); // Returns true

// Variation 2

a.IsEndingWithSamePunctuation(b, "."); // Returns true

VB

' Variation 1

Dim a As String = "This is a test sentence."
Dim b As String = "This is an another test sentence."

a.IsEndingWithSamePunctuation(b) ' Returns true

' Variation 2

a.IsEndingWithSamePunctuation(b, ".") ' Returns true

Go to top

# g. SplitLines

This function is available in version 3.6 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The SplitLines() method allows you to split a string with multiple lines, and put each line of this string into an array of string. It returns a string[] value.

This method has no argument.

Here's an example of usage:

C#

string text = "Hello,\nWorld!";
string[] lines = text.SplitLines();

// lines[0] = Hello,
// lines[1] = World!

VB

Dim text As String = "Hello," & vbLf & "World!"
Dim lines As String() = text.SplitLines()

' lines[0] = Hello,
' lines[1] = World!

Go to top

# h. HasRepeatedCharacters

This function is available in version 3.8 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The HasRepeatedCharacters() method allows you to know if a string has repeated characters. It returns a bool value.

It's in:

LeoCorpLibrary.Extensions.StringExtensions.HasRepeatedCharacters()

This method doesn't take any argument.

Here's an example of usage:

C#

bool r = "aabb".HasRepeatedCharacters();
// r = true

VB

Dim r As Boolean = "aabb".HasRepeatedCharacters()
' r = true

Go to top

# Int Extensions

# a. IsEven

This function is available in version 2.5 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The IsEven() method allows you to know if a specic int is even. Returns a bool value.

It's in:

LeoCorpLibrary.Extensions.IntExtensions.IsEven()

This method doesn't take any argument.

Here's an example of usage:

C#

bool even = 12.IsEven(); // Returns true
bool odd = 11.IsEven(); // Returns false

VB

Dim even As Boolean = 12.IsEven() ' Returns true
Dim odd As Boolean = 11.IsEven() ' Returns false

Go to top

# b. ToDouble

This function is available in version 2.5 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The ToDouble() method allows you to convert an ìnt value to a double value. Returns a double value.

It's in:

LeoCorpLibrary.Extensions.IntExtensions.IsEven()

This method doesn't take any argument.

Here's an example of usage:

C#

int a = 15;
int b = 2;
double result = a.ToDouble() / b.ToDouble();

VB

Dim a As Integer = 15
Dim b As Integer = 2
Dim result As Double = a.ToDouble() / b.ToDouble()

Go to top

# c. ConvertSizeUnitToByte

This function is available in version 3.6 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The ConvertSizeUnitToByte() method allows you to convert a size unit such as kilobyte, gigabyte to byte. It returns a double value.

It's in:

LeoCorpLibrary.Extensions.IntExtensions.ConvertSizeUnitToByte()

It has one argument:

Value Argument Description
UnitType unitType The source unit (kb, mb...)

Here's an example of usage:

C#

int kiloByte = 5000;
double byteVar = kiloByte.ConvertSizeUnitToByte(UnitType.Kilobyte);

// Expected value for byteVar: 5000000

VB

Dim kiloByte As Integer = 5000
Dim byteVar As Double = kiloByte.ConvertSizeUnitToByte(UnitType.Kilobyte)

' Expected value for byteVar: 5000000

Go to top

# d. ConvertSizeUnitToKilobyte

This function is available in version 3.6 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The ConvertSizeUnitToKilobyte() method allows you to convert a size unit such as kilobyte, gigabyte to kilobyte. It returns a double value.

It's in:

LeoCorpLibrary.Extensions.IntExtensions.ConvertSizeUnitToKilobyte()

It has one argument:

Value Argument Description
UnitType unitType The source unit (kb, mb...)

Here's an example of usage:

C#

int megaByte = 50;
double kiloByte = megaByte.ConvertSizeUnitToKilobyte(UnitType.Megabyte);

// Expected value for kiloByte: 50000

VB

Dim megaByte As Integer = 50
Dim kiloByte As Double = megaByte.ConvertSizeUnitToKilobyte(UnitType.Megabyte)

' Expected value for kiloByte: 50000

Go to top

# e. ConvertSizeUnitToMegabyte

This function is available in version 3.6 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The ConvertSizeUnitToMegabyte() method allows you to convert a size unit such as megabyte, gigabyte to megabyte. It returns a double value.

It's in:

LeoCorpLibrary.Extensions.IntExtensions.ConvertSizeUnitToMegabyte()

It has one argument:

Value Argument Description
UnitType unitType The source unit (kb, mb...)

Here's an example of usage:

C#

int gigaByte = 50;
double megaByte = gigaByte.ConvertSizeUnitToMegabyte(UnitType.Gigabyte);

// Expected value for megaByte: 50000

VB

Dim gigaByte As Integer = 50
Dim megaByte As Double = gigaByte.ConvertSizeUnitToMegabyte(UnitType.Gigabyte)

' Expected value for megaByte: 50000

Go to top

# f. ConvertSizeUnitToGigabyte

This function is available in version 3.6 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The ConvertSizeUnitToGigabyte() method allows you to convert a size unit such as megabyte, petabyte to gigabyte. It returns a double value.

It's in:

LeoCorpLibrary.Extensions.IntExtensions.ConvertSizeUnitToGigabyte()

It has one argument:

Value Argument Description
UnitType unitType The source unit (kb, mb...)

Here's an example of usage:

C#

int megaByte = 4400;
double gigaByte = megaByte.ConvertSizeUnitToGigabyte(UnitType.Megabyte);

// Expected value for gigaByte: 4

VB

Dim megaByte As Integer = 4400
Dim gigaByte As Double = megaByte.ConvertSizeUnitToGigabyte(UnitType.Megabyte)

' Expected value for gigaByte: 4

Go to top

# g. ConvertSizeUnitToTerabyte

This function is available in version 3.6 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The ConvertSizeUnitToTerabyte() method allows you to convert a size unit such as megabyte, petabyte to terabyte. It returns a double value.

It's in:

LeoCorpLibrary.Extensions.IntExtensions.ConvertSizeUnitToTerabyte()

It has one argument:

Value Argument Description
UnitType unitType The source unit (kb, mb...)

Here's an example of usage:

C#

int gigaByte = 2000;
double teraByte = gigaByte.ConvertSizeUnitToTerabyte(UnitType.Gigabyte);

// Expected value for teraByte: 2

VB

Dim gigaByte As Integer = 2000
Dim teraByte As Double = gigaByte.ConvertSizeUnitToTerabyte(UnitType.Gigabyte)

' Expected value for teraByte: 2

Go to top

# h. ConvertSizeUnitToPetabyte

This function is available in version 3.6 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The ConvertSizeUnitToPetabyte() method allows you to convert a size unit such as megabyte, terabyte to petabyte. It returns a double value.

It's in:

LeoCorpLibrary.Extensions.IntExtensions.ConvertSizeUnitToPetabyte()

It has one argument:

Value Argument Description
UnitType unitType The source unit (kb, mb...)

Here's an example of usage:

C#

int teraByte = 6000;
double petaByte = teraByte.ConvertSizeUnitToPetabyte(UnitType.Terabyte);

// Expected value for petaByte: 6

VB

Dim teraByte As Integer = 6000
Dim petaByte As Double = teraByte.ConvertSizeUnitToPetabyte(UnitType.Terabyte)

' Expected value for petaByte: 6

Go to top

# Arrays Extensions

# a. Append

This function is available in version 2.5 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The Append() method allows you to append an item or multiple items to an array. Returns a T[] value.

It's in:

LeoCorpLibrary.Extensions.ArrayExtensions.Append()

This method has two variations:

Variation 1

Value Argument Description
T item The item which is going to be append in the array

Variation 2

Value Argument Description
params T[] items The items which are going to be append in the array

Here's an example of usage:

C#

// Variation 1
int[] numbers = { 1, 2, 3, 4 };
int[] appendNumbers = numbers.Append(5);
// appendNumbers: { 1, 2, 3, 4, 5 }

// Variation 2
int[] numbers = { 1, 2, 3, 4 };
int[] appendNumbers = numbers.Append(5, 6);
// appendNumbers: { 1, 2, 3, 4, 5, 6 }

VB

' Variation
Dim numbers As Integer() = {1, 2, 3, 4}
Dim appendNumbers As Integer() = numbers.Append(5)
' appendNumbers: { 1, 2, 3, 4, 5 }

' Variation 2
Dim numbers As Integer() = {1, 2, 3, 4}
Dim appendNumbers As Integer() = numbers.Append(5, 6)
' appendNumbers: { 1, 2, 3, 4, 5, 6 }

Go to top

# b. RemoveItem

This function is available in version 2.5 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The RemoveItem() method allows you to remove a specific item from an array. It returns a T[] value.

It's in:

LeoCorpLibrary.Extensions.ArrayExtensions.RemoveItem()

This method has two variations:

Variation 1

Value Argument Description
T item The item to remove

Variation 2

Value Argument Description
params T[] items The items to remove

Here's an example of usage:

C#

// Variation 1
int[] numbers = { 1, 2, 3, 4, 5 };
int[] remove = numbers.RemoveItem(5);
// remove: { 1, 2, 3, 4 }

// Variation 2
int[] numbers = { 1, 2, 3, 4, 5 };
int[] remove = numbers.RemoveItem(5, 3);
// remove: { 1, 2, 4 }

VB

' Variation 1
Dim numbers As Integer() = {1, 2, 3, 4, 5}
Dim remove As Integer() = numbers.RemoveItem(5)
' remove: { 1, 2, 3, 4 }

' Variation 2
Dim numbers As Integer() = {1, 2, 3, 4, 5}
Dim remove As Integer() = numbers.RemoveItem(5, 3)
' remove: { 1, 2, 4 }

Go to top

# c. UnSplit

This function is available in version 3.6 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The UnSplit() method allows you to unsplit an array of string using a separator. It returns a string value.

It's in:

LeoCorpLibrary.Extensions.ArrayExtensions.UnSplit()

It has one argument:

Value Argument Description
string separator The separator to insert between items.

Here's an example of usage:

C#

string[] array = { "a", "b", "c", "d" };
string unSplit = array.UnSplit(";");

// Output:
// a;b;c;d

VB

Dim array As String() = {"a", "b", "c", "d"}
Dim unSplit As String = array.UnSplit(";")

' Output:
' a;b;c;d

Go to top