【问题标题】:C# delete some stringC#删除一些字符串
【发布时间】:2017-06-04 03:51:28
【问题描述】:

我有这样的字符串:
<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" />

如何使它像:
<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"/>

这意味着我想删除所有字符串,例如:
content="test_img image ... - test_server" des="test_img image ... - test_server"

如何用c#做到这一点?

【问题讨论】:

标签: c#


【解决方案1】:
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, "") ;
}

【讨论】:

    【解决方案2】:

    您可以在这种情况下使用 Regex.Replace,因为您想删除与特定模式匹配的所有元素。 更多信息请查看MSDN Regex Replace

    您可以在此处找到一些示例代码:

    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);                             
    }
    

    【讨论】:

    • 回答完后我就认为“RegularExpresions 也应该可以解决问题”
    【解决方案3】:

    您可以使用基于正则表达式的替换来查找与您要删除的内容匹配的文本并将其替换为空字符串:

    Regex.Replace(src, @"content="".+?"" des="".+?"" ", "")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-23
      • 2023-03-14
      • 1970-01-01
      相关资源
      最近更新 更多