C# 在C语言中向字符串的特定部分注入一些字符串#
如何将某个字符串插入到另一个字符串的特定部分。我试图实现的是,我在变量sayC# 在C语言中向字符串的特定部分注入一些字符串#,c#,string,string-concatenation,C#,String,String Concatenation,如何将某个字符串插入到另一个字符串的特定部分。我试图实现的是,我在变量saystringstringcontent中有一个这样的html字符串 <html><head> <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"> <meta name="Viewport" content="width=320; user-scaleable=no; initi
stringstringcontent中有一个这样的html字符串代码>
<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>
身体{
背景:黑色;
颜色:#80c0c0;
}
打电话给本地人
密码!
我需要在
标记中添加以下字符串内容
function callNative()
{
window.external.notify("Uulalaa!");
}
function addToBody(text)
{
document.body.innerHTML = document.body.innerHTML + "<br>" + text;
}
函数callNative()
{
window.external.notify(“Uulalaa!”);
}
函数addToBody(文本)
{
document.body.innerHTML=document.body.innerHTML+“
”+文本;
}
如何在C#中实现这一点 var htmlString=“你的html bla bla bla脚本标记在哪里?这里就是!!!”;
var yourScript=“警报('HA-HA-HA!!!')”;
htmlString=htmlString.Insert(html.IndexOf(“”+“”).Length+1,您的脚本);
使用htmlString。替换(what,with)
var htmlString=“你的html bla bla bla脚本标记在哪里?这里就是!!!”;
var yourScript=“警报('HA-HA-HA!!!')”;
htmlString=htmlString.Replace(“,”+yourScript);
请注意,这将在所有
元素中插入yourScript
。var htmlString=@“$var1$var2”
var htmlString = @"<script>$var1</script> <script>$var2</script>"
.Replace("$var1", "alert('var1')")
.Replace("$var2", "alert('var2')");
.替换(“$var1”,“警报('var1')”)
.替换($var2)、“警报($var2”);
假设您的内容存储在字符串content
中,您可以通过以下方式查找脚本标记开始:
int scriptpos = content.IndexOf("<script");
最后插入新内容:
content = content.Insert(scriptpos, newContent);
这至少允许脚本标记中存在潜在属性。为此,可以使用file.ReadAllText方法将html文件读入字符串。例如,在这里,我使用了示例html字符串。之后,通过一些字符串操作,您可以在脚本下添加标记,如下所示
string text = "<test> 10 </test>";
string htmlString =
@" <html>
<head>
<script>
<tag1> 5 </tag1>
</script>
</head>
</html>";
int startIndex = htmlString.IndexOf("<script>");
int length = htmlString.IndexOf("</script>") - startIndex;
string scriptTag = htmlString.Substring(startIndex, length) + "</script>";
string expectedScripTag = scriptTag.Replace("<script>", "<script><br>" + text);
htmlString = htmlString.Replace(scriptTag, expectedScripTag);
string text=“10”;
字符串htmlString=
@"
5.
";
int startIndex=htmlString.IndexOf(“”);
int length=htmlString.IndexOf(“”)-startIndex;
string scriptTag=htmlString.Substring(startIndex,length)+“”;
字符串expectedScripTag=scriptTag.Replace(“,”
“+文本);
htmlString=htmlString.Replace(scriptTag,expectedScripTag);
这可以通过另一种(更安全的)方式完成,使用HTML Agility Pack(开源项目)。它可以帮助您解析和编辑html,而无需担心格式错误的标记(
、
、
、
等)。它包括易于插入元素的操作,如AppendChild
如果你正在处理HTML,这就是方法。
查看StrugBu建dRead。考虑使用RaSoRexEng.另一个答案是使用<代码>替换< /代码>方法建议的?!恐怕这是个错误。。我只是删除了评论,谢谢你的努力。对于ans+1,我会给你+1,复制我的示例文本没什么不好的,代码本身是不同的,所以答案是绝对正确的。@arturudo:Sry麻烦了,我没有找到更改部分。这是我的道歉。我很抱歉做出这样的评论。@StezPet,绝对没有冒犯的意思,我只是想说我不介意使用我答案的一部分,因为我们正在进行一个面向解决方案的讨论,我相信你不应该道歉。你不应该忘记字符串在C#和内容中是不可变的。Insert
不会修改内容,所以你必须用新的值重新赋值。所有答案都可以接受。。但我之所以给这一次打分,只是因为它获得了更多的选票:)
scriptpos = content.IndexOf(">", scriptpos) + 1;
content = content.Insert(scriptpos, newContent);
string text = "<test> 10 </test>";
string htmlString =
@" <html>
<head>
<script>
<tag1> 5 </tag1>
</script>
</head>
</html>";
int startIndex = htmlString.IndexOf("<script>");
int length = htmlString.IndexOf("</script>") - startIndex;
string scriptTag = htmlString.Substring(startIndex, length) + "</script>";
string expectedScripTag = scriptTag.Replace("<script>", "<script><br>" + text);
htmlString = htmlString.Replace(scriptTag, expectedScripTag);