|
Answer» I need to modify a VB function that gets a substring from between a starting string and ending string. The problem is that I need it to be able to ACCEPT multiple parameters for the ending string and stop when it finds the first instance of one of those strings. Can anyone (BC_Programmer) help me with this? Anyone who helps me with this, I will send the full SOURCE of the application when it's done if you'd like.
Here is the function: Code: [Select] Private Function GetBetween(ByVal sSearch As String, ByVal sStart As String, ByVal sStop As String, Optional ByVal lSearch As Integer = 1) As String Dim lTemp As Long
lSearch = InStr(lSearch, sSearch, sStart) If lSearch > 0 Then lSearch = lSearch + Len(sStart) lTemp = InStr(lSearch, sSearch, sStop) If lTemp > lSearch Then Return Trim(Mid$(sSearch, lSearch, lTemp - lSearch)) End If Return vbNullString End Function uses Linq:
Code: [Select] ''' <summary> ''' Searches for the first string delimited by any entry in sStart and any entry in sEnd. ''' </summary> ''' <param name="sLookin">String to look in</param> ''' <param name="sStarts">Enumerable of Strings that are valid "starts"</param> ''' <param name="sEnds">Enumerable of Strings that are valid ends</param> ''' <param name="sStart">Optional starting string location.</param> ''' <returns></returns> ''' <remarks></remarks> Private Function GetBetween(sLookin As String, sStarts As IEnumerable(Of String), sEnds As IEnumerable(Of String), Optional sStart As Integer = 0) As String
'starting from sStart, find the first occurence of any item in sStarts.
Dim firstfound As Integer = (From q In sStarts Let ttc = sLookin.IndexOf(q, sStart) Order By ttc Select ttc).First()
'we FOUND the first instance at firstfound. look for the first instance of an entry in sEnds starting there. Dim secondfound As Integer = (From q In sEnds Let tqc = sLookin.IndexOf(q, firstfound + 1) Order By tqc Select tqc).First()
Return sLookin.Substring(firstfound + 1, secondfound - firstfound - 1)
End Function
Private Function GetBetween(sLookin As String, strStart As String, strEnd As String, Optional sStart As Integer = 0) As String Return GetBetween(sLookin, New String() {strStart}, New String() {strEnd}, sStart) End Function Example of usage:
Code: [Select] Dim angletest As String = "this is a TEST of < angle brackets! > as well as some ( parentheses!)"
Console.WriteLine("-" + GetBetween(angletest, New String() {"<", "("}, New String() {">", ")"}, 0) + "-")
It's supposed to find the first instance of any of the items in the first list and then find the first instance of any element in the second list that occurs afterward, and then return the text in-between those occurences.
|