[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 195
  • Last Modified:

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?
0
ddetering
Asked:
ddetering
  • 2
1 Solution
 
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
 
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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