Solved

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

Posted on 2008-06-12
5
1,419 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

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…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

752 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