Simple string manipulation problem driving me crazy

Hi and Happy Thanksgiving to all of you!

This is what I have tried to figure out today: I want to replace internet links in the input field of a textbox with encrypted ones before publishing the text, and everything works fine, only that it does not do anything at all towards the end of the string.

Dim headerTxt as String = [content from a textbox]
'it is no html, but for simplification, links start with "http://"
Dim linkStart As String = "http://"
Dim link As String                   'this is what I am looking for
Dim linkLength As Integer        'how long is it?
Dim NewLink As String            'this is what I want to replace link with
Dim remainString As String      'this is the entire string minus the part in front of the current link
Dim stringIndex As Integer       'this is the index in the string we are looking at

        'looping through the string to find the beginning of all links
For stringIndex = 0 To (headerTxt.Length - 8)
        'did I find a link?
  If headerTxt.Substring(stringIndex, 7) = linkStart Then
        'cut away the stuff in front of the link to tidy up the code
    remainString = headerTxt.Remove(0, stringIndex)
        'for this bugfixing, I simplified it by stating that any link ends with a space
    linkLength = remainString.IndexOf(" ")
    link = remainString.Substring(0, linkLength)
    colLinks.Add(link)
    NewLink = [encrypted tracking link]
    headerTxt = headerTxt.Replace(link, NewLink)
  End If
Next              'go to the next stringIndex

So, what could be wrong? Why is my code not looping through to the end? It looks to me as if I would be counting the length of my string correctly, or am I not?
LVL 1
ddeteringAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arif_eqbalCommented:
I feel there's a problem with the line
remainString = headerTxt.Remove(0, stringIndex)
This does not seem to fit in your Logic

Also You are looping till the Length of HeaderTxt and you are changing the text of HeaderTxt so the length will keep changing and the stringIndex might go beyond it

I hope you have a free flowing text in your textBox and you want to replace the occurance of any Links with a codede value
I have just modified your code a bit try out


Dim headerTxt As String = Me.RichTextBox1.Text
        'it is no html, but for simplification, links start with "http://"
        Dim linkStart As String = "http://"
        Dim link As String                   'this is what I am looking for
        Dim linkLength As Integer        'how long is it?
        Dim NewLink As String            'this is what I want to replace link with
        Dim remainString As String      'this is the entire string minus the part in front of the current link
        Dim stringIndex As Integer       'this is the index in the string we are looking at

        'looping through the string to find the beginning of all links
        Dim ResTxt As String = headerTxt
        'We will use this string to temperorily store the result
        For stringIndex = 0 To headerTxt.Length - 8
            If headerTxt.Substring(stringIndex, 7) = linkStart Then
                link = headerTxt.Substring(stringIndex, headerTxt.IndexOf(" ", stringIndex) - stringIndex)
                colLinks.Add(link)
                NewLink = "{Your Coded Value}"
                ResTxt = ResTxt.Replace(link, NewLink)
                stringIndex += link.Length
            End If
        Next              'go to the next stringIndex
        headerTxt=ResTxt



I have just modified the Loop

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ddeteringAuthor Commented:
Yeah! So far it seems to work. It is too late for me to figure out exactly, but I had the feeling that some of the numbers just weren't working well in the code. Now I will have to implement it into the more complex code that I have in my application, but this seems to have been cleared!
0
ddeteringAuthor Commented:
oops - forgot to close this question... Anyway, the code is working as intended and I am very happy with the answer.

Cheers!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.