Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' ***********************************************************************
- ' Author : Elektro
- ' Modified : 16-June-2015
- ' ***********************************************************************
- ' <copyright file="IEnumerable(Of String) Extensions.vb" company="Elektro Studios">
- ' Copyright (c) Elektro Studios. All rights reserved.
- ' </copyright>
- ' ***********************************************************************
- #Region " Public Members Summary "
- #Region " Functions "
- ' IEnumerable(Of String).BubbleSort As IEnumerable(Of String)
- ' IEnumerable(Of String).CountEmptyItems As Integer
- ' IEnumerable(Of String).CountNonEmptyItems As Integer
- ' IEnumerable(Of String).FindByContains(String, Boolean) As IEnumerable(Of String)
- ' IEnumerable(Of String).FindByLike(String, Boolean) As IEnumerable(Of String)
- ' IEnumerable(Of String).FindExact(String, StringComparison) As IEnumerable(Of String)
- ' IEnumerable(Of String).RemoveByContains(String, Boolean) As IEnumerable(Of String)
- ' IEnumerable(Of String).RemoveByLike(String, Boolean) As IEnumerable(Of String)
- ' IEnumerable(Of String).RemoveExact(String, StringComparison) As IEnumerable(Of String)
- #End Region
- #End Region
- #Region " Option Statements "
- Option Strict On
- Option Explicit On
- Option Infer Off
- #End Region
- #Region " Imports "
- Imports System
- Imports System.Diagnostics
- Imports System.Runtime.CompilerServices
- Imports System.Text.RegularExpressions
- #End Region
- ''' <summary>
- ''' Contains custom extension methods to use with a <see cref="IEnumerable(Of String)"/>.
- ''' </summary>
- Public Module IEnumerableOfStringExtensions
- #Region " Public Extension Methods "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <remarks>
- ''' Title : Count empty items of IEnumerable(Of String) (or same as count blank lines)
- ''' Author: Elektro
- ''' Date : 16-June-2015
- ''' </remarks>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <example>
- ''' Dim emptyLinesCount As Integer = {"Hello", " ", "World!"}.CountEmptyItems
- ''' </example>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Counts the empty items of an <see cref="IEnumerable(Of String)"/>.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="sender">
- ''' The source <see cref="IEnumerable(Of String)"/>.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' The total amount of empty items.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <Extension>
- Public Function CountEmptyItems(ByVal sender As IEnumerable(Of String)) As Integer
- Return (From str As String In sender
- Where String.IsNullOrEmpty(str) OrElse
- String.IsNullOrWhiteSpace(str)).Count
- End Function
- ''' ----------------------------------------------------------------------------------------------------
- ''' <remarks>
- ''' Title : Count non-empty items of IEnumerable(Of String) (or same as count non-blank lines)
- ''' Author: Elektro
- ''' Date : 16-June-2015
- ''' </remarks>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <example>
- ''' Dim nonEmptyLinesCount As Integer = {"Hello", " ", "World!"}.CountNonEmptyItems
- ''' </example>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Counts the non-empty items of an <see cref="IEnumerable(Of String)"/>.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="sender">
- ''' The source <see cref="IEnumerable(Of String)"/>.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' The total amount of non-empty items.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <Extension>
- Public Function CountNonEmptyItems(ByVal sender As IEnumerable(Of String)) As Integer
- Return (From str As String In sender
- Where Not String.IsNullOrEmpty(str) AndAlso
- Not String.IsNullOrWhiteSpace(str)).Count
- End Function
- ''' ----------------------------------------------------------------------------------------------------
- ''' <remarks>
- ''' Title : Sort Collection by BubbleSort Method.
- ''' Author: Elektro
- ''' Date : 08-March-2015
- ''' </remarks>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <example> This is a code example.
- ''' <code>
- ''' ' Dim col As IEnumerable(Of String) = {"10", "333", "2", "45"}
- ''' ' Debug.WriteLine(String.Join(", ", col.BubbleSort))
- ''' </code>
- ''' </example>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Sorts the source <see cref="IEnumerable(Of String)"/> by BubbleSort method.
- ''' </summary>
- ''' ---------------------------------------------------------------------------------------------------- '''
- ''' <param name="sender">
- ''' The source collection.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' <see cref="IEnumerable(Of T)"/>.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <DebuggerHidden>
- <Extension>
- Public Function BubbleSort(ByVal sender As IEnumerable(Of String)) As IEnumerable(Of String)
- Return sender.Select(Function(value As String)
- Return New With
- {
- Key .OrgStr = value,
- Key .SortStr =
- Regex.Replace(value, "(\d+)|(\D+)",
- Function(match As Match)
- Return match.Value.PadLeft(sender.Select(Function(str As String)
- Return str.Length
- End Function).Max,
- If(Char.IsDigit(match.Value(0)),
- " "c,
- Char.MaxValue))
- End Function)
- }
- End Function).
- OrderBy(Function(anon) anon.SortStr).
- Select(Function(anon) anon.OrgStr)
- End Function
- ''' ----------------------------------------------------------------------------------------------------
- ''' <remarks>
- ''' Title : Find String-Exact In Collection.
- ''' Author: Elektro
- ''' Date : 08-March-2015
- ''' </remarks>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <example> This is a code example.
- ''' <code>
- ''' Dim col As IEnumerable(Of String) = {"Hello World !!", "a", "b", "c"}
- ''' Debug.WriteLine(String.Join(", ", col.FindExact(searchString:="a", stringComparison:=StringComparison.OrdinalIgnoreCase)))
- ''' </code>
- ''' </example>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Finds the elements that are equals to the specified string on the source <see cref="IEnumerable(Of String)"/>.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="sender">
- ''' The source collections.
- ''' </param>
- '''
- ''' <param name="searchString">
- ''' The string to search for.
- ''' </param>
- '''
- ''' <param name="stringComparison">
- ''' The string comparison rule.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' <see cref="IEnumerable(Of String)"/>.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <DebuggerHidden>
- <Extension>
- Public Function FindExact(ByVal sender As IEnumerable(Of String),
- ByVal searchString As String,
- ByVal stringComparison As StringComparison) As IEnumerable(Of String)
- Return From value As String In sender
- Where value.Equals(searchString, stringComparison)
- End Function
- ''' ----------------------------------------------------------------------------------------------------
- ''' <remarks>
- ''' Title : Find String-Contains In Collection.
- ''' Author: Elektro
- ''' Date : 08-March-2015
- ''' </remarks>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <example> This is a code example.
- ''' <code>
- ''' Dim col As IEnumerable(Of String) = {"Hello World !!", "a", "b", "c"}
- ''' Debug.WriteLine(String.Join(", ", col.FindByContains(searchString:="World", ignoreCase:=True)))
- ''' </code>
- ''' </example>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Finds the elements that contains the specified string on the source <see cref="IEnumerable(Of String)"/>.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="sender">
- ''' The source collections.
- ''' </param>
- '''
- ''' <param name="searchString">
- ''' The string to search for.
- ''' </param>
- '''
- ''' <param name="ignoreCase">
- ''' If set to <c>true</c>, performs a non sensitive string-case comparison.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' <see cref="IEnumerable(Of String)"/>.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <DebuggerHidden>
- <Extension>
- Public Function FindByContains(ByVal sender As IEnumerable(Of String),
- ByVal searchString As String,
- ByVal ignoreCase As Boolean) As IEnumerable(Of String)
- Return From value As String In sender
- Where If(ignoreCase,
- value.ToLower.Contains(searchString.ToLower),
- value.Contains(searchString))
- End Function
- ''' ----------------------------------------------------------------------------------------------------
- ''' <remarks>
- ''' Title : Find String-Like In Collection.
- ''' Author: Elektro
- ''' Date : 08-March-2015
- ''' </remarks>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <example> This is a code example.
- ''' <code>
- ''' Dim col As IEnumerable(Of String) = {"Hello World", "a", "b", "c"}
- ''' Debug.WriteLine(String.Join(", ", col.FindByLike(likePattern:="*hello*", ignoreCase:=True)))
- ''' </code>
- ''' </example>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Performs a String-Like pattern search on the source <see cref="IEnumerable(Of String)"/> and returns all the matches.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="sender">
- ''' The source collections.
- ''' </param>
- '''
- ''' <param name="likePattern">
- ''' The pattern comparison to use with the <see langword="Like"/> operator.
- ''' </param>
- '''
- ''' <param name="ignoreCase">
- ''' If set to <c>true</c>, performs a non sensitive string-case comparison.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' <see cref="IEnumerable(Of String)"/>.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <DebuggerHidden>
- <Extension>
- Public Function FindByLike(ByVal sender As IEnumerable(Of String),
- ByVal likePattern As String,
- ByVal ignoreCase As Boolean) As IEnumerable(Of String)
- Return From value As String In sender
- Where If(ignoreCase,
- value.ToLower Like likePattern.ToLower,
- value Like likePattern)
- End Function
- ''' ----------------------------------------------------------------------------------------------------
- ''' <remarks>
- ''' Title : Remove String-Exact In Collection.
- ''' Author: Elektro
- ''' Date : 08-March-2015
- ''' </remarks>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <example> This is a code example.
- ''' <code>
- ''' Dim col As IEnumerable(Of String) = {"Hello World !!", "a", "b", "c"}
- ''' Debug.WriteLine(String.Join(", ", col.RemoveExact(searchString:="Hello", ignoreCase:=True)))
- ''' </code>
- ''' </example>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Removes the elements that are equals to the specified string on the source <see cref="IEnumerable(Of String)"/>.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="sender">
- ''' The source collections.
- ''' </param>
- '''
- ''' <param name="searchString">
- ''' The string to search for.
- ''' </param>
- '''
- ''' <param name="stringComparison">
- ''' The string comparison rule.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' <see cref="IEnumerable(Of String)"/>.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <DebuggerHidden>
- <Extension>
- Public Function RemoveExact(ByVal sender As IEnumerable(Of String),
- ByVal searchString As String,
- ByVal stringComparison As StringComparison) As IEnumerable(Of String)
- Return From value As String In sender
- Where Not value.Equals(searchString, stringComparison)
- End Function
- ''' ----------------------------------------------------------------------------------------------------
- ''' <remarks>
- ''' Title : Remove String-Contains In Collection.
- ''' Author: Elektro
- ''' Date : 08-March-2015
- ''' </remarks>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <example> This is a code example.
- ''' <code>
- ''' Dim col As IEnumerable(Of String) = {"Hello World !!", "a", "b", "c"}
- ''' Debug.WriteLine(String.Join(", ", col.RemoveByContains(searchString:="Hello", ignoreCase:=True)))
- ''' </code>
- ''' </example>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Removes the elements that contains the specified string on the source <see cref="IEnumerable(Of String)"/>.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="sender">
- ''' The source collections.
- ''' </param>
- '''
- ''' <param name="searchString">
- ''' The string to search for.
- ''' </param>
- '''
- ''' <param name="ignoreCase">
- ''' If set to <c>true</c>, performs a non sensitive string-case comparison.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' <see cref="IEnumerable(Of String)"/>.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <DebuggerHidden>
- <Extension>
- Public Function RemoveByContains(ByVal sender As IEnumerable(Of String),
- ByVal searchString As String,
- ByVal ignoreCase As Boolean) As IEnumerable(Of String)
- Return From value As String In sender
- Where If(ignoreCase,
- Not value.ToLower.Contains(searchString.ToLower),
- Not value.Contains(searchString))
- End Function
- ''' ----------------------------------------------------------------------------------------------------
- ''' <remarks>
- ''' Title : Remove String-Like In Collection.
- ''' Author: Elektro
- ''' Date : 08-March-2015
- ''' </remarks>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <example> This is a code example.
- ''' <code>
- ''' Dim col As IEnumerable(Of String) = {"Hello World", "a", "b", "c"}
- ''' Debug.WriteLine(String.Join(", ", col.RemoveByLike(likePattern:="*hello*", ignoreCase:=True)))
- ''' </code>
- ''' </example>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Performs a String-Like pattern search on the source <see cref="IEnumerable(Of String)"/> and removes all the matches.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="sender">
- ''' The source collections.
- ''' </param>
- '''
- ''' <param name="likePattern">
- ''' The pattern comparison to use with the <see langword="Like"/> operator.
- ''' </param>
- '''
- ''' <param name="ignoreCase">
- ''' If set to <c>true</c>, performs a non sensitive string-case comparison.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' <see cref="IEnumerable(Of String)"/>.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <DebuggerHidden>
- <Extension>
- Public Function RemoveByLike(ByVal sender As IEnumerable(Of String),
- ByVal likePattern As String,
- ByVal ignoreCase As Boolean) As IEnumerable(Of String)
- Return From value As String In sender
- Where If(ignoreCase,
- Not value.ToLower Like likePattern.ToLower,
- Not value Like likePattern)
- End Function
- #End Region
- End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement