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

Help on a string function

The function below works just fine if I have one link in the string from Text-box, but it duplicates the first link if I have more than one link in the string.  
Any idea how can I make it works with more than one link?

Thank you!

 public string ConvLkToStr(string xstr)
        {
            string str = xstr;
            string finalStr = xstr;
            int slength = finalStr.Length;

            int strBigging = finalStr.IndexOf("http");

            if (strBigging == -1)
            {
                strBigging = finalStr.IndexOf("https");
            }
            else if (strBigging == -1)
            {
                strBigging = finalStr.IndexOf("www");
            }
            int strEnding = finalStr.IndexOf(" target=_blank");

            if (strBigging != -1 || strEnding != -1)
            {
                string recoveredLink = finalStr.Substring(strBigging, strEnding - strBigging);                
                str = str.Replace("Click_Link", recoveredLink);

                return Regex.Replace(str, "<(.|\n)*?>", string.Empty); 
            }
             else
            {
             return str;
            }
        }

Open in new window

0
Rad1
Asked:
Rad1
  • 5
  • 3
1 Solution
 
Rad1Author Commented:
By the way:

"Click_Link" is a hyperlink I made using another function.
0
 
Tom BeckCommented:
When you have more than one link in the string, are you first splitting them up and then running this function once for each string part of the textbox value? Or are you running this function once with one string that has two links inside?

Also, this looks a little odd. The second conditional can never be reached under any circumstances.

            if (strBigging == -1)
            {
                strBigging = finalStr.IndexOf("https");
            }
            else if (strBigging == -1)
            {
                strBigging = finalStr.IndexOf("www");
            }
0
 
Rad1Author Commented:
I am running it once.
And thank you for pointing this out.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
Rad1Author Commented:
One string
0
 
Tom BeckCommented:
Here's the way I'm reading your code. You have a string that contains an URL. The code progresses and when you get to this line:
string recoveredLink = finalStr.Substring(strBigging, strEnding - strBigging);

Now the recoveredLink variable contains the extracted URL. But you don't use it for anything except to replace the word "Click_Link" with the extracted URL in the original string that was passed to the function. In other words, if your original string contains "http://www.google.com Click_Link", the string returned from the function will be "http://www.google.com http://www.google.com". On the other hand if the original string contains "Click_Link http://www.google.com http://www.yahoo.com" (two URLs) then the returned string will be "http://www.google.com http://www.google.com http://www.yahoo.com".

Hope that helps to clarify what is currently going on with your code.
0
 
Rad1Author Commented:
Yes.
0
 
Jaime OlivaresCommented:
side comment: all this algorithm seems to be unnecessary. You can use the Uri class to parse the URL components; of course, after splitting multiple URLs.
Tutorial blogs:
http://code.rawlinson.us/2008/07/c-parsing-url-for-its-component-parts.html
http://www.stev.org/post/2011/06/27/C-HowTo-Parse-a-URL.aspx
0
 
Rad1Author Commented:
Ok, thank you!
0
 
Tom BeckCommented:
You're welcome. @jaimie_olivares says "thanks for the points".
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

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