Program to find how many times a word repeated in a given string using LINQ and VB.NET

Posted by Rajnilari2015 under VB.NET category on | Points: 40 | Views : 1167
Suppose we have a string of words and we need to figure out how many of them have been repeated.The below code will help to do so

Imports System.Linq
Namespace ConsoleApplication1
Class Program
Private Shared Sub Main(args As String())
Dim lstWords As String = "hello this is dotnet funda . dotnet funda is a dotnet forum"
(From g In From word In lstWords.Split(" "C)Group word By wordNew With { _
Key .Words = g.Key, _
Key .WordsRepeated = If((g.Count() - 1) = 0, [String].Concat("Word ", g.Key, " has never repeated"), [String].Concat("Word ", g.Key, " repeated ", g.Count(), " times")) _
.ForEach(Function(i) Console.WriteLine("{0} : {1}", i.Words, i.WordsRepeated))
End Sub
End Class
End Namespace

Hope this will be helpful.

Comments or Responses

Login to post response