290

I have a string.

string strToProcess = "fkdfdsfdflkdkfk@dfsdfjk72388389@kdkfkdfkkl@jkdjkfjd@jjjk@";

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

My Output should be like this

fkdfdsfdflkdkfk@
dfsdfjk72388389@
kdkfkdfkkl@
jkdjkfjd@
jjjk@
Aristos
  • 66,005
  • 16
  • 114
  • 150
balaweblog
  • 14,982
  • 28
  • 73
  • 95
  • 2
    what do you mean by "the newline is not effective in the text file"? How are you writing to the text file? – Mark Cidade Oct 22 '08 at 05:08
  • 6
    Post the code you use to write the file out. \r\n (windows - Environment.Newline) or \n (Unix) should work... – Gishu Oct 22 '08 at 05:20

13 Answers13

592

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

string text = "fkdfdsfdflkdkfk@dfsdfjk72388389@kdkfkdfkkl@jkdjkfjd@jjjk@";

text = text.Replace("@", "@" + System.Environment.NewLine);
Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • 1
    But I need to write this information in a text file like this fkdfdsfdflkdkfk@ dfsdfjk72388389@ kdkfkdfkkl@ jkdjkfjd@ jjjk@ The New line character is not effective in the text file. – balaweblog Oct 22 '08 at 04:08
  • 1
    This is not a good solution, since System.String (alias `string`) is an immutable object, which means that the entire string is being copied over. Better to use a System.Text.StringBuilder object for operations such as String.Replace. – alexpanter Feb 04 '19 at 09:32
  • @Alexander I'd say that string operations being inefficient is a problem we don't have to worry anymore in 2019. – Filipe Madureira Dec 04 '19 at 12:54
  • 8
    @FilipeMadureira that is so wrong in so many ways. Hardware improvements or not, there is never an excuse for being a bad programmer on purpose. If we know how to do it better we should. Besides imagine implementing this in a library which is being used in a core feature in a database system - you would want high performance. – alexpanter Dec 08 '19 at 11:06
  • @Alexander Being a good programmer is a luxury only people who haven't deadlines / bad jobs/bosses can have, otherwise, I'd agree with you. I thought my point was clear at first. – Filipe Madureira Dec 09 '19 at 12:24
81

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).

Pero P.
  • 25,813
  • 9
  • 61
  • 85
Jason
  • 1,119
  • 8
  • 9
  • 1
    But I need to write this information in a text file like this fkdfdsfdflkdkfk@ dfsdfjk72388389@ kdkfkdfkkl@ jkdjkfjd@ jjjk@ The New line character is not effective in the text file. – balaweblog Oct 22 '08 at 03:23
21

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.

Marcus Griep
  • 8,156
  • 1
  • 23
  • 24
13

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.

Samuel Rondeau-Millaire
  • 1,100
  • 2
  • 13
  • 24
Benjamin Autin
  • 4,143
  • 26
  • 34
8

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 = "fkdfdsfdflkdkfk@dfsdfjk72388389@kdkfkdfkkl@jkdjkfjd@jjjk@";
            str = str.Replace("@", "@" + Environment.NewLine);
            Console.WriteLine(str);
            Console.ReadKey();
        }
    }
}
Jason Jackson
  • 17,016
  • 8
  • 49
  • 74
8

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",
        "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);
        }
    }
}
Hath
  • 12,606
  • 7
  • 36
  • 38
  • Only saw your environment.newline == \r\n = true now (maybe I missed it because it is not C# syntax ;) ). Also, more info here http://msdn.microsoft.com/de-de/library/system.environment.newline.aspx. Just stumbled upon a similar problem with using hardcoded \n not giving me a new line in windows.. – Andreas Reiff May 03 '12 at 15:35
5
string strToProcess = "fkdfdsfdflkdkfk@dfsdfjk72388389@kdkfkdfkkl@jkdjkfjd@jjjk@";
var result = strToProcess.Replace("@", "@ \r\n");
Console.WriteLine(result);

Output

Kathiravan C
  • 51
  • 1
  • 2
  • 2
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Thomas Flinkow Mar 07 '18 at 12:32
4
string str = "fkdfdsfdflkdkfk@dfsdfjk72388389@kdkfkdfkkl@jkdjkfjd@jjjk@";
str = str.Replace("@", Environment.NewLine);
richTextBox1.Text = str;
takrl
  • 6,356
  • 3
  • 60
  • 69
Glinas
  • 41
  • 1
3

Based on your replies to everyone else, something like this is what you're looking for.

string file = @"C:\file.txt";
string strToProcess = "fkdfdsfdflkdkfk@dfsdfjk72388389@kdkfkdfkkl@jkdjkfjd@jjjk@";
string[] lines = strToProcess.Split(new char[] { '@' }, StringSplitOptions.RemoveEmptyEntries);

using (StreamWriter writer = new StreamWriter(file))
{
    foreach (string line in lines)
    {
        writer.WriteLine(line + "@");
    }
}
Himanshu
  • 31,810
  • 31
  • 111
  • 133
Timothy Carter
  • 15,459
  • 7
  • 44
  • 62
3

Change your string as mentioned below.

string strToProcess = "fkdfdsfdflkdkfk"+ System.Environment.NewLine +" dfsdfjk72388389"+ System.Environment.NewLine +"kdkfkdfkkl"+ System.Environment.NewLine +"jkdjkfjd"+ System.Environment.NewLine +"jjjk"+ System.Environment.NewLine;
1

You could also use string[] something = text.Split('@'). Make sure you use single quotes to surround the "@" to store it as a char type. This will store the characters up to and including each "@" as individual words in the array. You can then output each (element + System.Environment.NewLine) using a for loop or write it to a text file using System.IO.File.WriteAllLines([file path + name and extension], [array name]). If the specified file doesn't exist in that location it will be automatically created.

TehSpowage
  • 11
  • 3
-2
protected void Button1_Click(object sender, EventArgs e)
{
    string str = "fkdfdsfdflkdkfk@dfsdfjk72388389@kdkfkdfkkl@jkdjkfjd@jjjk@";
    str = str.Replace("@", "@" + "<br/>");
    Response.Write(str);       
}
Aleks Andreev
  • 7,016
  • 8
  • 29
  • 37
Papun Sahoo
  • 407
  • 5
  • 13
  • 1
    Welcome to the SO community. Thank you for answering the question. However it is good practice to add some explanation as to why this works. – phuzi Dec 07 '17 at 12:40
-2
using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main()
    {
             string strToProcess = "fkdfdsfdflkdkfk@dfsdfjk72388389@kdkfkdfkkl@jkdjkfjd@jjjk@";
            strToProcess.Replace("@", Environment.NewLine);
            Console.WriteLine(strToProcess);
    }
}
FAREH
  • 31
  • 6