c# - 将一些字符串注入(inject)到 C# 中字符串的特定部分

标签 c# string string-concatenation

如何将一些字符串插入到另一个字符串的特定部分。我想要实现的是我的变量中有一个这样的 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/

相关文章:

c# - 字段初始化后无法从主线程访问对象

java - 如何使用 + 号和转义码连接 NETBEANS 中的字符串?

r - 如何在 R 中的单个字符串中打印向量的所有元素?

c++ - 这个boost c++正则表达式代码有什么问题?

c# - 一串重复字符的正则表达式,最后还有另一个可选字符

c# - 在 Windows Azure 辅助角色中发布 MongoDB - 始终忙碌

c# - 使用 LINQ 将两个不同类型的 IQueryable 变量连接在一起

swift - 字符串到日期转换后得到 nil

c# - Newtonsoft.Json.Linq.JArray 到字符串数组 C#

c - 尝试用空格拆分 C 中的字符串并使用第二个单词