Advertisement
Advertisement


Adding a newline into a string in C#


Question

I have a string.

string strToProcess = "[email protected]@[email protected]@[email protected]";

I need to add a newline after every occurence of "@" symbol in the string.

My Output should be like this

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
2013/08/30
1
232
8/30/2013 9:47:37 AM

Accepted Answer

Use Environment.NewLine whenever you want in any string. An example:

string text = "[email protected]@[email protected]@[email protected]";

text = text.Replace("@", "@" + System.Environment.NewLine);
487
11/15/2019 11:35:44 AM

You can add a new line character after the @ symbol like so:

string newString = oldString.Replace("@", "@\n");  

You can also use the NewLine property in the Environment Class (I think it is Environment).

2012/04/03

The previous answers come close, but to meet the actual requirement that the @ symbol stay close, you'd want that to be str.Replace("@", "@" + System.Environment.NewLine). That will keep the @ symbol and add the appropriate newline character(s) for the current platform.

2008/10/22

Then just modify the previous answers to:

Console.Write(strToProcess.Replace("@", "@" + Environment.NewLine));

If you don't want the newlines in the text file, then don't preserve it.

2018/07/04

as others have said new line char will give you a new line in a text file in windows. try the following:

using System;
using System.IO;

static class Program
{
    static void Main()
    {
        WriteToFile
        (
        @"C:\test.txt",
        "[email protected]@[email protected]@[email protected]",
        "@"
        );

        /*
        output in test.txt in windows =
        [email protected]
        [email protected]
        [email protected]
        [email protected]
        [email protected] 
        */
    }

    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);
        }
    }
}
2008/10/22

A simple string replace will do the job. Take a look at the example program below:

using System;

namespace NewLineThingy
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "[email protected]@[email protected]@[email protected]";
            str = str.Replace("@", "@" + Environment.NewLine);
            Console.WriteLine(str);
            Console.ReadKey();
        }
    }
}
2008/10/22

Source: https://stackoverflow.com/questions/224236
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]