-8

There is a string which is innerHTML of an HTML page. In that, there are contents ...which I need to replace part by parts...I have replaced a few by the following formula.

string formatted = str.replace("*", "")

This will replace all * in that HTML page with blank. But I need to replace following contents to blank as well.

[Issue /]

[-Select- /]

[ ]

[Upload]

[Issue /]

So I used code as below:

str.replace("[Issue \/]", "")
str.replace("[-Select- \/]", "")
str.replace("[    ]", "")
str.replace("[Upload]", "")
str.replace("[Issue \/]", "")

But I couldn't see any replaces for the contents in the HTML content for the above string.

And there is an image button in the HTML. That image button also I need to replace.

The HTML code of the image button is below:

<asp:ImageButton ID="btnAddHotel" runat="server" ImageUrl="/Style Library/images/icon-add.gif" OnClick="btnAddHotel_Click" OnClientClick="return HideAllTicketValidations();" />

How to replace all these strings?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
PeteEngineer
  • 123
  • 4
  • 14
  • 4
    I have no clue what you're actually trying to do. What are you trying to replace? What are some examples of the input string? What are the respective desired outputs? What does "not working" mean? And what does the random chunk of HTML have to do with anything? – millimoose Nov 11 '12 at 13:52
  • 1
    See the posts http://stackoverflow.com/questions/1811969/regular-expression-to-replace-square-brackets-with-angle-brackets and http://stackoverflow.com/questions/5840992/regex-not-removing-square-brackets – Hamlet Hakobyan Nov 11 '12 at 13:53
  • 3
    from your code, I can bet that `str.replace("*","")` _does not work_ as well. – Vlad Nov 11 '12 at 13:55
  • god just tell me how to replace square brackets using this str.replace("[","") not working any other work around? – PeteEngineer Nov 11 '12 at 13:57
  • 2
    str.replace("[", "") works perfectly. Given a string like "something[else]" it will transform it into "somethingelse]", which is what it is supposed to do. The question is what do you want it to do? Please provide an example of your input string and what you would like to transform it to. – AHM Nov 11 '12 at 14:11
  • 2
    `-1` for soliciting an answer without any effort to clarify the question. Also I didn't get the last part with "html". – Michal Klouda Nov 11 '12 at 14:24

2 Answers2

4

str.Replace doesn't make the changes in place. Instead it returns a new string.

Try using:

var newstr = str.Replace(...)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
L.B
  • 114,136
  • 19
  • 178
  • 224
  • god just tell me how to replace square brackets using this str.replace("[","") not working any other work around? i know it will return that ... – PeteEngineer Nov 11 '12 at 13:58
  • 6
    @PeteEngineer then please post a better question with *real* code so that people can help. – L.B Nov 11 '12 at 14:00
  • i have posted a better one now – PeteEngineer Nov 11 '12 at 16:37
  • 1
    @PeteEngineer 1) It is `Replace` not `replace` 2) You still don't show your real code `str.replace("[Issue \/]","")` doesn't have any effect and can not be compiled 3) How your input string look like? 4) What is the role of `image button` defintinition you posted? I would suugest reading [this](http://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect-question.aspx) – L.B Nov 11 '12 at 17:19
-2

You need disable special characters (brackets), like that:

str = "Example [Issue]" 
str.replace("\[Issue\]", "")   //str == "Example "
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Drasius
  • 825
  • 1
  • 11
  • 26