1

I have a string of text coming from a database. I also have a list of links from a database which have a start index and length correstponding to my string. I want to append the links within the text to be links

<a href=...

I.e

var stringText = "Hello look at http://www.google.com and this hello.co.uk";

This would have in the database

Link:http://www.google.com
Index:14
Length:21

Link:hello.co.uk
Index:45
Length:11

I eventually want

var stringText = "Hello look at <a href="http://www.google.com">http://www.google.com</a> and this <a href="hello.co.uk">hello.co.uk</a>";

There may be many links in the string, so I need a way of looping through these links and replacing based on the index and length. I would just loop through and replace based on the link (string.replace) but causes issues if there are the same link twice

var stringText = "www.google.com www.google.com www.google.com";

www.google.com would become a link and the second time would make the link within the link... a link.

I can obviously find the first index, but if I change it at that point, the index's are no longer valid.

Is there an easy way to do this or am I missing something?

MichaelMcCabe
  • 523
  • 4
  • 15
  • 33

3 Answers3

3

You simply need to remove the subject from source using String.Remove, then use String.Insert to insert your replacement string.

As @hogan suggested in comments you need to sort the replacement list and do the replacement in reverse order (from last to first) to make it work.

If you need to perform many replacements in single string I recommend StringBuilder for performance reasons.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
0

I would use regural expressions.

Take a look at this: Regular expression to find URLs within a string

It might help.

Community
  • 1
  • 1
Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
  • or look at the `URI` Class in .NET if you are not familiar with RegEx – MethodMan Dec 19 '14 at 15:58
  • That will replace all occurrences of the string isn't it? That's not what OP wants. – Sriram Sakthivel Dec 19 '14 at 15:58
  • This has already been done, its why I have the indices of the URLs in a table from originally using Regex – MichaelMcCabe Dec 19 '14 at 16:01
  • Yes but it will not pose the problem of multiple replacements, it will replace each url once. – Athanasios Kataras Dec 19 '14 at 16:01
  • Then why not use the regex to find these and replace with link? Also why not doing the replacement before you concat the string with multiple urls and just add the link strings afterwards? – Athanasios Kataras Dec 19 '14 at 16:02
  • I did. I was using regex again to replace the strings saving back to the original text. If the text contained the same URL twice, I save it twice. So the regex finds that expression twice and runs the code twice. This caused it to replace all occurrences on the first time, and then all the internal links again the second time – MichaelMcCabe Dec 19 '14 at 16:16
  • No I mean use regex to identity URL patterns and replace them.One regex one replacement. – Athanasios Kataras Dec 22 '14 at 08:35
0

Here's solution without Remove or Insert, or regexes. Just addition.

string stringText = "Hello look at http://www.google.com and this hello.co.uk!";

var replacements = new [] {
    new { Link = "http://www.google.com", Index = 14, Length = 21 },
    new { Link = "hello.co.uk", Index = 45, Length = 11 } };

string result = "";
for (int i = 0; i <= replacements.Length; i++)
{
    int previousIndex = i == 0 ? 0 : replacements[i - 1].Index + replacements[i - 1].Length;
    int nextIndex = i < replacements.Length ? replacements[i].Index : replacements[i - 1].Index + replacements[i - 1].Length + 1;

    result += stringText.Substring(previousIndex, nextIndex - previousIndex);

    if (i < replacements.Length)
    {
        result += String.Format("<a href=\"{0}\">{1}</a>", replacements[i].Link,
            stringText.Substring(replacements[i].Index, replacements[i].Length));
    }
}
Dialecticus
  • 16,400
  • 7
  • 43
  • 103