askquestionzero
askquestionzero

Reputation: 107

converting simple javascript code to c#

yesterday i have asked a question here. and the solution is simple using javascript and html

a while ago what im planning is to manipulate the html to do the task in javascript but i changed my mind, i rewrite the javascript code to c#

here is the input

<Abstract>
    <Heading>Abstract</Heading>
    <Para TextBreak="No" >Some paragraph <Emphasis Type="Italic">q</Emphasis><Emphasis Type="Bold">Bold</Emphasis><Emphasis Type="Underline">Underline</Emphasis> </Para>

</Abstract>

but the problem is the output on my c# is wrong?

what's wrong with the code i translate?

here is the javascript code

<script type="text/javascript">
        jQuery(document).ready(function(){
textval = $('textarea').val();
textnewval = textval.replace('Para TextBreak="No"', 'p').replace('/Para', '/p'); 

  if(textnewval.indexOf('Italic') >= 0) //If Italic
{
    EmphasisAttr = 'Italic';
  textnewval = textnewval.replace('Emphasis Type="'+EmphasisAttr+'"', 'i').replace('/Emphasis', '/i'); 
}
if(textnewval.indexOf('Bold') >= 0) //If Bold
{
    EmphasisAttr = 'Bold';
  textnewval = textnewval.replace('Emphasis Type="'+EmphasisAttr+'"', 'b').replace('/Emphasis', '/b'); 
}
if(textnewval.indexOf('Underline') >= 0) //If underline
{
    EmphasisAttr = 'Underline';
    textnewval = textnewval.replace('Emphasis Type="'+EmphasisAttr+'"', 'u').replace('/Emphasis', '/u'); 
}


  $('textarea').val(textnewval);
  alert($('textarea').val());
});
    </script>

the output of the code above is this

  <Abstract>
    <Heading>Abstract</Heading>
    <p >Some paragraph <i>q</i><b>Bold</b><u>Underline</u> </p>

</Abstract>

which is correct, and below is my c# code. and the output is wrong

string result = null;
string input = textBox.Text;

result = input.Replace("Para TextBreak=\"No\"", "p").ToString().Replace("/Para", "/p");
if (result.IndexOf("Italic") >= 0)
{
    string EmphasisAttr = "Italic";
    result = result.Replace("Emphasis Type=\"" + EmphasisAttr + "\"", "i").ToString().Replace("/Emphasis", "/i");
}
 if (result.IndexOf("Bold") >= 0)
{
    string EmphasisAttr = "Bold";
    result = result.Replace("Emphasis Type=\"" + EmphasisAttr + "\"", "b").ToString().Replace("/Emphasis", "/b");
}
 if (result.IndexOf("Underline") >= 0)
{
    string EmphasisAttr = "Underline";
    result = result.Replace("Emphasis Type=\"" + EmphasisAttr + "\"", "u").ToString().Replace("/Emphasis", "/u");
}
Console.WriteLine(result);

output:

<Abstract>
    <Heading>Abstract</Heading>
    <p >Some paragraph <i>q</i><b>Bold</i><u>Underline</i> </p>

</Abstract>

Upvotes: 0

Views: 109

Answers (1)

Scott Hannen
Scott Hannen

Reputation: 29312

The problem is that here

result = result.Replace("Emphasis Type=\"" + EmphasisAttr + "\"", "i").ToString()
    .Replace("/Emphasis", "/i");

you're replacing every instance of /emphasis with /i. Not just the first one, all of them. After that there are no instances of /emphasis to replace with /b or /u.

In JavaScript, .replace by default replaces only the first instance of the matched string. In .NET it replaces all of them.

Upvotes: 1

Related Questions