Give a value with the sign \ to a string variable.

G

Guest

Hello!
I want to give a value with the sign \ to a string variable.
The problem is that the program always add the sign @ before the variable.
The next line gives me : @"Name\shai"

string str = "Name\\shai";

I want str to be only: "Name\shai" .
How can I do it?
Thanks in advance.
 
D

Dan Manges

Niron said:
Hello!
I want to give a value with the sign \ to a string variable.
The problem is that the program always add the sign @ before the variable.
The next line gives me : @"Name\shai"

string str = "Name\\shai";

I want str to be only: "Name\shai" .
How can I do it?
Thanks in advance.

The @ before the string just means it's a literal value and that '\' is
not an escape character. "Name\\shai" == @"Name\sahi"


Dan Manges
 
M

Marc Gravell

What you see is not always what you get;

If the debugger is displaying @"Name\shai" or "Name\\shai", then this *is*
the string you want; the \\ and @ are just there to tell you (as a
developer, not a user) how the string is being visualised; it doesn't change
the actual contents.

If you output the string onto the screen or a file, or whatever, then it
should appear as "Name\shai"

Marc
 
V

Vadym Stetsyak

Hello, Niron!

Nk> string str = "Name\\shai";

the upper gives you Name\Shai,

\ - is special character, and in order to see it in the string you should escape it with another \ symbol.

You can test this with simple console application:

string str = "test1\\test2";
Console.WriteLine(str);
string str1 = "test3\\\\test4";
Console.WriteLine(str1);

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
G

Guest

Thanks but:
I don’t get the value I need.
If I get a variable with this value - @"name1\name2"
How can I change it to this value- "name1\name2"
(Remove the @)

I must pass the value without the @.
I try Substring but it doesn’t work.
 
T

Tom Porterfield

Niron said:
Thanks but:
I don’t get the value I need.
If I get a variable with this value - @"name1\name2"
How can I change it to this value- "name1\name2"
(Remove the @)

I must pass the value without the @.
I try Substring but it doesn’t work.

The @ simply means that the escape characters in the string are treated as
literals. It is not actually part of the string. Just pass the string, the
@ won't get passed.
 
M

Marc Gravell

You already have it:

Consider: @"name1\name2"

Look where the @ is; it isn't in the string (quotes); the string DOES NOT
contain an @ character. This is purely for the debugger and developer, so
that they know that it shouldn't be read "name1{newline}ame2" (since \n ==
{newline}). Try Console.WriteLine or Debug.WriteLine; there is no @.
Honestly.

There isn't Substring will not do anything because the @ isn't there. The
debugger is not always the easiest place to visualise string values, but
what it is saying is correct - you just have to know how to read it.

Marc
 
G

Guest

First, thank you for your attention!!!
Second, I am not be near the program now, but I will check it later.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top