如何将一些字符串插入到另一个字符串的特定部分。我想要实现的是我的变量中有一个这样的 html 字符串说 string stringContent;
<html><head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<meta name="Viewport" content="width=320; user-scaleable=no;
initial-scale=1.0">
<style type="text/css">
body {
background: black;
color: #80c0c0;
}
</style>
<script>
</script>
</head>
<body>
<button type="button" onclick="callNative();">Call to Native
Code!</button>
<br><br>
</body></html>
我需要在 <script> <script/>
中添加以下字符串内容标签
function callNative()
{
window.external.notify("Uulalaa!");
}
function addToBody(text)
{
document.body.innerHTML = document.body.innerHTML + "<br>" + text;
}
我如何在 C# 中实现这一点。
最佳答案
假设您的内容存储在字符串 content
中,您可以从查找脚本标签开始:
int scriptpos = content.IndexOf("<script");
然后越过脚本标签的末尾:
scriptpos = content.IndexOf(">", scriptpos) + 1;
最后插入您的新内容:
content = content.Insert(scriptpos, newContent);
这至少允许脚本标记中的潜在属性。
关于c# - 将一些字符串注入(inject)到 C# 中字符串的特定部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16856694/