The C/C++ StrTok command takes two parameters, an input string and a string of delimiting characters. After the first call to StrTok, the C/C++ programmer passes a null for the input string and StrTok returns the next Token in the original input string. Sounds like an attempt to create an Enumerable function. Unfortunately standard C/C++ does not have enumerable functions.
Here is a C# enumerable function that you allows you to iterate through the tokens found in the input string. This C# version takes two parameters as well, the input string and an array of delimiting charaters.
Then using built-in power from the .net framework we:
We split the string into a string array using the delimiting characters and we yield return each element in the string array where the element in the string arrays length is greater than 0.
Code Snippet
private IEnumerable<string> StrToken(string TokenizableString, char[] Delimiters)- {
- foreach (var Token in TokenizableString.Split(Delimiters, StringSplitOptions.RemoveEmptyEntries))
- {
- yield return Token;
- }
- yield break;
- }
No comments:
Post a Comment