Advertisement
Guest User

IEnumerable(Of String) Extensions - By Elektro

a guest
Sep 19th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.28 KB | None | 0 0
  1. ' ***********************************************************************
  2. ' Author : Elektro
  3. ' Modified : 16-June-2015
  4. ' ***********************************************************************
  5. ' <copyright file="IEnumerable(Of String) Extensions.vb" company="Elektro Studios">
  6. ' Copyright (c) Elektro Studios. All rights reserved.
  7. ' </copyright>
  8. ' ***********************************************************************
  9.  
  10. #Region " Public Members Summary "
  11.  
  12. #Region " Functions "
  13.  
  14. ' IEnumerable(Of String).BubbleSort As IEnumerable(Of String)
  15. ' IEnumerable(Of String).CountEmptyItems As Integer
  16. ' IEnumerable(Of String).CountNonEmptyItems As Integer
  17. ' IEnumerable(Of String).FindByContains(String, Boolean) As IEnumerable(Of String)
  18. ' IEnumerable(Of String).FindByLike(String, Boolean) As IEnumerable(Of String)
  19. ' IEnumerable(Of String).FindExact(String, StringComparison) As IEnumerable(Of String)
  20. ' IEnumerable(Of String).RemoveByContains(String, Boolean) As IEnumerable(Of String)
  21. ' IEnumerable(Of String).RemoveByLike(String, Boolean) As IEnumerable(Of String)
  22. ' IEnumerable(Of String).RemoveExact(String, StringComparison) As IEnumerable(Of String)
  23.  
  24. #End Region
  25.  
  26. #End Region
  27.  
  28. #Region " Option Statements "
  29.  
  30. Option Strict On
  31. Option Explicit On
  32. Option Infer Off
  33.  
  34. #End Region
  35.  
  36. #Region " Imports "
  37.  
  38. Imports System
  39. Imports System.Diagnostics
  40. Imports System.Runtime.CompilerServices
  41. Imports System.Text.RegularExpressions
  42.  
  43. #End Region
  44.  
  45. ''' <summary>
  46. ''' Contains custom extension methods to use with a <see cref="IEnumerable(Of String)"/>.
  47. ''' </summary>
  48. Public Module IEnumerableOfStringExtensions
  49.  
  50. #Region " Public Extension Methods "
  51.  
  52. ''' ----------------------------------------------------------------------------------------------------
  53. ''' <remarks>
  54. ''' Title : Count empty items of IEnumerable(Of String) (or same as count blank lines)
  55. ''' Author: Elektro
  56. ''' Date : 16-June-2015
  57. ''' </remarks>
  58. ''' ----------------------------------------------------------------------------------------------------
  59. ''' <example>
  60. ''' Dim emptyLinesCount As Integer = {"Hello", " ", "World!"}.CountEmptyItems
  61. ''' </example>
  62. ''' ----------------------------------------------------------------------------------------------------
  63. ''' <summary>
  64. ''' Counts the empty items of an <see cref="IEnumerable(Of String)"/>.
  65. ''' </summary>
  66. ''' ----------------------------------------------------------------------------------------------------
  67. ''' <param name="sender">
  68. ''' The source <see cref="IEnumerable(Of String)"/>.
  69. ''' </param>
  70. ''' ----------------------------------------------------------------------------------------------------
  71. ''' <returns>
  72. ''' The total amount of empty items.
  73. ''' </returns>
  74. ''' ----------------------------------------------------------------------------------------------------
  75. <DebuggerStepThrough>
  76. <Extension>
  77. Public Function CountEmptyItems(ByVal sender As IEnumerable(Of String)) As Integer
  78.  
  79. Return (From str As String In sender
  80. Where String.IsNullOrEmpty(str) OrElse
  81. String.IsNullOrWhiteSpace(str)).Count
  82.  
  83. End Function
  84.  
  85. ''' ----------------------------------------------------------------------------------------------------
  86. ''' <remarks>
  87. ''' Title : Count non-empty items of IEnumerable(Of String) (or same as count non-blank lines)
  88. ''' Author: Elektro
  89. ''' Date : 16-June-2015
  90. ''' </remarks>
  91. ''' ----------------------------------------------------------------------------------------------------
  92. ''' <example>
  93. ''' Dim nonEmptyLinesCount As Integer = {"Hello", " ", "World!"}.CountNonEmptyItems
  94. ''' </example>
  95. ''' ----------------------------------------------------------------------------------------------------
  96. ''' <summary>
  97. ''' Counts the non-empty items of an <see cref="IEnumerable(Of String)"/>.
  98. ''' </summary>
  99. ''' ----------------------------------------------------------------------------------------------------
  100. ''' <param name="sender">
  101. ''' The source <see cref="IEnumerable(Of String)"/>.
  102. ''' </param>
  103. ''' ----------------------------------------------------------------------------------------------------
  104. ''' <returns>
  105. ''' The total amount of non-empty items.
  106. ''' </returns>
  107. ''' ----------------------------------------------------------------------------------------------------
  108. <DebuggerStepThrough>
  109. <Extension>
  110. Public Function CountNonEmptyItems(ByVal sender As IEnumerable(Of String)) As Integer
  111.  
  112. Return (From str As String In sender
  113. Where Not String.IsNullOrEmpty(str) AndAlso
  114. Not String.IsNullOrWhiteSpace(str)).Count
  115.  
  116. End Function
  117.  
  118. ''' ----------------------------------------------------------------------------------------------------
  119. ''' <remarks>
  120. ''' Title : Sort Collection by BubbleSort Method.
  121. ''' Author: Elektro
  122. ''' Date : 08-March-2015
  123. ''' </remarks>
  124. ''' ----------------------------------------------------------------------------------------------------
  125. ''' <example> This is a code example.
  126. ''' <code>
  127. ''' ' Dim col As IEnumerable(Of String) = {"10", "333", "2", "45"}
  128. ''' ' Debug.WriteLine(String.Join(", ", col.BubbleSort))
  129. ''' </code>
  130. ''' </example>
  131. ''' ----------------------------------------------------------------------------------------------------
  132. ''' <summary>
  133. ''' Sorts the source <see cref="IEnumerable(Of String)"/> by BubbleSort method.
  134. ''' </summary>
  135. ''' ---------------------------------------------------------------------------------------------------- '''
  136. ''' <param name="sender">
  137. ''' The source collection.
  138. ''' </param>
  139. ''' ----------------------------------------------------------------------------------------------------
  140. ''' <returns>
  141. ''' <see cref="IEnumerable(Of T)"/>.
  142. ''' </returns>
  143. ''' ----------------------------------------------------------------------------------------------------
  144. <DebuggerStepThrough>
  145. <DebuggerHidden>
  146. <Extension>
  147. Public Function BubbleSort(ByVal sender As IEnumerable(Of String)) As IEnumerable(Of String)
  148.  
  149. Return sender.Select(Function(value As String)
  150.  
  151. Return New With
  152. {
  153. Key .OrgStr = value,
  154. Key .SortStr =
  155. Regex.Replace(value, "(\d+)|(\D+)",
  156. Function(match As Match)
  157. Return match.Value.PadLeft(sender.Select(Function(str As String)
  158. Return str.Length
  159. End Function).Max,
  160. If(Char.IsDigit(match.Value(0)),
  161. " "c,
  162. Char.MaxValue))
  163.  
  164. End Function)
  165. }
  166. End Function).
  167. OrderBy(Function(anon) anon.SortStr).
  168. Select(Function(anon) anon.OrgStr)
  169.  
  170. End Function
  171.  
  172. ''' ----------------------------------------------------------------------------------------------------
  173. ''' <remarks>
  174. ''' Title : Find String-Exact In Collection.
  175. ''' Author: Elektro
  176. ''' Date : 08-March-2015
  177. ''' </remarks>
  178. ''' ----------------------------------------------------------------------------------------------------
  179. ''' <example> This is a code example.
  180. ''' <code>
  181. ''' Dim col As IEnumerable(Of String) = {"Hello World !!", "a", "b", "c"}
  182. ''' Debug.WriteLine(String.Join(", ", col.FindExact(searchString:="a", stringComparison:=StringComparison.OrdinalIgnoreCase)))
  183. ''' </code>
  184. ''' </example>
  185. ''' ----------------------------------------------------------------------------------------------------
  186. ''' <summary>
  187. ''' Finds the elements that are equals to the specified string on the source <see cref="IEnumerable(Of String)"/>.
  188. ''' </summary>
  189. ''' ----------------------------------------------------------------------------------------------------
  190. ''' <param name="sender">
  191. ''' The source collections.
  192. ''' </param>
  193. '''
  194. ''' <param name="searchString">
  195. ''' The string to search for.
  196. ''' </param>
  197. '''
  198. ''' <param name="stringComparison">
  199. ''' The string comparison rule.
  200. ''' </param>
  201. ''' ----------------------------------------------------------------------------------------------------
  202. ''' <returns>
  203. ''' <see cref="IEnumerable(Of String)"/>.
  204. ''' </returns>
  205. ''' ----------------------------------------------------------------------------------------------------
  206. <DebuggerStepThrough>
  207. <DebuggerHidden>
  208. <Extension>
  209. Public Function FindExact(ByVal sender As IEnumerable(Of String),
  210. ByVal searchString As String,
  211. ByVal stringComparison As StringComparison) As IEnumerable(Of String)
  212.  
  213. Return From value As String In sender
  214. Where value.Equals(searchString, stringComparison)
  215.  
  216. End Function
  217.  
  218. ''' ----------------------------------------------------------------------------------------------------
  219. ''' <remarks>
  220. ''' Title : Find String-Contains In Collection.
  221. ''' Author: Elektro
  222. ''' Date : 08-March-2015
  223. ''' </remarks>
  224. ''' ----------------------------------------------------------------------------------------------------
  225. ''' <example> This is a code example.
  226. ''' <code>
  227. ''' Dim col As IEnumerable(Of String) = {"Hello World !!", "a", "b", "c"}
  228. ''' Debug.WriteLine(String.Join(", ", col.FindByContains(searchString:="World", ignoreCase:=True)))
  229. ''' </code>
  230. ''' </example>
  231. ''' ----------------------------------------------------------------------------------------------------
  232. ''' <summary>
  233. ''' Finds the elements that contains the specified string on the source <see cref="IEnumerable(Of String)"/>.
  234. ''' </summary>
  235. ''' ----------------------------------------------------------------------------------------------------
  236. ''' <param name="sender">
  237. ''' The source collections.
  238. ''' </param>
  239. '''
  240. ''' <param name="searchString">
  241. ''' The string to search for.
  242. ''' </param>
  243. '''
  244. ''' <param name="ignoreCase">
  245. ''' If set to <c>true</c>, performs a non sensitive string-case comparison.
  246. ''' </param>
  247. ''' ----------------------------------------------------------------------------------------------------
  248. ''' <returns>
  249. ''' <see cref="IEnumerable(Of String)"/>.
  250. ''' </returns>
  251. ''' ----------------------------------------------------------------------------------------------------
  252. <DebuggerStepThrough>
  253. <DebuggerHidden>
  254. <Extension>
  255. Public Function FindByContains(ByVal sender As IEnumerable(Of String),
  256. ByVal searchString As String,
  257. ByVal ignoreCase As Boolean) As IEnumerable(Of String)
  258.  
  259. Return From value As String In sender
  260. Where If(ignoreCase,
  261. value.ToLower.Contains(searchString.ToLower),
  262. value.Contains(searchString))
  263.  
  264. End Function
  265.  
  266. ''' ----------------------------------------------------------------------------------------------------
  267. ''' <remarks>
  268. ''' Title : Find String-Like In Collection.
  269. ''' Author: Elektro
  270. ''' Date : 08-March-2015
  271. ''' </remarks>
  272. ''' ----------------------------------------------------------------------------------------------------
  273. ''' <example> This is a code example.
  274. ''' <code>
  275. ''' Dim col As IEnumerable(Of String) = {"Hello World", "a", "b", "c"}
  276. ''' Debug.WriteLine(String.Join(", ", col.FindByLike(likePattern:="*hello*", ignoreCase:=True)))
  277. ''' </code>
  278. ''' </example>
  279. ''' ----------------------------------------------------------------------------------------------------
  280. ''' <summary>
  281. ''' Performs a String-Like pattern search on the source <see cref="IEnumerable(Of String)"/> and returns all the matches.
  282. ''' </summary>
  283. ''' ----------------------------------------------------------------------------------------------------
  284. ''' <param name="sender">
  285. ''' The source collections.
  286. ''' </param>
  287. '''
  288. ''' <param name="likePattern">
  289. ''' The pattern comparison to use with the <see langword="Like"/> operator.
  290. ''' </param>
  291. '''
  292. ''' <param name="ignoreCase">
  293. ''' If set to <c>true</c>, performs a non sensitive string-case comparison.
  294. ''' </param>
  295. ''' ----------------------------------------------------------------------------------------------------
  296. ''' <returns>
  297. ''' <see cref="IEnumerable(Of String)"/>.
  298. ''' </returns>
  299. ''' ----------------------------------------------------------------------------------------------------
  300. <DebuggerStepThrough>
  301. <DebuggerHidden>
  302. <Extension>
  303. Public Function FindByLike(ByVal sender As IEnumerable(Of String),
  304. ByVal likePattern As String,
  305. ByVal ignoreCase As Boolean) As IEnumerable(Of String)
  306.  
  307. Return From value As String In sender
  308. Where If(ignoreCase,
  309. value.ToLower Like likePattern.ToLower,
  310. value Like likePattern)
  311.  
  312. End Function
  313.  
  314. ''' ----------------------------------------------------------------------------------------------------
  315. ''' <remarks>
  316. ''' Title : Remove String-Exact In Collection.
  317. ''' Author: Elektro
  318. ''' Date : 08-March-2015
  319. ''' </remarks>
  320. ''' ----------------------------------------------------------------------------------------------------
  321. ''' <example> This is a code example.
  322. ''' <code>
  323. ''' Dim col As IEnumerable(Of String) = {"Hello World !!", "a", "b", "c"}
  324. ''' Debug.WriteLine(String.Join(", ", col.RemoveExact(searchString:="Hello", ignoreCase:=True)))
  325. ''' </code>
  326. ''' </example>
  327. ''' ----------------------------------------------------------------------------------------------------
  328. ''' <summary>
  329. ''' Removes the elements that are equals to the specified string on the source <see cref="IEnumerable(Of String)"/>.
  330. ''' </summary>
  331. ''' ----------------------------------------------------------------------------------------------------
  332. ''' <param name="sender">
  333. ''' The source collections.
  334. ''' </param>
  335. '''
  336. ''' <param name="searchString">
  337. ''' The string to search for.
  338. ''' </param>
  339. '''
  340. ''' <param name="stringComparison">
  341. ''' The string comparison rule.
  342. ''' </param>
  343. ''' ----------------------------------------------------------------------------------------------------
  344. ''' <returns>
  345. ''' <see cref="IEnumerable(Of String)"/>.
  346. ''' </returns>
  347. ''' ----------------------------------------------------------------------------------------------------
  348. <DebuggerStepThrough>
  349. <DebuggerHidden>
  350. <Extension>
  351. Public Function RemoveExact(ByVal sender As IEnumerable(Of String),
  352. ByVal searchString As String,
  353. ByVal stringComparison As StringComparison) As IEnumerable(Of String)
  354.  
  355. Return From value As String In sender
  356. Where Not value.Equals(searchString, stringComparison)
  357.  
  358. End Function
  359.  
  360. ''' ----------------------------------------------------------------------------------------------------
  361. ''' <remarks>
  362. ''' Title : Remove String-Contains In Collection.
  363. ''' Author: Elektro
  364. ''' Date : 08-March-2015
  365. ''' </remarks>
  366. ''' ----------------------------------------------------------------------------------------------------
  367. ''' <example> This is a code example.
  368. ''' <code>
  369. ''' Dim col As IEnumerable(Of String) = {"Hello World !!", "a", "b", "c"}
  370. ''' Debug.WriteLine(String.Join(", ", col.RemoveByContains(searchString:="Hello", ignoreCase:=True)))
  371. ''' </code>
  372. ''' </example>
  373. ''' ----------------------------------------------------------------------------------------------------
  374. ''' <summary>
  375. ''' Removes the elements that contains the specified string on the source <see cref="IEnumerable(Of String)"/>.
  376. ''' </summary>
  377. ''' ----------------------------------------------------------------------------------------------------
  378. ''' <param name="sender">
  379. ''' The source collections.
  380. ''' </param>
  381. '''
  382. ''' <param name="searchString">
  383. ''' The string to search for.
  384. ''' </param>
  385. '''
  386. ''' <param name="ignoreCase">
  387. ''' If set to <c>true</c>, performs a non sensitive string-case comparison.
  388. ''' </param>
  389. ''' ----------------------------------------------------------------------------------------------------
  390. ''' <returns>
  391. ''' <see cref="IEnumerable(Of String)"/>.
  392. ''' </returns>
  393. ''' ----------------------------------------------------------------------------------------------------
  394. <DebuggerStepThrough>
  395. <DebuggerHidden>
  396. <Extension>
  397. Public Function RemoveByContains(ByVal sender As IEnumerable(Of String),
  398. ByVal searchString As String,
  399. ByVal ignoreCase As Boolean) As IEnumerable(Of String)
  400.  
  401. Return From value As String In sender
  402. Where If(ignoreCase,
  403. Not value.ToLower.Contains(searchString.ToLower),
  404. Not value.Contains(searchString))
  405.  
  406. End Function
  407.  
  408. ''' ----------------------------------------------------------------------------------------------------
  409. ''' <remarks>
  410. ''' Title : Remove String-Like In Collection.
  411. ''' Author: Elektro
  412. ''' Date : 08-March-2015
  413. ''' </remarks>
  414. ''' ----------------------------------------------------------------------------------------------------
  415. ''' <example> This is a code example.
  416. ''' <code>
  417. ''' Dim col As IEnumerable(Of String) = {"Hello World", "a", "b", "c"}
  418. ''' Debug.WriteLine(String.Join(", ", col.RemoveByLike(likePattern:="*hello*", ignoreCase:=True)))
  419. ''' </code>
  420. ''' </example>
  421. ''' ----------------------------------------------------------------------------------------------------
  422. ''' <summary>
  423. ''' Performs a String-Like pattern search on the source <see cref="IEnumerable(Of String)"/> and removes all the matches.
  424. ''' </summary>
  425. ''' ----------------------------------------------------------------------------------------------------
  426. ''' <param name="sender">
  427. ''' The source collections.
  428. ''' </param>
  429. '''
  430. ''' <param name="likePattern">
  431. ''' The pattern comparison to use with the <see langword="Like"/> operator.
  432. ''' </param>
  433. '''
  434. ''' <param name="ignoreCase">
  435. ''' If set to <c>true</c>, performs a non sensitive string-case comparison.
  436. ''' </param>
  437. ''' ----------------------------------------------------------------------------------------------------
  438. ''' <returns>
  439. ''' <see cref="IEnumerable(Of String)"/>.
  440. ''' </returns>
  441. ''' ----------------------------------------------------------------------------------------------------
  442. <DebuggerStepThrough>
  443. <DebuggerHidden>
  444. <Extension>
  445. Public Function RemoveByLike(ByVal sender As IEnumerable(Of String),
  446. ByVal likePattern As String,
  447. ByVal ignoreCase As Boolean) As IEnumerable(Of String)
  448.  
  449. Return From value As String In sender
  450. Where If(ignoreCase,
  451. Not value.ToLower Like likePattern.ToLower,
  452. Not value Like likePattern)
  453.  
  454. End Function
  455.  
  456. #End Region
  457.  
  458. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement