简体   繁体   English

C#String.replace无法正常工作

[英]C# String.replace is not working

I have this string from which I want to remove the Hosting html part. 我有一个要从其中删除主机html部分的字符串。

string r = @"2014-09-18 20:59:53|
<!-- Hosting24 Analytics Code -->
<script type=""text/javascript"" src=""http://stats.hosting24.com/count.php""></script>
<!-- End Of Analytics Code -->";

But

r = r.Replace(
@"<!-- Hosting24 Analytics Code -->
<script type=""text/javascript"" src=""http://stats.hosting24.com/count.php""></script>
<!-- End Of Analytics Code -->","");

Is not working. 不管用。 I get the same annoying original string.This has been driving me crazy 我得到了同样令人讨厌的原始字符串,这让我发疯了

From examining your string, it appears you're trying to remove everything after the first bar character. 通过检查字符串,您似乎正在尝试删除第一个bar字符之后的所有内容。 If that's the case, this might work better: 如果是这样,这可能会更好:

r = r.Split('|')[0];

The code you've posted does remove the string as you are trying to. 您发布的代码确实会删除您尝试的字符串。 You can verify this yourself here. 您可以在这里自己验证。

https://dotnetfiddle.net/p4lchi https://dotnetfiddle.net/p4lchi

The string you are ultimately examining must come from some other source. 您最终要检查的字符串必须来自其他来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM