StezPet
StezPet

Reputation: 2450

Inject some string to a specific part of string in C#

How to insert some string to a specific part of another string. What i am trying to achieve is i have an html string like this in my variable say 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>

I need to add below string content inside <script> <script/> tag

    function callNative()
{
    window.external.notify("Uulalaa!");
}
    function addToBody(text)
{
    document.body.innerHTML = document.body.innerHTML + "<br>" + text;
}

How i can achieve this in C#.

Upvotes: 5

Views: 2915

Answers (6)

petchirajan
petchirajan

Reputation: 4352

For this you can read the html file into string by using File.ReadAllText method. Here For example, i have used sample html string. After that, by some string operations you can add tags under script like follows.

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);

Upvotes: 1

L-Four
L-Four

Reputation: 13531

This can be done in another (safer) way, using HTML Agility Pack (open source project http://htmlagilitypack.codeplex.com). It helps you to parse and edit html without you having to worry about malformed tags (<br/>, <br />, < br / > etc). It includes operations to make it easy to insert elements, like AppendChild.

If you are dealing with HTML, this is the way to go.

Upvotes: 1

James Holderness
James Holderness

Reputation: 23001

Assuming your content is stored in the string content, you can start by finding the script tag with:

int scriptpos = content.IndexOf("<script");

Then to get past the end of the script tag:

scriptpos = content.IndexOf(">", scriptpos) + 1;

And finally to insert your new content:

content = content.Insert(scriptpos, newContent);

This at least allows for potential attributes in the script tag.

Upvotes: 6

cnd
cnd

Reputation: 33724

var htmlString = @"<script>$var1</script> <script>$var2</script>"
                 .Replace("$var1", "alert('var1')")
                 .Replace("$var2", "alert('var2')");

Upvotes: 2

Ahmed KRAIEM
Ahmed KRAIEM

Reputation: 10427

Use htmlString.Replace(what, with)

var htmlString = "you html bla bla where's the script tag? oooups here it is!!!<script></script>";

var yourScript = "alert('HA-HA-HA!!!')";

htmlString = htmlString.Replace("<script>", "<script>" + yourScript);

Note that this will insert yourScript inside all <script> elements.

Upvotes: 2

Artur Udod
Artur Udod

Reputation: 4743

var htmlString = "you html bla bla where's the script tag? oooups here it is!!!<script></script>";

var yourScript = "alert('HA-HA-HA!!!')";

htmlString = htmlString.Insert(html.IndexOf("<script>") + "<script>".Length + 1, yourScript);

Upvotes: 1

Related Questions