Friday, April 4, 2008

Index into words in string array

The programming problem that I recently ran across was as follows: I had a string of text and I needed to turn that into an array of strings and get a list of where a certain word occurred inside that array. In other words an int array that indexes a word in a string array.

I believe that this is how compression algorithms such as zip work. They store indexes of common occurring words instead of the words themselves.

Here is the code that solved the problem for me using the latest in C# 3.0 and LINQ:

string sentence = "this is an array with is in it three is times";

string[] words = sentence.Split(' ');words.Select((wrds, index) => new {Word = wrds, Indx = index}).Where(a => a.Word == "is").Select(a => a.Indx).ToArray().Dump();


No comments:

Post a Comment