What does the following setting do in Visual Studio?
Tools > Options > Text Editor > C# > Advanced > Editor Help > Fix text pasted into string literals (experimental)
I didn't see it "fix" things I paste into a " ". Perhaps in certain cases?
This attempts to escape characters inserted into string literals that are not legal there, such as quotes or single backspaces. Unfortunately, the option pages are very poorly documented. The documentation page states
Not all options may be listed here.
Which, at least, is honest...
This option (I have now turned it off) was adding a \ character before pasted text when pasting at the end of a string. In my case, this happened when using a StringBuilder.
For Example:
sb.AppendLine(" ,'NA' as CheckedDate
Desired Code after pasting "); at the end
sb.AppendLine(" ,'NA' as CheckedDate ");
Resultant code after pasting with this option turned on
sb.AppendLine(" ,'NA' as CheckedDate \");
Related
I am new to this site, and I don't know if I am providing enough info - I'll do my best =)
If you use Notepad++, then you will know what I am talking about -- When a user loads a .exe into Notepad++, the NUL / \x0 character is replaced by NULL, which has a black background, and white text. I tried pasting it into Visual Studio, hoping to obtain the same output, but it just pasted some spaces...
Does anyone know if this is a certain key-combination, or something? I would like to put the NULL character in replacement of \x0, just like Notepad++ =)
Notepad++ is a rich text editor unlike your regular notepad. It can display custom graphics so common in all modern text editors. While reading a file whenever notepad++ encounters the ASCII code of a null character then instead of displaying nothing it adds the string "NULL" to the UI setting the text background colour to black and text colour to white which is what you are seeing. You can show any custom style in your rich text editor too.
NOTE: This is by no means an efficient solution. I'm clearly traversing a read string 2 times just to take benefit of already present methods. This can be done manually in a single pass. It is just to give a hint about how you can do it. Also I wrote the code carefully but haven't ran it because I don't have the tools at the moment. I apologise for any mistakes let me know I'll update it
Step 1 : Read a text file by line (line ends at '\n') and replace all instances of null character of that line with the string "NUL" using the String.Replace(). Finally append the modified text to your RichTextBox.
Step 2 : Re traverse your read line using String.IndexOf() finding start indexes of each "NUL" word. Using these indexed you select text from RichTextBox and then style that selected text using RichTextBox.SelectionColor and RichTextBox.SelectionBackColor
richTextBoxCursor basically just represents the start index of each line in RichTextBox
StreamReader sr = new StreamReader(#"c:\test.txt" , Encoding.UTF8);
int richTextBoxCursor = 0;
while (!sr.EndOfStream){
richTextBoxCursor = richTextBox.TextLength;
string line = sr.ReadLine();
line = line.Replace(Convert.ToChar(0x0).ToString(), "NUL");
richTextBox.AppendText(line);
i = 0;
while(true){
i = line.IndexOf("NUL", i) ;
if(i == -1) break;
// This specific select function select text start from a certain start index to certain specified character range passed as second parameter
// i is the start index of each found "NUL" word in our read line
// 3 is the character range because "NUL" word has three characters
richTextBox.Select(richTextBoxCursor + i , 3);
richTextBox.SelectionColor = Color.White;
richTextBox.SelectionBackColor = Color.Black;
i++;
}
}
Notepad++ may use custom or special fonts to show these particular characters. This behavior also may not appropriate for all text editors. So, they don't show them.
If you want to write a text editor that visualize these characters, you probably need to implement this behavior programmatically. Seeing notepad++ source can be helpful If you want.
Text editor
As far as I know in order to make Visual Studio display non printable characters you need to install an extension from the marketplace at https://marketplace.visualstudio.com.
One such extension, which I have neither tried nor recomend - I just did a quick search and this is the first result - is
Invisible Character Visualizer.
Having said that, copy-pasting binaries is a risky business.
You may try Edit > Advanced > View White Space first.
Binary editor
To really see what's going on you could use the VS' binary editor: File->Open->(Open with... option)->Binary Editor -> OK
To answer your question.
It's a symbolic representation of 00H double byte.
You're copying and pasting the values. Notepad++ is showing you symbols that replace the representation of those values (because you configured it to do so in that IDE).
I am displaying a text file using this code in C#
string text = System.IO.File.ReadAllText(Server.MapPath(path));
LabelContent.Text = text.ToString();
Once the text is displayed I am getting this (see below -�- ) characthers:
For fine-grained control over your PC�s power settings, click the
�Change plan settings� link next to the power plan you�ve...
My question is how can I get rid of that using C# ?
I'll appreciate any help. Thank you !
You can use Regex.Replace() with the pattern:
"[^\\w\\s\\p{P}\\p{Sm}<>]+"
This matches anything that is not a letter, number, punctuation, white space characters, mathematical operators, or tag characters ("<>") Any character that does not match the pattern, replace it with String.Empty. If there ends up being other characters you want to keep, then you have to add it to the pattern.
Example:
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
string fileContent = " For fine-grained control over your PC�s power settings, click the\n" +
"<p> �Change plan settings� link next to the power plan you�ve... </p>";
fileContent = Regex.Replace(fileContent, "[^\\w\\s\\p{P}\\p{Sm}<>]+", String.Empty);
Console.WriteLine(fileContent);
}
}
Results:
For fine-grained control over your PCs power settings, click the
<p> Change plan settings link next to the power plan youve... </p>
Fiddle Demo
The issue you are having is the matter of properly encoding the UTF characters in the html (I assume you are using ASP.NET) I would try to make sure you encode as utf-8 <meta charset="utf-8" /> if this does not work search for other encoding.
Okay, I finally found out how to get rid of those annoying characters (���).
All I have to do is save my text file with the Encoding UTF-8 instead of ANSI, which is the default selection in Notepad. I have to say that adding alone won't fix the problem. I had to go back to Notepad and save again my files, but this time using a different Encoder (UTF-8).
However I appreciate all of you, for taking the time to help me. This site is so great because of all of you. THANK YOU !!!!
Okay, I have a string
string textToShow = "this\nrocks"
which when put in label in winforms window will then show
this
rocks
Which is the result I'd like to get. Now, instead of setting the textToShow in the code, I set it in the resource file. When I tried to get the value from resource file using
Properties.Resources.ResourceManager.GetString("textToShow");
the whole string instead will be treated as verbatim, showing
this\nrocks
when put in a label in a winforms window. This is not the result i'm looking for. What's the best way to store strings with special characters in resource file then? I can do string replace for every special characters, like
string.Replace(#"\n", "\n");
but then I need to replace every special characters whenever I call method ResourceManager.GetString, which I think is not the most elegant solution. If there is some ways to make string returned from method ResourceManager.GetString not verbatim, please do tell me.
Thanks
This was already answered here: StackOverflow: How to deal with newline
Basically you have two useful options:
Use shift + enter in the resource manager text editer to add a new line.
Or use String.Format() to replace {0} with \n on read.
The .Net 4.5 framework has the unescape functionality as shown here:
using System.Text.RegularExpressions;
Regex.Unescape(Properties.Resources.ResourceManager.GetString("textToShow"));
solves your issue. Now you can use \n and \u in the resource files.
On the resource editor type "this<shift+enter>rocks" as the resource value.
I know this is kind of easy question but i cant seem to find it anywhere. Is there someone out there who knows how to create a soft return inside a set of text using C#.net?
I need to print soft return to a text file/xml file. this text file will be generated using c#.net. you could verify if the answer is correct if you use NOTEPAD++ then enable the option to “View>Show Symbol > Show End of Line” then you will see a symbol like this:
Thanks in advance :)
Not sure what you mean by a soft return. A quick Google search says it's a non-stored line break typically due to word wrapping in which case you wouldn't actually put this in a string, it would only be relevant when the string was rendered for display.
To put a carriage return and/or line feed in the string you would use:
string s = "line one\r\nline two";
And for further reference, here are the other escape codes that you can use.
Link (MSDN Blogs)
In response to your edit
The LF that you see can be represented with \n in a string. Obviously you have a specific line ending sequence that you need to represent. If you were to use Environment.NewLine that is going to give you different results on different platforms.
var message = $"Tom{Convert.ToChar(10)}Harry";
Results in:
Tom
Harry
With just a line feed between.
Lke already mentioned you can use Enviroment.NewLine but I am not sure if that i what you want or if you are actually trying to append a ASCII 141 to your string as mentioned in the comments.
You can add ASCII chr sequences to your string like this.
var myString = new StringBuilder("Foo");
myString.Append((char)141);
The file format my application uses is Xml based. I just got a customer who has a botched xml file. The thing contains nearly 90,000 lines and for some reason there are about 20 "=" symbols randomly interspersed.
I get an XmlException for most of them with a line number and char position which allows me to find offending chars and remove them manually. I've just started writing a small app that automates this process, but I was wondering if there are better ways to repair damaged xml files.
Example of botched line:
<item name="InstanceGuid" typ=e_name="gh_guid" type_code="9">ee330f9f-a1e2-451a-8c6d-723f066a6bd4</item>
↑ (this is supposed to be [type_name])
You could search for any equal sign that isn't followed by a double quote. A regular expression (regex) would be pretty simple to write up.
Or you could just open the file in an advanced text editor and search by that same regex expression to find and replace/remove. Some text editors allow you to find/replace with regex, so you could search for any equal sign not followed by double quote and just remove it.
Of course, I'd keep a copy of the original since if you had equal signs in the inner XML then it might mess it up, etc.
Use a regular expression to clean the xml first.
something like:
s/([^\s"]+)=([^\s"]+="[^"]*")/\1\2/
Obviously this would need to be ported to your Regex engine of choice :)
In TextPad if you search using the regular expression =[^"] you will find any = signs not followed by a "
This should find the locations in the document where the rogue = signs have appeared. To replace them, first open the document in TextPad. Then press F8.
In the dialog enter the following:
Find what: =\([^"]\)
Replace with: \1
Check the "Regular expressions" box, select "All documents" and click "Replace All"
This should match all = that aren't followed by a " and replace the = with the symbol that did follow it.
typename="test" typ=ename="test"
will become
typename="test" typename="test"