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.
Related
string s = "P\04";
string z = s.Replace('\\', '-');
Console.WriteLine(z);
I need to replace '\' character in to '-' character in a string. I tried several ways to replace, couldn't able to do for '\' character only.
Please any one suggest a way to do this
Your code to replace the \ is fine. The problem is with your input string, where the \ escapes the 0. It would work if you had this:
string s = "P\\04";
string z = s.Replace('\\', '-');
Console.WriteLine(z);
The output is P-04 assuming that's what you expect.
string s = #"P\04";
string z = s.Replace('\\', '-');
Console.WriteLine(z);
Add # at the before value of string s to make it a verbatim. That way '\' is treated as is. Otherwise \0 are treated as one character to make a different character.
You can also use Regex,
var result = Regex.Replace(#"P\04", #"\\", #"-");
Console.WriteLine(result);
FIDDLE
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("\"", "");
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
How to escape the character \ in C#?
You just need to escape it:
char c = '\\';
Or you could use the Unicode escape sequence:
char c = '\u005c';
See my article on strings for all the various escape sequences available in string/character literals.
You can escape a backslash using a backslash.
//String
string backslash = "\\";
//Character
char backslash = '\\';
or
You can use the string literal.
string backslash = #"\";
char backslash = #"\"[0];
use double backlash like so "\"
"\\"
cause an escape
If you want to output it in a string, you can write "\\" or as a character, you can write '\\'.
Escape it: "\\"
or use the verbatim syntax: #"\"
Double escape it. Escape escape = no escape! \\
To insert a backslash you need to type it twice:
string myPath = "C:\\Users\\YourUser\\Desktop\\YourFile.txt";
The string myPath should now contain: C:\Users\YourUser\Desktop\YourFile.txt
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("\"", "");
}