查找并替换字符串中的内容 (C#)
下面的字符串来自 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果我理解您的要求,您可以使用正则表达式来替换字符串,如下所示:
编辑: 我修改了正则表达式来替换
src='/.../< /code> 与
src='{newLocation}/
If I'm understanding what you're asking, you could use Regex to replace the string like so:
EDIT : I modified the regular expression to replace
src='/.../
withsrc='{newLocation}/
您可以尝试查看 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//
?您可以执行以下操作:
You can do the following:
这解决了问题:
This fixes the problem: