Last active
November 14, 2022 18:37
-
-
Save DanielAdeniji/6fa740ed1429f7f208e686d1de33efba to your computer and use it in GitHub Desktop.
Microsoft - .Net - C# - Get Number of Words Using Extension Classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Specialized; | |
using System.Collections.Generic; | |
using nsStringExtensions; | |
enum methodType | |
{ | |
legacy | |
, extensionMethod | |
}; | |
class HelloWorld | |
{ | |
static Char delimiter = ' '; | |
static String FORMAT_ITEM_WORDCOUNT = "{0}> {1} - [{2}]"; | |
static void Main() | |
{ | |
lyricBabyFaceSoonAsIGetHome(); | |
} | |
static void lyricBabyFaceSoonAsIGetHome() | |
{ | |
// Create and initializes a new StringCollection. | |
StringCollection objLyric = null; | |
List<int> objWordCountLegacy = null; | |
List<int> objWordCountExtensionMethod = null; | |
objLyric = new StringCollection(); | |
objLyric.Add("I give good love"); | |
objLyric.Add("I'll buy your clothes"); | |
objLyric.Add("I'll cook your dinner too (yeah)"); | |
objLyric.Add("Soon as I get home from work"); | |
objWordCountLegacy = wordCount | |
( | |
objLyric | |
// method type | |
, methodType.legacy | |
, delimiter | |
); | |
Console.WriteLine("Legacy"); | |
Console.WriteLine("------"); | |
Console.WriteLine(""); | |
wordCountList | |
( | |
objLyric | |
, objWordCountLegacy | |
, delimiter | |
, FORMAT_ITEM_WORDCOUNT | |
); | |
objWordCountExtensionMethod = | |
wordCount | |
( | |
objLyric | |
// method type | |
, methodType.extensionMethod | |
, delimiter | |
); | |
Console.WriteLine(""); | |
Console.WriteLine(""); | |
Console.WriteLine("extensionMethod"); | |
Console.WriteLine("---------------"); | |
Console.WriteLine(""); | |
wordCountList | |
( | |
objLyric | |
, objWordCountExtensionMethod | |
, delimiter | |
, FORMAT_ITEM_WORDCOUNT | |
); | |
objLyric = null; | |
objWordCountLegacy = null; | |
objWordCountExtensionMethod = null; | |
} | |
static List<int> wordCount | |
( | |
StringCollection lyricList | |
, methodType methodTypeIs | |
, Char delimiter | |
) | |
{ | |
List<int> objL = new List<int>(); | |
int iNumberOfEntries =-1; | |
int iNumberofWords = -1; | |
int i = 0; | |
String strItem = null; | |
/* | |
If object is not valid, exit function | |
*/ | |
if (lyricList == null) | |
{ | |
return null; | |
} | |
//get number of nodes in collection class | |
iNumberOfEntries = lyricList.Count; | |
//Iterate List | |
for | |
( | |
i = 0; | |
i < iNumberOfEntries; | |
i++ | |
) | |
{ | |
//reset local variables | |
iNumberofWords = -1; | |
//Get Lyric Item | |
strItem = lyricList[i]; | |
/* | |
Sanitize item before processing | |
*/ | |
//if (strItem != null) | |
{ | |
if (methodTypeIs == methodType.legacy) | |
{ | |
iNumberofWords = wordCount | |
( | |
strItem | |
, delimiter | |
); | |
} | |
else if (methodTypeIs == methodType.extensionMethod ) | |
{ | |
// call extension method | |
iNumberofWords = strItem.wordCount | |
( | |
delimiter | |
); | |
} | |
} | |
//Add Item to Array | |
objL.Add(iNumberofWords); | |
} | |
return ( objL ); | |
} | |
static void wordCountList | |
( | |
StringCollection lyricList | |
, List<int> wordCountList | |
, Char delimiter | |
, String FORMAT_ITEM_WORDCOUNT | |
) | |
{ | |
int iNumberOfEntries =-1; | |
int i = 0; | |
String strItem = null; | |
int iWordCount = 0; | |
String strLog = null; | |
//get number of nodes in collection class | |
iNumberOfEntries = lyricList.Count; | |
//Iterate List | |
for | |
( | |
i = 0; | |
i < iNumberOfEntries; | |
i++ | |
) | |
{ | |
//Get Lyric Item | |
strItem = lyricList[i]; | |
//Get Word Count | |
iWordCount = wordCountList[i]; | |
strLog = String.Format | |
( | |
FORMAT_ITEM_WORDCOUNT | |
, (i+1) | |
, strItem | |
, iWordCount | |
); | |
Console.WriteLine(strLog); | |
} | |
} | |
static int wordCount | |
( | |
String item | |
, Char delimiter | |
) | |
{ | |
int iWordCount = -1; | |
string[] strItemArray = null; | |
iWordCount = -1; | |
//split item | |
if ( | |
( item != null ) | |
) | |
{ | |
// using delimiter split word | |
strItemArray = item?.Split | |
( | |
delimiter | |
); | |
if (strItemArray != null) | |
{ | |
//get number of words | |
iWordCount = strItemArray.Length; | |
} | |
} | |
return (iWordCount); | |
} // method wordCount | |
} | |
namespace nsStringExtensions | |
{ | |
public static class stringExtensions | |
{ | |
public static int wordCount | |
( | |
this string strItem | |
, char delimiter = ' ' | |
) | |
{ | |
int iNumberofWords =-1; | |
string[] strItemArray = null; | |
if ( | |
( strItem != null) | |
) | |
{ | |
//split item | |
strItemArray = strItem?.Split(delimiter); | |
//get number of words | |
if (strItemArray != null) | |
{ | |
iNumberofWords = strItemArray.Length; | |
} | |
} | |
return (iNumberofWords); | |
} // method - wordCount | |
} //class stringExtensions | |
} //namespace - nsStringExtensions |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment