• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

Replacing/joining strings in vb.net

Hello experts,
I am using the function below to loop through and replace all image src tag in the htmldata with a different image source. The function does it's job but takes too long. After some testing, I was able to figure out "HtmlData = HtmlData.Substring(0, X) & NewImageTag & HtmlData.Substring(Y + 1)" is taking up almost 80% of the time it takes to finish the call. How can I speed this up? Thanks


Public Function ReplaceImageSRC(ByVal HtmlData As String, ByVal PageURL As String) As String
        Dim X As Integer = -1, Y As Integer = -1, ImageStartTag As String = "<IMG ", ImageEndTag As String = ">"
        Do
            X = HtmlData.IndexOf(ImageStartTag, X + 1, System.StringComparison.CurrentCultureIgnoreCase)
            If X <> -1 Then
                Y = HtmlData.IndexOf(ImageEndTag, X)
                If Y > X Then
                    Dim NewImageTag As String = ""
                    Dim ImageTag As String = HtmlData.Substring(X, Y - X + 1)
                    Dim ImageSourceData As TagAttributeData = GetHTMLTagAttribute(ImageTag, "SRC")
                    If ImageSourceData.AttributeData <> "" Then
                        NewImageTag = ImageTag.Substring(0, ImageSourceData.StartPos) & "newimagesrchere" & ImageTag.Substring(ImageSourceData.EndPos)
                    End If
                    HtmlData = HtmlData.Substring(0, X) & NewImageTag & HtmlData.Substring(Y + 1)
                Else
                    Exit Do
                End If
            Else
                Exit Do
            End If
        Loop
        Return HtmlData
    End Function
0
mxpoint
Asked:
mxpoint
  • 5
  • 3
  • 2
1 Solution
 
hiteshgoldeneyeCommented:
0
 
vbturboCommented:
I cant say if it is any faster but try it

replace all text in tags that starts with

 <img src="Images/top.gif">

and parse the content of (my_own_string_text) variable

String my_own_string_text ="test.gif"

<img src="test.gif">

inputData = Regex.Replace(inputData, "(<img.*?src=)(""|').+?(""|')", "$1" + my_own_string_text)

vbturbo
0
 
hiteshgoldeneyeCommented:
Use StringBuilder for faster performance here is one more link
http://pinhoes.blogspot.com/2007/08/speed-up-net.html
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
vbturboCommented:
Good comment from hiteshgoldeneye - Denfently the way to go
0
 
mxpointAuthor Commented:
I thought about using stringbuilder. But how will you use stringbuilder in my function? stringbuilder don't have indexof or substring methods.
0
 
mxpointAuthor Commented:
Ok, I made some changes to the function, now the function load the htmldata into a stringbuilder and replace the img src in the stringbuilder instead of htmldata. I am not sure if this is the right way to get the job done but the code is 10x faster.

    Public Function ReplaceImageSRC(ByVal HtmlData As String, ByVal PageURL As String) As String
        Dim X As Integer = -1, Y As Integer = -1, ImageStartTag As String = "<IMG ", ImageEndTag As String = ">", NewImageTag As String, sbPos As Integer
        Dim sbHtmlData As New System.Text.StringBuilder(HtmlData)
        Do
            X = HtmlData.IndexOf(ImageStartTag, X + 1, System.StringComparison.CurrentCultureIgnoreCase)
            If X <> -1 Then
                Y = HtmlData.IndexOf(ImageEndTag, X)
                If Y > X Then
                    Dim ImageTag As String = HtmlData.Substring(X, Y - X + 1)
                    Dim ImageSourceData As TagAttributeData = GetHTMLTagAttribute(ImageTag, "SRC")
                    If ImageSourceData.AttributeData.Length <> 0 Then
                        NewImageTag = ImageTag.Substring(0, ImageSourceData.StartPos) & "newimagesrchere" & ImageTag.Substring(ImageSourceData.EndPos)
                        sbHtmlData.Remove(X + sbPos, Y - X + 1)
                        sbHtmlData.Insert(X + sbPos, NewImageTag)
                        If NewImageTag.Length > ImageTag.Length Then
                            sbPos = sbPos + (NewImageTag.Length - ImageTag.Length)
                        Else
                            sbPos = sbPos - (ImageTag.Length - NewImageTag.Length)
                        End If
                    End If
                Else
                    Exit Do
                End If
            Else
                Exit Do
            End If
        Loop
        Return sbHtmlData.ToString
    End Function
0
 
hiteshgoldeneyeCommented:
No please read the following link properly, you have not utilised the Append function of StringBuilder
http://www.codeproject.com/useritems/Speed_up__Web_application.asp

'try the following code
 Public Function ReplaceImageSRC(ByVal HtmlData As String, ByVal PageURL As String) As String
        Dim X As Integer = -1, Y As Integer = -1, ImageStartTag As String = "<IMG ", ImageEndTag As String = ">", NewImageTag As New System.Text.StringBuilder(), sbPos As Integer
        Dim sbHtmlData As New System.Text.StringBuilder(HtmlData)
        Do
            X = HtmlData.IndexOf(ImageStartTag, X + 1, System.StringComparison.CurrentCultureIgnoreCase)
            If X <> -1 Then
                Y = HtmlData.IndexOf(ImageEndTag, X)
                If Y > X Then
                    Dim ImageTag As String = HtmlData.Substring(X, Y - X + 1)
                    Dim ImageSourceData As TagAttributeData = GetHTMLTagAttribute(ImageTag, "SRC")
                    If ImageSourceData.AttributeData.Length <> 0 Then
                        NewImageTag.Append(ImageTag.Substring(0, ImageSourceData.StartPos))
