Solved

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

Posted on 2008-06-12
5
1,418 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

679 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