How to add double quotations to a string

  • Thread starter Thread starter Kondapanaidu
  • Start date Start date
K

Kondapanaidu

Hi,

I am using C#V1.1.

How to add double quotations to a string.

If I go with "\"" the back slash is going to be added to the assigned
string.

EX: string str="naidu" + "\"" + "GK" + "\"";
The o/p for this is naidu\"GK"\

But I need the o/p like naidu"GK"

Thanks in advance
 
Kondapanaidu said:
I am using C#V1.1.

How to add double quotations to a string.

If I go with "\"" the back slash is going to be added to the assigned
string.

No it isn't - although the debugger may make you think it is.
EX: string str="naidu" + "\"" + "GK" + "\"";
The o/p for this is naidu\"GK"\

No - try writing it out to a console and you'll see it's correct.

The debugger unfortunately often confuses people - see
http://www.pobox.com/~skeet/csharp/strings.html#debugger

Jon
 
Hello Kondapanaidu,
you can use the following code to find the desired result...........

string oldstring =
textBox1.Text;
string newstring = oldstring.Replace("'","''");
MessageBox.Show(newstring);

Here you can add any type of character to your string and it will be
attached to you string.
i:e

if you write amit"Bansal" in the textbox.
message box will show the same to you.

this will really work.
 
Hi,


amity said:
Hello Kondapanaidu,
you can use the following code to find the desired result...........

string oldstring =
textBox1.Text;
string newstring = oldstring.Replace( " ' "," ' ' ");

Why get so complex, you are addind two characters intead of one, this may be
confusing even if they display the same way.

The code of the OP should work ok. A simple test will prove it.
 
Hello Ignacio Machin ,

I have just find the solution of the problem and it works fine.
It dosen't matter whether you are using one character or two...........
It only matters that how we can solve the problem.

amit bansal
india.
 
amity said:
I have just find the solution of the problem and it works fine.
It dosen't matter whether you are using one character or two...........
It only matters that how we can solve the problem.

But Ignacio is right - it *doesn't* solve the OP's problem. *You* may
have situations where it doesn't matter whether you use one character
or two, but the OP had a specific requirement which I don't think your
code solves.

The replacement part of your code isn't actually doing anything for the
OP in this case (other than potentially corrupting their data - there's
no sign that they wanted ' replaced with ''). Basically the OP's
problem (I believe) is in how they're trying to view their data. Yes,
showing it in a TextBox will mitigate that, but they don't need to do
any replacements (or use a TextBox for that matter). The code is just
okay as it is.
 
Hi,


amity said:
Hello Ignacio Machin ,

I have just find the solution of the problem and it works fine.
It dosen't matter whether you are using one character or two...........
It only matters that how we can solve the problem.

NO, you did not solve the problem, you just potentially introduced another
bigger one.

And not, it does not matter if what the user sees is what you expect, it
does matter and A LOT how you do it.
If you write code like the one you posted you will finish with such a big
mess that it will be un-maintenable in the long run. Not the reflect of a
good programmer
 
Back
Top