NewImageTag.Append("newimagesrchere")
NewImageTag.Append(ImageTag.Substring(ImageSourceData.EndPos))
                       Dim mystring As New String(NewImageTag.toString())
                        sbHtmlData.Remove(X + sbPos, Y - X + 1)
                        sbHtmlData.Insert(X + sbPos, NewImageTag)
                        If mystring.Length > ImageTag.Length Then
                            sbPos = sbPos + (mystring.Length - ImageTag.Length)
                        Else
                            sbPos = sbPos - (ImageTag.Length - mystring.Length)
                        End If
                    End If
                Else
                    Exit Do
                End If
            Else
                Exit Do
            End If
        Loop
        Return sbHtmlData.ToString
    End Function
0
 
hiteshgoldeneyeCommented:
sorry made a mistake in the above code try this
Public Function ReplaceImageSRC(ByVal HtmlData As String, ByVal PageURL As String) As String
        Dim X As Integer = -1, Y As Integer = -1, ImageStartTag As String = "<IMG ", ImageEndTag As String = ">", NewImageTag As New System.Text.StringBuilder(), sbPos As Integer,mystring as String
        Dim sbHtmlData As New System.Text.StringBuilder(HtmlData)
        Do
            X = HtmlData.IndexOf(ImageStartTag, X + 1, System.StringComparison.CurrentCultureIgnoreCase)
            If X <> -1 Then
                Y = HtmlData.IndexOf(ImageEndTag, X)
                If Y > X Then
                    Dim ImageTag As String = HtmlData.Substring(X, Y - X + 1)
                    Dim ImageSourceData As TagAttributeData = GetHTMLTagAttribute(ImageTag, "SRC")
                    If ImageSourceData.AttributeData.Length <> 0 Then
                        NewImageTag.Append(ImageTag.Substring(0, ImageSourceData.StartPos))
NewImageTag.Append("newimagesrchere")
NewImageTag.Append(ImageTag.Substring(ImageSourceData.EndPos))
                       mystring =NewImageTag.toString()
                        sbHtmlData.Remove(X + sbPos, Y - X + 1)
                        sbHtmlData.Insert(X + sbPos, mystring)
                        If mystring.Length > ImageTag.Length Then
                            sbPos = sbPos + (mystring.Length - ImageTag.Length)
                        Else
                            sbPos = sbPos - (ImageTag.Length - mystring.Length)
                        End If
                    End If
                Else
                    Exit Do
                End If
            Else
                Exit Do
            End If
        Loop
        Return sbHtmlData.ToString
    End Function

0
 
mxpointAuthor Commented:
In the above code you changed newimagetag to stringbuilder instead of string. I ran the code but it is actually slower than the code I posted before. About four times slower.
0
 
hiteshgoldeneyeCommented:
i think you will need to clear the NewImageTag in above loop every time in the loop, otherwise it will keep appending and forming a large string, got it?

Public Function ReplaceImageSRC(ByVal HtmlData As String, ByVal PageURL As String) As String
        Dim X As Integer = -1, Y As Integer = -1, ImageStartTag As String = "<IMG ", ImageEndTag As String = ">", NewImageTag As New System.Text.StringBuilder(), sbPos As Integer,mystring as String
        Dim sbHtmlData As New System.Text.StringBuilder(HtmlData)
        Do
            X = HtmlData.IndexOf(ImageStartTag, X + 1, System.StringComparison.CurrentCultureIgnoreCase)
            If X <> -1 Then
                Y = HtmlData.IndexOf(ImageEndTag, X)
                If Y > X Then
                    Dim ImageTag As String = HtmlData.Substring(X, Y - X + 1)
                    Dim ImageSourceData As TagAttributeData = GetHTMLTagAttribute(ImageTag, "SRC")
                    If ImageSourceData.AttributeData.Length <> 0 Then
                        NewImageTag.Append(ImageTag.Substring(0, ImageSourceData.StartPos))
NewImageTag.Append("newimagesrchere")
NewImageTag.Append(ImageTag.Substring(ImageSourceData.EndPos))
                       mystring =NewImageTag.toString()
                       NewImageTag.length=0 'add this line                        
                        sbHtmlData.Remove(X + sbPos, Y - X + 1)
                        sbHtmlData.Insert(X + sbPos, mystring)
                        If mystring.Length > ImageTag.Length Then
                            sbPos = sbPos + (mystring.Length - ImageTag.Length)
                        Else
                            sbPos = sbPos - (ImageTag.Length - mystring.Length)
                        End If
                    End If
                Else
                    Exit Do
                End If
            Else
                Exit Do
            End If
        Loop
        Return sbHtmlData.ToString
    End Function

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now