Solved

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

Posted on 2008-06-12
5
1,416 Views
Last Modified: 2010-04-21
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#

 _node.Replace(-,_).

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=
                                @"Individual_Program__Serious_Incidents_Sorted_by_Incident_Report";
                    Label1.Text=myNode;
                    myNode=myNode.Replace("_","-").ToString();
                    Label2.Text=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.

0
Comment
Question by:M3hcSS
  • 3
5 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 21773974
myNode.Replace('_','-'). try single quotes
0
 
LVL 3

Accepted Solution

by:
aphillips earned 500 total points
ID: 21774875
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)
    Console.WriteLine((int)c);

A dash (minus sign) should have value 45 and underscore should have value 95.
0
 
LVL 2

Author Comment

by:M3hcSS
ID: 21778356
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?!
0
 
LVL 2

Author Comment

by:M3hcSS
ID: 21778366
Hmm, I think the square thingy might be the cause of the problem. I need to check...
0
 
LVL 2

Author Closing Comment

by:M3hcSS
ID: 31466750
Yeah, it was not an ASCII standard format.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question