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.
Related
I'm faced with the following problem - I format a string from a textbox
stringValue = String.Format(new CultureInfo("ru-RU"), "{0:N}",
result);
Everything seems OK, but when I try to find spaces inside this string, all methods return null or -1 like spaces are absent in the string, but numbers are split by spaces in my textbox! Why are the spaces not found? If I use any other string format - I could find any symbol in the string. What's the problem here? Who knows?
It's because there isn't a space, in terms of U+0020 (the normal ASCII space).
What you'll get in the output for the thousands separator is U+00A0, which is a non-breaking space. (At least, that's what I've seen.)
It's not clear what you're using this for, but perhaps you need to change your code to detect any whitespace rather than ' '.
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("'", #"\'");
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);
For the sake of the example, let's assume I am parsing some text written in German. This means that it contains symbols like ü or Ö. The problem is that when all German specific symbols get rendered as an empty square. Please take a look at this image:
Image http://img8.imageshack.us/img8/7502/93341046.png
Since I do not know whether this symbol is ü or Ö I want to replace it with "." (dot). So the string from the image above, should become "Osnabr.ck". How do I do that?
Any help would be greatly appreciated!
Best Regards,
Kiril
You can use a regular expression to replace any characters that you don't want. Just put the characters that you want in a negative set:
str = Regex.Replace(str, "[^0-9A-Za-z _]", ".");
You should look into what encoding you are using to decode the text. It looks like you are not using the same encoding as was used to encode the text as the characters doesn't show up correctly.
If you want to see the actual characters (and I notice you are displaying the value in the immediate window in visual studio), you need to use a font that can display the characters. The presence of the square means the font you are using does not contain glyphs that match those characters. You can change the font used in various parts of Visual Studio in the options dialog.
Some more detail in this question here.
There is a Replace method on the string class. It's easiest to replace a single character with something else:
InnerText.Replace("ü", ".");
You can change several characters at the same time by chaining Replace:
InnerText.Replace("ü", "[ue]").Replace("Ö", "[Oe]");
I have a helper class pulling a string from an XML file. That string is a file path (so it has backslashes in it). I need to use that string as it is... How can I use it like I would with the literal command?
Instead of this:
string filePath = #"C:\somepath\file.txt";
I want to do this:
string filePath = #helper.getFilePath(); //getFilePath returns a string
This isn't how I am actually using it; it is just to make what I mean a little clearer. Is there some sort of .ToLiteral() or something?
I don't think you have to worry about it if you already have the value. The # operator is for when you're specifying the string (like in your first code snippet).
What are you attempting to do with the path string that isn't working?
I'm not sure if I understand. In your example: if helper.getFilePath() returns "c:\somepath\file.txt", there will be no problem, since the # is only needed if you are explicitely specifying a string with "".
When Functions talk to each other, you will always get the literal path. If the XML contains c:\somepath\file.txt and your function returns c:\somepath\file.txt, then string filePath will also contain c:\somepath\file.txt as a valid path.
The #"" just makes it easier to write string literals.
string (C# Reference, MSDN)
Verbatim string literals start with # and are also enclosed in double quotation marks. For example:
#"good morning" // a string literal
The advantage of verbatim strings is that escape sequences are not processed, which makes it easy to write, for example, a fully qualified file name:
#"c:\Docs\Source\a.txt" // rather than "c:\\Docs\\Source\\a.txt"
One place where I've used it is in a regex pattern:
string pattern = #"\b[DdFf][0-9]+\b";
If you have a string in a variable, you do not need to make a "literal" out of it, since if it is well formed, it already has the correct contents.
In C# the # symbol combined with doubles quotes allows you to write escaped strings. E.g.
print(#"c:\mydir\dont\have\to\escape\backslashes\etc");
If you dont use it then you need to use the escape character in your strings.
http://msdn.microsoft.com/en-us/library/aa691090(VS.71).aspx
You dont need to specify it anywhere else in code. In fact doing so should cause a compiler error.
You've got it backwards. The #-operator is for turning literals into strings, while keeping all funky characters. Your path is already a string - you don't need to do anything at all to it. Just lose the #.
string filePath = helper.getFilePath();
The string returned from your helper class is not a literal string so you don't need to use the '#' character to remove the behaviour of the backslashes.