Substring a stringRaw from String1 to String2

125 Views Asked by At

If you didn't really get it what I actually want let me explain you better:

I have a:

stringRaw = "<img src=\"http://www.b92.net/news/pics/2015/01/15/111348233854b817af5b4bc991063053_v4 big.jpg\" title=\"\" alt=\"\" />"  

Now I have to do some kind of substring from string1 = "<img src=\"" to string2 = "\" title=\"\" alt=\"\" />" and have to take what is in the middle of that which will be stringFinal = "http://www.b92.net/news/pics/2015/01/15/111348233854b817af5b4bc991063053_v4 big.jpg" in this case.

I can't use substring method since I never know how many characters can be in string1 and string2 also, seems like split method do not work as it should from THIS question on StackOverFlow.

edit For some reason my stringRaw has this value: vbCrLf & vbTab & vbTab & vbTab & "<img src=""http://www.b92.net/news/pics/2015/02/05/203819435354d39f17d74f0173575440_v4 big.jpg"" title="""" alt="""" />" & vbTab & vbTab
Anyway, I do what others advised and still I get an error that Index is out of range

Thank you for the answers in advance.

5

There are 5 best solutions below

4
On BEST ANSWER

I was having issues going after the same characters so I went after text within the string such as "http" and "title=". Here it is in VB:

    Dim startIndex As Int32 = stringRaw.IndexOf("http")
    Dim endIndex As Int32 = stringRaw.IndexOf("title=") - 2
    Return stringRaw.Substring(startIndex, endIndex - startIndex)

I used "http" and didn't include "://" just in case you need to get an "https" at some point. I used "title=" instead of "title" in case the word title ever showed up in the link.

2
On

I believe this code should grab the 'stringFinal' you desire.

// Get the index of 'src="'
Int32 startIndex = stringRaw.IndexOf("src=\"");

// Get the substring starting at 'src="' and ending at the first '"' after 'src="'.
stringRaw.Substring(startIndex, stringRaw.IndexOf("\"", startIndex) - startIndex);

Oh, sorry just noticed the VB tag. This is C# so the sytnax might be slightly different, but the functions and parameters should still be the same.

I forgot the - startIndex I was thinking that SubString second parameter was an index instead of count. Try adding that adjustment.

1
On

As mentioned in my comment about you could use Regex to accomplish this easily. This uses a Positive-LookBehind and Positive-LookAhead. Please see below for example. This is tried and tested as well.

  Dim rgex As New Regex("((?<=src=\\"").*)(?=\\""\s+title=)")
  Dim mtch As Match = rgex.Match(TextBox1.Text)

  If mtch.Success Then
       MessageBox.Show(mtch.Groups(1).Value)
  End If

enter image description here

3
On

You could also use a regular expression. Something like:

Dim stringRaw as String = vbCrLf & vbTab & vbTab & vbTab & "<img src=""http://www.b92.net/news/pics/2015/02/05/203819435354d39f17d74f0173575440_v4 big.jpg"" title="""" alt="""" />" & vbTab & vbTab
Dim regx as New Regex("src=\""(.*)\""\s+title")
Dim match as Match = regx.Match(stringRaw)

If match.Success
    ' Outputs http://www.b92.net/news/pics/2015/02/05/203819435354d39f17d74f0173575440_v4 big.jpg
    Console.WriteLine(match.Groups(1).Value)
End If
0
On
Public Function ExtractSrcAttribute(ByVal element As String) As String
    Dim startToken As String = "src="""
    Dim endToken As String = """"

    Dim startIndex As Integer = element.IndexOf(startToken) + startToken.Length
    Dim endIndex As Integer = element.IndexOf(endToken, startIndex)

    If startIndex > element.Length OrElse endIndex > element.Length OrElse endIndex < startIndex Then
        'error
    End If

    Return element.SubString(startIndex, endIndex)
End Function