String.Replace

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I am trying to change the following string:
"<span class=\"Error\">Required field</span>"

To:
"<p class=\"Error\"><span>Required field</span></p>"

The initial span tag should be replaced by "p".
The inner text should be wrapped inside a new span tag with no
attributes.

However I am getting the following:
"<span class=\"Error\"><span>Required field</span></span>"

The span tag is not being replaced by "span".

I know my code is probably not the most robust, from a previous post
feedback but since the the string will be always very similar I think
my approach is ok.

My code is as follows:

String html = htmlHelper.ValidationMessage(modelName,
validationMessage, htmlAttributes);
XmlDocument xml = new XmlDocument();
if (!String.IsNullOrEmpty(html)) {
html.Replace("span", "p");
xml.LoadXml(html);
xml.DocumentElement.InnerXml = String.Concat("<span>",
xml.DocumentElement.InnerText, "</span>");
}
return xml.InnerXml;

The code that is not working is html.Replace("span", "p");
My code seems to be ok ...

Any idea why it is not working?

Thanks,
Miguel
 
shapper said:
Hello,

I am trying to change the following string:
"<span class=\"Error\">Required field</span>"

To:
"<p class=\"Error\"><span>Required field</span></p>"

The initial span tag should be replaced by "p".
The inner text should be wrapped inside a new span tag with no
attributes.

However I am getting the following:
"<span class=\"Error\"><span>Required field</span></span>"

The span tag is not being replaced by "span".

You mean replaced by "p"... Well, it is, but you throw away that string.
I know my code is probably not the most robust, from a previous post
feedback but since the the string will be always very similar I think
my approach is ok.

My code is as follows:

String html = htmlHelper.ValidationMessage(modelName,
validationMessage, htmlAttributes);
XmlDocument xml = new XmlDocument();
if (!String.IsNullOrEmpty(html)) {
html.Replace("span", "p");

html = html.Replace("span", "p");
xml.LoadXml(html);
xml.DocumentElement.InnerXml = String.Concat("<span>",
xml.DocumentElement.InnerText, "</span>");
}
return xml.InnerXml;

The code that is not working is html.Replace("span", "p");
My code seems to be ok ...

Any idea why it is not working?

Thanks,
Miguel

Parsing the string as XML really seems to be overkill just to put the
span tag in there. I would rather use a regular expression, replacing
all of your code above with just:

return Regex.Replace(htmlHelper.ValidationMessage(modelName,
validationMessage, htmlAttributes) ?? String.Empty,
"<span([^>]*)>([^<]*)</span>", "<p$1><span>$2</span></p>");
 
I would rather use a regular expression, replacing
all of your code above with just:

return Regex.Replace(htmlHelper.ValidationMessage(modelName,
validationMessage, htmlAttributes) ?? String.Empty,
"<span([^>]*)>([^<]*)</span>", "<p$1><span>$2</span></p>");

Thank you Goran. It worked really fine.

Thanks,
Miguel
 
But the reply from Goran did not answer your question.

That did the answer from Peter


Cor
 
Cor said:
But the reply from Goran did not answer your question.
That did the answer from Peter

Peter wrote:

#Well, the main issue is that strings are immutable, and the Replace()
#method returns a new string rather than modifying the current one.

Goran wrote:

#> The span tag is not being replaced by "span".
#
#You mean replaced by "p"... Well, it is, but you throw away that string.

and:

#> html.Replace("span", "p");
#
#html = html.Replace("span", "p");

Same answer. Goran just supplied the actual code change.

Arne
 
Arne,

There was one sentence with a question mark in the OP's message, that was

When I had seen that two hours latter then Peter had sent his message, there
was not anything to reply for me as well.
Especially as Regex as it is about HTML is often a short time incontrollable
solution.

(Or I should have written that I got or used the main information from Pete
and added something, but I know, we are luckily not all the same)

Not important, but as an OP thanks somebody else then the original replier
which is not me with the correct reply, then I sometimes want to show that.

Cor
 
Arne,

There was one sentence with a question mark in the OP's message, that was

When I had seen that two hours latter then Peter had sent his message, there
was not anything to reply for me as well.
Especially as Regex as it is about HTML is often a short time incontrollable
solution.

(Or I should have written that I got or used the main information from Pete
and added something, but I know, we are luckily not all the same)

Not important, but as an OP thanks somebody else then the original replier
which is not me with the correct reply, then I sometimes want to show that.

Cor
 
Cor said:
But the reply from Goran did not answer your question.

Yes, it did.

Would you ever consider that you might be wrong and double check before
you reply?
 
Cor said:
But the reply from Goran did not answer your question.

Yes, it did.

Would you ever consider that you might be wrong and double check before
you reply?
 
Yes, it did.

Would you ever consider that you might be wrong and double check before
you reply?

Thank you all in equal parts for all the help everybody gave me ... lol
 
Yes, it did.

Would you ever consider that you might be wrong and double check before
you reply?

Thank you all in equal parts for all the help everybody gave me ... lol
 
Back
Top