Replace string function in C# working in web forms but not win forms.

He says:

What am I missing here?
I have the following string:
string _node =  Individual_Program__Serious_Incidents_Sorted_by_Incident_Report 
I use the following C#


I am trying to replace the dash with an underscore.  After the code executes, it is still a dash!!!
Any suggestions?

I reply back:
Heres your answer&

    protected void Page_Load(object sender, EventArgs e)
                    string myNode=

He says:
Im not using ASP.  This is in my code generator.

Its maddening& this doesnt work:

string _treeNode = ri.treeNodeName.Replace(" ", "_");
string_treeNode2 = _treeNode.Replace("-","_");

After that _treeNode2 still has the darn -.

I cant believe this is a bug&weird.  Ive googled and no help.

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

aphillipsConnect With a Mentor Commented:
I think you are doing the right thing, but it is hard to be sure as the code you have posted is syntactically wrong and what you say is inconsistent.

First, as suggested above use String.Replace(char, char) rather than String.Replace(String, String) for efficiency.  But I don't think that is your problem as the 2nd version should still work.

You can verify that String.Replace is working with code like this:

  string a = "-A-";
  string b = a.Replace("-", "_"); // should produce "_A_"
  Console.WriteLine(b);           // or just look at b in the debugger

I suspect that the string passed to you is produced elsewhere and the character is not the same character (minus sign) as you are trying to replace.  There may be other Unicode characters that look like a dash.  You can iterate through the string and print out the decimal value of each character by doing something like this:

  foreach (char c in _treeNode2)

A dash (minus sign) should have value 45 and underscore should have value 95.
myNode.Replace('_','-'). try single quotes
M3hcSSAuthor Commented:
Another email from him. I think...aphillips might be on the right track.

Interesting, this works:

string test = "hello-has_Dash";
test = test.Replace("-","_");

but this doesnt work:

string test = "Program__Serious_Incidents_Sorted_by_Incident";
test = test.Replace("-","_");

Is the dash different in those two?!
M3hcSSAuthor Commented:
Hmm, I think the square thingy might be the cause of the problem. I need to check...
M3hcSSAuthor Commented:
Yeah, it was not an ASCII standard format.
All Courses

From novice to tech pro — start learning today.