-5

i have string like :
<img src="test/1.jpg" content="test_img image 1 - test_server" des="test_img image 1 - test_server" /><img src="test/2.jpg" content="test_img image 2 - test_server" des="test_img image 2 - test_server" /><img src="test/3.jpg" content="test_img image 3 - test_server" des="test_img image 3 - test_server" /><img src="test/4.jpg" content="test_img image 4 - test_server" des="test_img image 4 - test_server" /><img src="test/5.jpg" content="test_img image 5 - test_server" des="test_img image 5 - test_server" /><img src="test/6.jpg" content="test_img image 6 - test_server" des="test_img image 6 - test_server" /><img src="test/7.jpg" content="test_img image 7 - test_server" des="test_img image 7 - test_server" /><img src="test/8.jpg" content="test_img image 8 - test_server" des="test_img image 8 - test_server" /><img src="test/9.jpg" content="test_img image 9 - test_server" des="test_img image 9 - test_server" /><img src="test/10.jpg" content="test_img image 10 - test_server" des="test_img image 10 - test_server" /><img src="test/11.jpg" content="test_img image 11 - test_server" des="test_img image 11 - test_server" /><img src="test/12.jpg" content="test_img image 12 - test_server" des="test_img image 12 - test_server" />

how to make it like :
<img src="test/1.jpg"/><img src="test/2.jpg"/><img src="test/3.jpg"/><img src="test/4.jpg"/><img src="test/5.jpg"/><img src="test/8.jpg"/><img src="test/9.jpg"/><img src="test/10.jpg"/><img src="test/11.jpg"/><img src="test/12.jpg"/>

that mean i want to delete all string like :
content="test_img image ... - test_server" des="test_img image ... - test_server"

how to do it with c#?

1234abcd
  • 467
  • 2
  • 5
  • 15

3 Answers3

0
String imgTexts ; //String that contains the <img texts...
String strToRemove ;

for(int i=1; i<=12;i++)
{
    //build the sctring that is going to be removed
    strToRemove = String.Format(" content=\"test_img image {0} - test_server\" des=\"test_img image {0} - test_server\" ", i) ;

    //replace the strToRemove with a empty string
    imgTexts = imgTexts.Replace(strToRemove, "") ;
}
Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
0

You could use Regex.Replace for this case, as you want to delete all elements that match a certain pattern. For more information check MSDN Regex Replace

Here is some example code you can find there:

public static void Main()
{
  string pattern =  "<your regex pattern>";
  string input = "<your html input>";
  string replacement = string.Empty;
  Regex rgx = new Regex(pattern);
  string result = rgx.Replace(input, replacement);

  Console.WriteLine("Original String:    '{0}'", input);
  Console.WriteLine("Replacement String: '{0}'", result);                             
}
rovinos
  • 222
  • 1
  • 3
  • 7
0

You can use a Regular Expression-based replacement to find text matching what you want to remove and replace it with an empty string:

Regex.Replace(src, @"content="".+?"" des="".+?"" ", "")
John M. Wright
  • 4,477
  • 1
  • 43
  • 61
NetMage
  • 26,163
  • 3
  • 34
  • 55