Friday, August 26, 2016

Extract specific number of words from string

The following function is useful where there is a need to display a snippet of text. Tested in real life  VB.NET application.

  Function GetNewString(ByVal mystr As String) As String
        Dim finStr As String = ""
        Dim count As Integer
        Dim words1() As String
        Dim strSeparator As Char = (" ")
        words1 = mystr.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

The function returns 10 words from a string that is passed as a string parameter.

No comments:

Post a Comment