Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace apostrophe with double apostrophe in string?

Tags:

c#

sql

I have a string

good overview of ESP's in more detail than you probably need.

While inserting into SQL table it is giving error. So I want replace apostrophe in the string with double apostrophe like

good overview of ESP''s in more detail than you probably need

How to manipulate this in c#?

like image 610
James123 Avatar asked Oct 12 '11 22:10

James123


1 Answers

Very easy:

string s = "good overview of ESP's in more detail than you probably need.";
string escaped = s.Replace("'","''");

Note: It is usually safer to use command parameters. Especially if the values of the input strings are not controlled by your code (i.e. user entries).

like image 149
Olivier Jacot-Descombes Avatar answered Sep 21 '22 00:09

Olivier Jacot-Descombes