String.Replace

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
 
G

Göran Andersson

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

shapper

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
 
C

Cor Ligthert[MVP]

But the reply from Goran did not answer your question.

That did the answer from Peter


Cor
 
A

Arne Vajhøj

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
 
C

Cor Ligthert[MVP]

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
 
C

Cor Ligthert[MVP]

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
 
G

Göran Andersson

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?
 
G

Göran Andersson

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?
 
S

shapper

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
 
S

shapper

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top