\r\n in C# not replaced - c#

I have this string read from a table in sql:
Yderligere tillidshverv:
Medlem af bestyrelsen for Dansk XXSW, region Ã…rhus.
Andre aktiviteter:
Opfinder af 123 trapper.
What I want to do is replace occurences of \r\n with just a single \n. However it is not working. The problem is that notepad++ still shows CRLF. It should only show LF. What am I missing? Here is my code:
string x = returnValue.Replace("\r\n", "\n");

When you paste a string from a Windows clipboard into Notepad++, the linebreaks get converted to the line ending style set for the current document (see the bottom of the screen).
If you right click and select UNIX, you will be able to paste the string with \n only. This will also change all line endings to LF.
If you do not want to change all line endings in the document to LF, you can use EDIT > Paste Special -> Paste Binary Content
However, from C#, you can just write the variable content into a text file:
var s = "I know\r\nit should\r\n";
var b = s.Replace("\r\n", "\n");
using (var sw = new StreamWriter(#"PATH_TO_FILE", false, Encoding.UTF8))
{
sw.Write(b);
}
And enjoy \n linebreaks.

Using the Regex class will work. I used linqpad to quickly test code which has the Dump() extension method
Regex.Replace("test\r\ntest","\r\n","\n").Select(x => x).Dump();
Update : A regular replace also works BTW. You state that it doesn't?
"test\r\ntest".Replace("\r\n","\n").Select(x => x).Dump();

Related

Why don't new line characters appear in the textbox when I use textBox1.AppendText

I have been trying to make some kind of a terminal application using ssh.net libray in c#. I have been succesful to send commands and recieve output but when I append the the output to textbox new line characters disappear although I can see them in message box. How can I fix this problem?
SshCommand sc = client.CreateCommand(command);
sc.Execute();
textBox1.AppendText(command);
textBox1.AppendText("\n");
string answer = sc.Result;
textBox1.AppendText(answer);
MessageBox.Show(sc.Result);
textBox1.AppendText("\n");
textBox2.Clear();
When I run ls command, I get the output as:
The problem is that you use the TextBox which doesn't work with \n, it needs \r\n as a new line.
You can use RichTextBox which works with only a \n as newline.

Add a symbol to a .docx file

I want to be able to add a "tick" to the end of a sentence using the DocX library.
//Create the table
Table signingTable = document.AddTable(18, 6);
//....many lines of code
string mySentence = "Hello World" + <tick>;
signingTable.Rows[entryItem + 1].Cells[3].Paragraphs.First().Append(mySentence);
such that when my table renders it will have a tick symbol next to the text Hello World. Can someone help with the encoding I need to add to make this work?
The tick mark is ascii character 252 in the wingdings font.
You need to add another run to the paragraph, set the run font to wingdings and add chr(252) into it.
Using the DocX library you would do something like the following:
char tick = (char) 252;
p.Append("Hello World").Append(tick).Font(new FontFamily("Wingdings"));
Using SDK OpenXML you would do something like this:
p.Append(new Run( new SymbolChar() { Font = "Wingdings", Char = "F0FE" }));

Plain text to HTML conversion in Outlook email

I am facing a situation to preserve the formatting of plain text email when displaying it as virtual plain text in C sharp. This is done during receiving in Outlook 2007 using VSTO.
The code below does not do the job, instead it converts the body into Times New Roman;Font Size 10 and displays it to the user.
string Text = "<html><body><p style=\"font-family:consolas;font-size:88%;\">" + mailItem.Body+ "</p></body></html>";
mailItem.HTMLBody = Text;
mailItem.HTMLBody = Regex.Replace(mailItem.HTMLBody, "(ASA[a-z][a-z][0-9][0-9])", "$&");
How can I rectify this problem?
EDIT:
Input:
ASAss87
ASAjj98
this is test input
Output:
ASAss87 ASAjj98 this is test input
EDIT 2:
Input:
ASAss87
ASAjj98
this is test input.
Output:
ASAss87
ASAjj98
this is test input.
*Moves one or two spaces forward. I am using tag.
Based on your feedback in the comments, try changing your first line to use Body instead of HTMLBody:
string Text = "<html><body><p style=\"font-family:consolas;font-size:88%;\">" + mailItem.Body+ "</p></body></html>";
Edit: Since the plain text contains line-breaks, maybe you should use a <pre> tag instead of a <p> tag, to prevent it from putting everything on one line.
string Text = "<html><body><pre style=\"font-family:consolas;font-size:88%;\">" + mailItem.Body+ "</pre></body></html>";
Edit2: Alternatively, you can replace all line-breaks with <br> tags.
string Text = "<html><body><p style=\"font-family:consolas;font-size:88%;\">" + mailItem.Body.Replace(Environment.NewLine,"<BR>") + "</p></body></html>";

Word 2010 Content Controls New Line

I am populating a content control in word 2010 with some text. I can't get my text to go on a new line. I have tried '\n' ,'\r', Environment.NewLine and it still appears on one line.
Any ideas what I should be using
I believe you may need to enable MultiLine = True for the CC. See: http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.contentcontrol.multiline(v=VS.90).aspx
If you've already done that, see Microsoft Word Document Controls not accepting carriage returns for a possible solution
You could use something like this:
private static Run InsertFormatRun(Run run, string[] formatText)
{
foreach (string text in formatText)
{
run.AppendChild(new Text(text));
RunProperties runProps = run.AppendChild(new RunProperties());
Break linebreak = new Break();
runProps.AppendChild(linebreak);
}
return run;
}
If you are using VBA, what you are searching for is
vbNewLine
which will insert a full line break.

Writing into a text file line by line

I am writing a text into a file. I am getting a column from database and stored it in a string and writing it in a text file. That column contains c# code and it is writing like a single line with small squares for next line(space). I need to write it line by line. Here is my code.
using (var dest = File.AppendText(Path.Combine(_logFolderPath, "a.txt")))
{
dest.WriteLine(line.TrimStart());
}
Any suggestion?
Does Notepad show small squares for new lines, but when you look at the file in Visual Studio it's OK? If so, my guess is that this will fix it:
dest.WriteLine(line.TrimStart().Replace("\n", Environment.NewLine));
try this
File.AppendAllText(Path.Combine(_logFolderPath, "a.txt",
line.Trim() + Environment.NewLine));
dest.WriteLine(line.TrimStart().Replace("\n", Environment.NewLine).Replace("\r", Environment.NewLine));

Categories

Resources