c# - 在指定字符串后添加字符串

标签 c# string

假设我有以下 HTML 字符串

<head>

</head>

<body>
<img src="stickman.gif" width="24" height="39" alt="Stickman">
<a href="http://www.w3schools.com">W3Schools</a>
</body> 

我想在 <head> 之间添加一个字符串标签。所以最终的 HTML 字符串变成了

<head>
<base href="http://www.w3schools.com/images/">
</head>

<body>
<img src="stickman.gif" width="24" height="39" alt="Stickman">
<a href="http://www.w3schools.com">W3Schools</a>
</body> 

所以我必须搜索 第一次 出现的 <head>字符串然后插入 <base href="http://www.w3schools.com/images/">紧接着。

我如何在 C# 中执行此操作。

最佳答案

那么为什么不做一些简单的事情呢

myHtmlString.Replace("<head>", "<head><base href=\"http://www.w3schools.com/images/\">");

不是最优雅或可扩展的,但满足您问题的条件。

关于c# - 在指定字符串后添加字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16517411/

相关文章:

c# - Web 部署代理服务返回 500 内部服务器错误

python - 使用 bs4 和 requests 仅获取原始链接

java - 使用正则表达式替换所有奇数出现的子字符串

c++ - 实现字符串到整数查找表的快速且可维护的方法

c# - 将 async wait 与 AcquireTokenForClient().ExecuteAsync 一起使用的正确方法

c# - ASP.NET 身份验证问题

c# - .NET Core 3 在舍入接近 0 的负数时返回 -0 字符串

c# - 按钮内容与字符串的比较

python - 使用 numpy.genfromtxt 给出 TypeError : Can't convert 'bytes' object to str implicitly

C#:反转句子中的单词