Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now