I need to use " and ' as a character in C#, however they are special characters. So when I put them as character in the string, it will give an error. The issue is that it has many " and ' so I need to find a way to allow me to use these special characters. How can I do that
Use escape sequences: "This is a double-quote: \", and this is a single quote: \'"
Although note that since the string is delimited by double quotes, the \' escape isn't necessary: "This is a double-quote: \", and this is a single quote: '"
Simple prefix your string with # for " use "" This makes newlines easy as pie:
string example = #"A string that has double quote "" and single quote ' also a new line
";
You can use escape character \
Example
var testSTring = "\"test\""
Having a single quote ' in string doesn't trouble C#, its the double quotes ", you have to escape them with backslash like:
string str = "some\"stri'''''ng";
You can also use verbatim string # in the start and then you have to escape double quotes with another double quote like:
string str = #"some""stri'ng";
Put a backslash before special chars you want to use to escape them.
Here's a list:
http://msdn.microsoft.com/en-us/library/h21280bw.aspx
Related
i am using a web service and result is coming like this
" methew wade watto"
then I've tried with string.replace():
jsona = jsona.Replace(#"", "");
but the problem is i am unable to replace special character's like " this in my replace statement, How can I replace " from the input string? and what are the other options of replacing the string other then this?
In c#, The # symbol means to read that string literally, and don't
interpret control characters otherwise. whereas \ followed by a
character that is not recognized as an escaped character, matches that
character.
So you have to use \" to represent " in .Replace() instead for #
I think you have to try something like this:
string jsonInput = "\"methew wade watto\""; // be the input
string replacedQuotes = jsonInput.Replace("\"", "");
Working example
You need to escape the " with \ , right now, you are just saying to replace empty string with empty string:
jsona= jsona.Replace("\"","");
Now this will replace the " sign in your string with empty string.
Output:
methew wade watto
Use a backslash to determine special character
string = string.Replace("\"", "");
string value = "\"2\";;\"0_67011297_2013-10-04_2\";\"dfdfd.sfdsf#fdsfsd.PL\";\"fdsf.fdfd#fsdfsdfd.pl\";\"RASP\";;\"2013-10-04\";\"2013-10-04\";\"fdfdfDwfdsfdsór fdsf\";\"Otwarcie fdsf+ wywiad z fdfdfdfds(Wardfdfdszawa)\";\"pkp\";\"MATEfdfdUSZ.fdsfd#fdsfsd.PL\";\"2014-10-01 15:41:20\";\"2014-10-01 15:41:20\";\"utworzony kod delegacji\"";
How to remove the \" from it?
value.Trim( new Char[] { '\\', '\"'});
doesnt work.
Help
There is no \ symbol in the original string. \ is used to as escape character in your source string.
More reference here
The backslash is used to escape characters (in this case the ").
Why not use ' instead?
string value = "'2';;'0_67011297_2013-10-04_2';'dfdfd.sfdsf#fdsfsd.PL';'fdsf.fdfd#fsdfsdfd.pl';'RASP';;'2013-10-04';
a.s.o.
Try this:
value.Replace(#"\", "")
It replaces "\" with an empty string. # is there so that compiler takes it as it is. If there is no # in front of "\" it won't work.
Input String is "How are you"
Expected output \"How are you\"
Due to combination of DoubleQuote and escape sequence I am not able to replace the string in the required format.
Please can someone provide me the code snippet for doing this.
I have tried below does not work
myString.Replace(""","\"");
Yes, that looks a little bit weird, you need to escape both:
string test = "\"How are you\"";
test = test.Replace("\"", "\\\"");
Both " and \ have special meaning within a string literal and must be escaped with \.
So something like this:
myString.Replace("\"","\\\"");
Escape your backslash
myString.Replace("\"","\\\"");
This:
"\\\""
produces this:
\"
And also you should escape your double quote in first parameter.
And you can use verbatim strings,but there is a weird case about double quote, instead of \" you should use two double quotes "" to escepe your charachter:
myString.Replace(#"""",#"\""");
string myString = "\"How are you\"";
myString = myString.Replace("\"", "\\\"");
Using C# we can do string check like if string.contains() method, e.g.:
string test = "Microsoft";
if (test.Contains("i"))
test = test.Replace("i","a");
This is fine. But what if I want to replace a string which contains " symbol to be replaced.
I want to achieve this:
"<html><head>
I want to remove the " symbol present in check so that the result would be:
<html><head>
The " character can also be replaced, just like any other:
test = test.Replace("\"","");
Also, note that you don't have to test if the character exists : your test.Contains("i") could be removed since the .Replace() method won't do anything (no replace, no error thrown) if the character doesn't exist inside the string.
To include a quote symbol in a string, you need to escape it, using a backslash. In your example, you want to use something lik this:
if (test.Contains("\""))
There are two ways to include a '"' character in a string literal. All the answers so far have used the c-style way:
var quotation = "Parting is such sweet sorrow";
var howSweetIsIt = quotation + " that I shall say \"good-night\" till it be morrow.";
In some contexts (especially for users experienced with Visual Basic), the verbatim string literal may be easier to read. A verbatim string literal begins with an # sign, and the only character that requires escaping is the quotation mark -- all other characters are included verbatim (hence the name). Significantly, the method of escaping the quotation mark is different: rather than preceding it with a backslash, it must be doubled:
var howSweetIsIt = quotation + " that I shall say ""good-night"" till it be morrow.";
string SymbolString = "Micro\"so\"ft";
The string above use scape char \ to insert " between the characters
string Result = SymbolString.Replace("\"", string.Empty);
With the following replace I replace the character "" for empty.
This is what you try to achieve?
if (check.Contains("\"")
output = check.Replace("\"", "");
output = check.Replace("\"", "");
Just remember to use "\"" for the quote sign as the backslash is an escape character.
if (str.Contains("\""))
{
str = str.Replace("\"", "");
}
I want this line of code to work-
int start = s.IndexOf(""_type": "Person""name": "");
But clearly the double quotes are messing up the search... Any ideas about how to get this working?
You can take two approaches to this.
The first is by using a string-literal and escaping double-quotes with another double quote:
string s = #"This is a ""quoted"" string.";
s.IndexOf(#"a ""quoted"" string");
The other is to escape the double-quotes with a backslash:
string s = "This is a \"quoted\" string.";
s.IndexOf("a \"quoted\" string");
If you want to use a double-quote in a string, one way is to escape it with a backslash. \
string myString = "This is a string \" with a double quote";
So what you want to do is escape the string? Try this:
int start = s.IndexOf(#"this ""word"" is escaped");
I'm assuming you want to run IndexOf() on the entire string, including the quotes inside? All you have to do is use both types of quotes: ' ' and " ". As long as you use one to designate the main string and the other to designate sub-strings, it should work, i.e. something like: s.IndexOf(' "_type": "Person""name": " ');