Wednesday, October 05, 2005

A VB.Net function to return the text contained within the first set of quotes in a string

I needed a quick function to return the text within the first set of quotes in a particular string. In case you need a similar function at some point, it is listed below for your use. I hope you find it helpful.

Joe Kunk



'Returns the text within the first set of quotes found
Function GetTextWithinQuotes(ByVal strSource As String) As String
Const Quote As Char = Chr(34)
If strSource.IndexOf(Quote) = -1 Then Return String.Empty
Dim StartPos As Integer = strSource.IndexOf(Quote)
Dim EndPos As Integer = strSource.IndexOf(Quote, StartPos + 1)
If (EndPos - StartPos = 1) Then Return String.Empty
Return strSource.Substring(StartPos + 1, (EndPos - StartPos - 1))
End Function

No comments: