472,778 Members | 2,537 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How to replace the "\\" char with "\"

JPS
I need to build a string with just one backslash included "\", but I
keep getting an error message unless I use "\\". The double
backslashes will not work with what I am doing.

May 23 '07 #1
2 10574
JPS,

Did you look at the output? If you use "\\", you will find in the final
output that there is only one "\". The slash is an escape character for
strings in C# (unless you use the literal specifier @ before the string),
and because of that, if you want a single backslash, you need to have two in
the string.

That being said, these two statements are equivalent in C#:

str = "\\\\somemachine\\someshare\\somefile.txt";
str = @"\\somemachine\someshare\somefile.txt";

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"JPS" <jp*****@yahoo.comwrote in message
news:11********************@p77g2000hsh.googlegrou ps.com...
>I need to build a string with just one backslash included "\", but I
keep getting an error message unless I use "\\". The double
backslashes will not work with what I am doing.

May 23 '07 #2
If you're working with the string, you have to use the "\" to tell the
compiler that you want to use the literal "\" (or whatever character)
follows. The "\" is used for commands, so if you put just one "\" then it
will think it's a command instead of a "\"

"JPS" <jp*****@yahoo.comwrote in message
news:11********************@p77g2000hsh.googlegrou ps.com...
>I need to build a string with just one backslash included "\", but I
keep getting an error message unless I use "\\". The double
backslashes will not work with what I am doing.
May 23 '07 #3

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

Similar topics

4
by: Patrick | last post by:
Anyone have any ideas on how to Marshal this parameter in C#? unsigned char* buf TIA, Patrick
10
by: fei.liu | last post by:
Consider the following sample code char * ptr = "hello"; char carray = "hello"; int main(void){ } What does the standard have to say about the storage requirement about ptr and carray? Is...
7
by: xgngli | last post by:
Hi all! I've taken some time on learning the difference between "pointers to const variables" and "const pointer variables". The question is: in the following code, can we change the contents of...
13
by: RoughRyde | last post by:
Hi all, I'm totally new to C and hava a problem how to handle different types of variables. I have a function which which you call by: double calcdiff(const char *old, const char *new) I...
4
by: sevenever | last post by:
It seems leads to memory leaking on windows + VC. The delete will not free the memory in such condition?
6
by: Darin Johnson | last post by:
I keep running across that I'm maintaining that likes to define function parameters as "const char &" or "const int &", etc. Ie, constant reference parameters to a primitive type. This is for...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.