Extract N Number Of Words From String

This VB.Net function takes a string parameter and extracts from it the first N number of words using String.Split() method. The result is returned as a string.



Function GetNewString(ByVal mystr As String) As String

Dim finStr As String = ""

Dim string1 As String = mystr

Dim count As Integer

Dim words1() As String

Dim strSeparator As Char = (" ")

words1 = string1.Split(strSeparator)

If words1.Length - 1 < 10 Then

For count = 0 To words1.Length - 1

finStr &= words1(count) & " "

Next

Else

For count = 0 To 10

finStr &= words1(count) & " "

Next

End If

Return finStr

End Function