LINQ Cookbook, Recipe 2: Find all capitalized words in a phrase and sort by length (then alphabetically) (Kit George)



          Visual Studio 2008 (Beta2 or Higher)

–     Some text you want to search


Categories: LINQ-To-Objects, LINQ and strings, LINQ and WinForms



          Open Visual Studio 2008, and Click ‘File/New Project’. Find and double-click the ‘Windows Forms Application’ Icon

          Drag and drop a Listbox to the form. Size the listbox to be fairly tall. Drag and drop a button to the form

          Double-click the button, and add the following code to the button:


        Dim text = “Good morning everyone. I’d like to welcome “ & _

                   “you to today’s presentation on LINQ. My “ & _

                   “name is Kit George and I’m a Program Manager “ & _

                   “for Microsoft, on the Visual Basic team. You “ & _

                   “might be wondering where my accent is from? “ & _

                   “Well, I hail from a small country called New “ & _

                   “Zealand but it sure is great to be here in  “ & _

                   “Atlanta today!”


        Dim capitalWords = _

                      From word In text.Split( _

                      New Char() {“,”, “.”, “!”, ” “}, _

                      StringSplitOptions.RemoveEmptyEntries) _

                      Where word(0) = Char.ToUpper(word(0)) _

                      Order By word.Length, word




          Change the text being searched to the text of your choice. If your text is coming from a file, use My.Computer.FileSystem.ReadAllText to get the data into the text variable


Leave a comment

Feedback usabilla icon