Adding a newline into a string in C#

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@
2

13 Answers

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

string text = "fkdfdsfdflkdkfk@dfsdfjk72388389@kdkfkdfkkl@jkdjkfjd@jjjk@";
text = text.Replace("@", "@" + System.Environment.NewLine);
5

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

1

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.

0

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.

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(); } }
}

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); } }
}
1
string strToProcess = "fkdfdsfdflkdkfk@dfsdfjk72388389@kdkfkdfkkl@jkdjkfjd@jjjk@";
var result = strToProcess.Replace("@", "@ \r\n");
Console.WriteLine(result);

Output

1
string str = "fkdfdsfdflkdkfk@dfsdfjk72388389@kdkfkdfkkl@jkdjkfjd@jjjk@";
str = str.Replace("@", Environment.NewLine);
richTextBox1.Text = str;

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 + "@"); }
}

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;

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.

protected void Button1_Click(object sender, EventArgs e)
{ string str = "fkdfdsfdflkdkfk@dfsdfjk72388389@kdkfkdfkkl@jkdjkfjd@jjjk@"; str = str.Replace("@", "@" + "<br/>"); Response.Write(str);
}
1
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); }
}

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like