查找并替换字符串中的内容 (C#)

发布于 2024-12-15 14:44:49 字数 486 浏览 3 评论 0原文

下面的字符串来自 DIV 标记。所以我附上了下面的值。

String cLocation = "'target="_blank'></a><img alt='testimage.jpg' src='/SPECIMAGE/testimage.jpg'"

我想通过将 "src="/" 更改为 "src='xyz/files'" 来替换上面的字符串。

我尝试了典型的 它不起作用,

我尝试了以下方法,

cNewLocation ="xyz/files";
cNewString = cLocation.Replce("src='/'", "src='" + cNewLocation + "'/")

但它不起作用。

string.Replace("old","new")但

The below string is coming from a DIV tag. So I have enclosed the value below.

String cLocation = "'target="_blank'></a><img alt='testimage.jpg' src='/SPECIMAGE/testimage.jpg'"

I would like to replace in the above string by changing "src="/" with "src='xyz/files'".

I have tried the typical string.Replace("old","new") but it didn't work.

I tried the below,

cNewLocation ="xyz/files";
cNewString = cLocation.Replce("src='/'", "src='" + cNewLocation + "'/")

It didn't work.

Please suggest.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦途 2024-12-22 14:44:49

如果我理解您的要求,您可以使用正则表达式来替换字符串,如下所示:

var cNewString = Regex.Replace(cLocation, @"src='/.*/", "src='" + newLocation + "/");

编辑: 我修改了正则表达式来替换 src='/.../< /code> 与 src='{newLocation}/

If I'm understanding what you're asking, you could use Regex to replace the string like so:

var cNewString = Regex.Replace(cLocation, @"src='/.*/", "src='" + newLocation + "/");

EDIT : I modified the regular expression to replace src='/.../ with src='{newLocation}/

轮廓§ 2024-12-22 14:44:49

您可以尝试查看 C# 中的 Replace 命令。

所以 mystring = srcstring.Replace("old", "New");

http://msdn.microsoft.com/en-us/library/system.string.replace%28v=vs.71%29.aspx

可能替换 / 在带有 // 的字符串中?

you might try looking at the Replace command in c#.

so mystring = srcstring.Replace("old", "New");

http://msdn.microsoft.com/en-us/library/system.string.replace%28v=vs.71%29.aspx

possible replace the / in the string with //?

风透绣罗衣 2024-12-22 14:44:49

您可以执行以下操作:

string cLocation = "'target='_blank'></a><img alt='testimage.jpg' src='/SPECIMAGE/testimage.jpg'";
cLocation = cLocation.Replace("src='/'", "src='xyz/files'");

You can do the following:

string cLocation = "'target='_blank'></a><img alt='testimage.jpg' src='/SPECIMAGE/testimage.jpg'";
cLocation = cLocation.Replace("src='/'", "src='xyz/files'");
小耗子 2024-12-22 14:44:49

这解决了问题:

int start = cLocation.IndexOf("src='") + 5;
int end = cLocation.LastIndexOf("'");
string xcLocation = cLocation.Remove(start, end - start);
string cLocation = xcLocation.Insert(start , "xyz/files");

This fixes the problem:

int start = cLocation.IndexOf("src='") + 5;
int end = cLocation.LastIndexOf("'");
string xcLocation = cLocation.Remove(start, end - start);
string cLocation = xcLocation.Insert(start , "xyz/files");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文