Link to home
Start Free TrialLog in
Avatar of ddetering
ddetering

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of arif_eqbal
arif_eqbal

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ddetering
ddetering

ASKER

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!
oops - forgot to close this question... Anyway, the code is working as intended and I am very happy with the answer.

Cheers!