Amin
Amin

Reputation: 172

Delete string into string

I have a problem with search and delete string in string. As you look in the image I want to delete the string between the balise and using my code. But it want delete so where is my problem?

string chaine = im;
int href = chaine.IndexOf("<a href");
int ahref = chaine.IndexOf("</a>");
string sup = "";
for (int c = href; c < ahref; c++)
{
    sup = sup + chaine[c];
    if (chaine[c] != ahref)
        break;
}
chaine = chaine.Replace(sup, "");
im = chaine;

my screen shoot

Upvotes: 0

Views: 153

Answers (2)

jac
jac

Reputation: 9726

Your code can be simplified. Please though add some error checking or at least a try / catch in case the sub-strings are not found.

int start = im.IndexOf("<a href");
int stop = im.IndexOf("</a>", start);
im = im.Remove(start, stop + 4 - start) // 4 is the length of the stop string

Upvotes: 2

Sahil Grover
Sahil Grover

Reputation: 219

Why don't you try a regex replace.

chaine = Regex.replace(chaine, @"\<a(?<attrs>.*)\>.*\<a/\>", m => "<a" + m.Groups["attrs"] + "></a>")

Upvotes: 1

Related Questions