removing escape charachters from a string

fguihen

Junior Contributor
Joined
Nov 10, 2003
Messages
248
Location
Eire
i have a string, and it has to become a filename. the problem is it contains backslashes in many places. ive tried using the string.replace method to replace the \ with blank spaces but it doesnt work. its the same story with doubble quote charachters (the " charachter).what am i doing wrong?
 
What code are you using to remove the backslashes as there is no reason why this should fail.

Visual Basic:
Dim s as string = "ssdfdsf\sdfsdfdsf"
s=s.Replace("\"," ")

C#:
string s = "dsfsdf\\fdsfsdf";
s = s.Replace('\\',' ');

either should work.
 
String methods

What code are you using to remove the backslashes as there is no reason why this should fail.

As a guess, I would say fguihen may be expecting the Replace method to affect the actual string instance it is invoked on. It is useful to remember that the string type is immutable, meaning that an individual string instance cannot be modified, instead a new string must be created. This applies to all string methods, such as Concat, Remove, Trim, ToUpper and ToLower:

Visual Basic:
'Incorrect
myString.ToUpper()

'Correct
myString = myString.ToUpper()

Good luck :)
 
Re: String methods

Good catch, Mister Paul. You know, assuming that this is the case, I wouldn't have caught that, even if I saw the code. Even now I find myself stumped from time to time when I use the String.Replace method.
 
Back
Top