web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :

Adding a newline into a string in C# and X++

Rahul Kiran Profile Picture Rahul Kiran 481

Below is the sample code we can use for adding a newline after every occurrence of "@" symbol in the string in C#

 using System;

using System.IO;

static class Program
{
static void Main()
{
WriteToFile
(
@"C:\test.txt",
"fkdfdsfdflkdkfk@dfsdfjk72388389@kdkfkdfkkl@jkdjkfjd@jjjk@",
"@"
);

/*
output in test.txt in windows =
fkdfdsfdflkdkfk@
dfsdfjk72388389@
kdkfkdfkkl@
jkdjkfjd@
jjjk@
*/

}

public static void WriteToFile(string filename, string text, string newLineDelim)
{
bool equal = Environment.NewLine == "\r\n";

//Environment.NewLine == \r\n = True
Console.WriteLine("Environment.NewLine == \\r\\n = {0}", equal);

//replace newLineDelim with newLineDelim + a new line
//trim to get rid of any new lines chars at the end of the file
string filetext = text.Replace(newLineDelim, newLineDelim + Environment.NewLine).Trim();

using (StreamWriter sw = new StreamWriter(File.OpenWrite(filename)))
{
sw.Write(filetext);
}
}
}


Below is the sample code we can use for adding a newline the string in X++
textContainer is a string which I'm used in my code to add my concatinated data. 
  if(textContainer)
textContainer += "\n"
fileData is a string variable and it will be holding my final return value
fileData += System.Text.RegularExpressions.Regex::Replace(textContainer,"\n", System.Environment::NewLine);



This was originally posted here.

Comments

*This post is locked for comments