I have a string that look like codes=”A,B,C”, which I am getting after parsing from a word document.
At the run time I just want to replace these double quotes with empty string.
I tried doing something like str.Replace("\"", "").
But above double quotes are not getting replaced :(
As codes=”A,B,C” seems to have some different looking double quotes, not sure if that's causing the problem.
Please guide how can I replace these double quotes from above string.
Thank you!
The “ and ” characters aren't the same as the " character. How about this instead?
string clean = dirty.Replace("“", "").Replace("”", "");
The double quote character you have tried to replace is ASCII code 34. Word has used "smart quotes", which you should be able to insert in your code by holding down Alt and typing 0147 on the number keypad (the number key row won't work) and 0148.
These are the codes for those characters in the default ANSI code page for Windows I believe.
” look to me like one half of the smart quotes stuff a different thing to standard quotes. You will need to include them in your replacement code. Looking a character map they are U+201C and U+201D. Assuming the source is Unicode.
str.Replace("\"", string.Empty);
Related
I'm having trouble with my c# code.
I have stored a string object when I print it out it prints out "username" and when I view it in the debugger it is shown as "\"username\"". How can I replace "\ with whitespace in the variable? It is stopping me from making comparison operations.
I tried with
memberNameStripped = teamMemberName.Replace(#"\", "");
But it does not replace the "\ so how can I do it?
Thanks in advance.
Why regex? Use String.Trim to remove leading and trailing quotes("):
memberNameStripped = memberNameStripped.Trim('"');
It's efficient and clear.
The \ is an escape character, what you probably want to replace is the double quote "
So try:
memberNameStripped = teamMemberName.Replace("\"", "");
In debugger it is shown as "\"username\"" because it is a quoted string. This is why it prints out "username". You could get rid of quotes using Replace("\"", "")
I have a StringBuilder object and wanted to used its Append() method to add this whole string to it:
so I used "#" and copy pasted that whole string like this, but it gives a lot of errors such as "; expected ", "Invalid Expression '<'" , etc
myString.Append(#"COPY-PASTED-THAT_WHOLE-STRING");
What is the correct way of adding this string to my string builder object?
Thank you.
Even with an # prefixing the string, you need to escape any " characters, otherwise they will be interpreted as the end of the string literal.
EDIT:
e.g.
var entity = #"<!ENTITY xsd ""http://www.w3.org/2001/XMLSchema#"">";
Double-quotes (") inside the string you want to paste need to be escaped by being replaced with two consecutive double-quotes, as in "". Here's a trick to use:
Paste your string into a new instance of Notepad
Replace all double quotes (") with two double quotes ("")
Select and copy the content from Notepad back into clipboard
Paste it into #"…" in your code/text editor
From C# docs:
In a verbatim string literal, the characters between the delimiters
are interpreted verbatim, the only exception being a
quote-escape-sequence.
You can use the # syntax to add multiple lines. But you need to escape the "s inside your string by using ""
For example
#"<Ontology xmlns=""http://www.w3.org/2002/07/owl#"""
If you don't escape them, C# will treat the quote mark as the end of the string.
One option, as others have said, is to escape all of the double quotes (") with a double double quote ("").
What I prefer to do, as it makes the code more readable, when adding an XML block as a literal string, is to use single quotes rather than double quotes. Just put the XML file into a text editor and do a replace all on double quote with a single quote (').
Another option, since your XML literal isn't all that short, is to put it into a file and read in that file at runtime.
You can escape them like this as well...
#"<Ontology xmlns=\"http://www.w3.org/2002/07/owl#\""
I am using .NET (C#) code to write to a database that interfaces with a Perl application. When a single quote appears in a string, I need to "escape" it. IOW, the name O'Bannon should convert to O\'Bannon for the database UPDATE. However, all efforts at string manipulation (e.g. .Replace) generate an escape character for the backslash and I end up with O\\'Bannon.
I know it is actually generating the second backslash, because I can read the resulting database field's value (i.e. it is not just the IDE debug value for the string).
How can I get just the single backslash in the output string?
R
Well I did
"O'Bannon".Replace("'","\\'")
and result is
"O\'Bannon"
Is this what you want?
You can use "\\", which is the escape char followed by a backslash.
See the list of Escape Sequences here: http://msdn.microsoft.com/en-us/library/h21280bw.aspx
even better assign a var to the replace so that you can check it as well if needed
var RepName = "O'Bannon";
var Repstr = RepName.Replace("'","\\'");
You can also use a verbatim string
s = s.Replace("'", #"\'");
My program outputs strings like "Wzyryrff}av{v5~fvzu: Bb``igbuz~+\177Ql\027}C5]{H5LqL{" and the problem is the escape codes (\\\ instead of \, \177 instead of the character, etc.)
I need a way to unescape the string of all escape codes (mainly just the \\\ and octal \027 types). Is there something that already does this?
Thanks
Reference: http://www.tailrecursive.org/postscript/escapes.html
The strings are an encrypted value and I need to decrypt them, but I'm getting the wrong values since the strings are escaped
It sounds more like it's encoded rather than simply escaped (if \177 is really a character). So, try decoding it.
There is nothing built in to do exactly this kind of escaping.
You will need to parse and replace these sequences yourself.
The \xxx octal escapes can be found with a RegEx (\\\d{3}), iterating over the matches will allow you to parse out the octal part and get the replacement character for it (then a simple replace will do).
The others appear to be simple to replace with string.Replace.
If the string is encrypted then you probably need to treat it as binary and not text. You need to know how it is encoded and decode it accordingly. The fact that you can view it as text is incidental.
If you want to replace specific contents you can just use the .Replace() method.
i.e. myInput.Replace("\\", #"\")
I am not sure why the "\" is a problem for you. If it its actually an escape code then it just should be fine since the \ represents the \ in a string.
What is the reason you need to "remove" the escape codes?
I have a string variable. And it contains the text:
\0#«Ия\0ьw7к\b\0E\0њI\0\0ЂЪ\n
When I try to add it to the TextBox control, nothing happens.Because \0 mean END.
How do I add text as it is?
UPDATE:
The text is placed in the variable dynamically.Thus, # is not suitable.
Is the idea that you want to display the backslashes? If so, the backslashes will need to be in the original string.
If you're getting that text from a string literal, it's just a case of making it a verbatim string literal:
string text = #"\0#«Ия\0ьw7к\b\0E\0њI\0\0ЂЪ\n";
If want to pass in a string which really contains the Unicode "nul" character (U+0000) then you won't be able to get Windows to display that. You should remove those characters first:
textBox.Text = value.Replace("\0", "");
"\\0#«Ия\\0ьw7к\\b\\0E\\0њI\\0\\0ЂЪ\\n"
or
#"\0#«Ия\0ьw7к\b\0E\0њI\0\0ЂЪ\n"
Well, I don't know where your text is coming from, but if you have to, you can use
using System.Text.RegularExpressions;
...
string escapedText = RegEx.Escape(originalText);
However, if it's not soon enough, the string will already contain null characters.
And it contains the text:
\0#«Ия\0ьw7к\b\0E\0њI\0\0ЂЪ\n
No it doesn't. That's what the debugger told you it contains. The debugger automatically formatted the content as though you had written it as a literal value in your source code. The string doesn't actually contain the backslashes, they were added by the debugger formatter.
The string actually contains binary zeros. You can see this for yourself by using string.ToCharArray(). You cannot display this string as-is, you have to get rid of the zeros. Displaying the content in hex could work for example, BitConverter.ToString(byte[]) helps with that.
You can't.
Standard Windows controls cannot display null characters.
If you're trying to display the literal text \0, change the string to start with an # sign, which tells the compiler not to parse escape sequences. (#\0#«Ия\0ьw7к\b\0E\0њI\0\0ЂЪ\n")
If you want to display as much of the string as you can, you can strip the nulls, like this:
textBox.Text = someString.Replace("\0", "");
You can also replace them with escape codes:
textBox.Text = someString.Replace("\0", #"\0");
You might try escaping the backslash in \0, i.e. \\0. See this MSDN reference for a full list of C# escape sequences.