Question Find strings inside strings

Gopher2011

Well-known member
Joined
Mar 3, 2011
Messages
111
Location
South England
Programming Experience
10+
Hi,

I have been looking for examples to find the string between two strings. This top one works fine;

VB.NET:
Public Sub ReadData(ByRef keywordStart As String, ByRef keywordEnd As String, ByVal filename As String)


        Using reader = New StreamReader(filename)
            Dim line As String = reader.ReadToEnd()
            'Debug.WriteLine(line)
            Dim Index_Vol1 As Integer = line.IndexOf("<Volume>") + 8
            Dim Index_Vol2 As Integer = line.IndexOf("</Volume>")
            Dim Ext_Volume As String = line.Substring(Index_Vol1, Index_Vol2 - Index_Vol1).Trim
            Debug.WriteLine("Extraction: " & Ext_Volume)


            Dim Index_Exp1 As Integer = line.IndexOf("^FDExp:") + 7
            Dim Index_Exp2 As Integer = line.IndexOf("^FS")
            Dim Ext_Exp As String = line.Substring(Index_Exp1, Index_Exp2 - Index_Exp1).Trim
            Debug.WriteLine("Extraction: " & Ext_Exp)


        End Using
    End Sub


Now the first one is fine - Ext_Volume is result of the string between the strings <Volume> and </Volume>.
<Volume> and </Volume> are unique so this is straight forward.

However the second one - "^FDExp:" is unique, but "^FS" is not unique.
There are occurances of "^FS" before and after "^FDExp:".

How do I get the string to search AFTER the occurrence, not before etc?

Sorry I cant help more but I really don't know the terminology of the words for what I am trying to do, so its harder to find on google.
Most examples are to find fixed sized strings using mid and the number of chars you want to get etc.
 
How do I get the string to search AFTER the occurrence, not before etc?
startIndex parameter can be specified for IndexOf method.
<Volume> and </Volume>
You are searching some markup that is not html, if it's xml you should probably use the xml tools.
 
Fixed.

VB.NET:
            Dim Index_Exp1 As Integer = line.IndexOf("^FDExp:") + 7
            Dim Index_Exp2 As Integer = InStr(Index_Exp1, line, "^FS", CompareMethod.Text)
            Dim Ext_Exp As String = line.Substring(Index_Exp1, (Index_Exp2 - 1) - Index_Exp1).Trim
            Debug.WriteLine("Extraction: " & Ext_Exp)
            rtb_Code.AppendText(Ext_Exp & vbCrLf)
 
Last edited:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'data String
Dim v As String = "<\\apple/0/><\\brown/0/><\\jelly/0/><\\peach/0/><\\pearl/0/>"

'Convert Return String To array
Dim n As Array = ReturnMiddle("<\\", "/0/>", v).ToString.Split(",")

'Loop Through Elements
For i As Integer = 0 To UBound(n)
MsgBox(n(i))
Next
End Sub

'*****Updated 8/16/2011
'*****Now works with variable sized and structured tags.
'*****Document must however be well formed with both opening and closing tags.

Private Function ReturnMiddle(ByVal sStart As String, ByVal sEnd As String, ByVal sData As String) As String
Dim nPos As Integer
Dim cPos As Integer
Dim tLen As Integer
Dim s As String = ""

Do
If Not sData.IndexOf(sStart, nPos) = -1 Then

cPos = nPos
nPos = sData.IndexOf(sEnd, cPos)

tLen = (nPos - cPos) - sStart.Length

s += sData.Substring(cPos + sStart.Length, tLen) & ","

nPos = nPos + sEnd.Length

Else
Exit Do
End If
Loop

s = s.Substring(0, s.LastIndexOf(","))

Return s
End Function
 
Last edited:
Back
Top