473,396 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Replace character in string

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,
Nov 14 '06 #1
4 3022
"Tim Kelley" <tk*****@company.comwrote in message
news:OI**************@TK2MSFTNGP04.phx.gbl...
>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
Nov 14 '06 #2
I tried this and the results were:
\\servername\\share\\folder\\filename.txt.

Thanks,

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:O2*************@TK2MSFTNGP02.phx.gbl...
"Tim Kelley" <tk*****@company.comwrote in message
news:OI**************@TK2MSFTNGP04.phx.gbl...
>>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


Nov 14 '06 #3
Tim Kelley <tk*****@company.comwrote:
I tried this and the results were:
\\servername\\share\\folder\\filename.txt.
How are you determining that? My guess is that you're actually looking
in the debugger.

See http://www.pobox.com/~skeet/csharp/s....html#debugger

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 14 '06 #4
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

Tim Kelley wrote:
I tried this and the results were:
\\servername\\share\\folder\\filename.txt.

Thanks,

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:O2*************@TK2MSFTNGP02.phx.gbl...
"Tim Kelley" <tk*****@company.comwrote in message
news:OI**************@TK2MSFTNGP04.phx.gbl...
>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
Nov 14 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Billy N. Patton | last post by:
-------- Original Message -------- Subject: <string>.replace Date: Fri, 15 Oct 2004 11:07:19 -0500 From: Billy N. Patton <b-patton@ti.com> Organization: Texas Instruments Newsgroups:...
19
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not...
14
by: Etu | last post by:
Hi, I have a string: string c = "'abc' \"cde\", 'mno' \"xyz\","; how can I use the c.Replace(???, ???) method to have this string: "'abc' "cde", 'mno' "xyz"," that is, all the...
5
by: djc | last post by:
I need to prepare a large text database field to display in an asp.net repeater control. Currently I am replacing all chr(13)'s with a "<br/>" and it works fine. However, now I also want to be able...
3
by: Pascal | last post by:
bonjour hello I would like to trim a string of all its white spaces so i used myString.trim() but it doesn't work as supposed : unsecable space are remaining in the middle of my string... i...
8
by: Warren Moxley | last post by:
Hi there, i've been searching for a C String search and replace function. I need to find all occurrences of " " in a char* array, and replace them with another char, I know how to do this in...
5
by: herman | last post by:
How can I replace all occurrences of a character with another character in std string? For example, I want to replace '/' with '+' in my std::string I have looked at the replace() method in...
6
by: Gabriel | last post by:
Hello, I do this : s = Environment.CurrentDirectory; s = s.Replace("\\", @"\"); Environment.CurrentDirectiry return a path like this C:\\....\\....\\..... I'd like replace the \\ by \, but...
10
by: Lonifasiko | last post by:
Hi, Just want to replace character at index 1 of a string with another character. Just want to replace character at that position. I thought Replace method would be overloaded with an index...
5
by: shapper | last post by:
Hello, I have a text as follows: "My email is something@something.xyz and I posted this @ 2 am" I need to replace the @ by (AT) bu only the ones that are in email addresses. All other @...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.