Replace character in string

  • Thread starter Thread starter Tim Kelley
  • Start date Start date
T

Tim Kelley

I am reading a text file with a streamreader. I parse out path and need to
use that path in a sql statement. The path is in the file as
\\servername\share\folder\filename.txt. When the line is read, it is in the
format of \\\\servername\\share\\folder\\filename.txt. When I try to use
this in my sql statement it fails because the field in the database in in
the previous format. I have tried using Replace, but I have not gotten it
to work. Any other suggestions?

Thanks,
 
Tim Kelley said:
I am reading a text file with a streamreader. I parse out path and need to
use that path in a sql statement. The path is in the file as
\\servername\share\folder\filename.txt. When the line is read, it is in
the format of \\\\servername\\share\\folder\\filename.txt. When I try to
use this in my sql statement it fails because the field in the database in
in the previous format. I have tried using Replace, but I have not gotten
it to work. Any other suggestions?

String.Replace doesn't modify the string you call it on - it returns a new
string with the replacements made.

path = path.Replace(@"\\",@"\");

-cd
 
I tried this and the results were:
\\servername\\share\\folder\\filename.txt.

Thanks,
 
Tim,

Of course... try this:

string strIntermediate = path.Replace(@"\\",
"??intermediate-replacement??");
strIntermediate = strIntermediate.Replace(@"\\", @"\");
return strIntermediate.Replace("??intermediate-replacement??", @"\\");

Your path starts with two directory delimiters to indicate a UNC
location. You have to escape that. You could do this in one step with a
regexp, but that may just be overkill. Of course, the solution above
won't work if your string contains the escape sequence, although the
question marks would be an illegal filename. The only way to do this
without regexp and that possibility is stateful recursion.

Also keep in mind that the debugger shows the inputtable C#
representation of the string. If your string containts "A\B," the
debugger will always show you "A\\B," with the backslash escaped.


Stephan
 
Back
Top