在C#中向字符串中添加换行符

本文关键字:添加 换行符 字符串 | 更新日期: 2023-09-27 17:48:51

我有一个字符串。

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

我需要在字符串中每次出现"@"符号后添加一个换行符。

我的输出应该像这个

fkdfdsfdflkdkfk@
dfsdfjk72388389@
kdkfkdfkkl@
jkdjkfjd@
jjjk@

在C#中向字符串中添加换行符

在任何字符串中随时使用Environment.NewLine。一个例子:

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

您可以在@符号后面添加一个换行符,如下所示:

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

您也可以在Environment类中使用NewLine属性(我认为它是Environment)。

前面的答案很接近,但为了满足@符号保持接近的实际要求,您希望它是str.Replace("@", "@" + System.Environment.NewLine)。这将保留@符号,并为当前平台添加适当的换行符。

然后将前面的答案修改为:

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

如果你不想在文本文件中使用换行符,那么就不要保留它

一个简单的字符串替换就可以完成这项工作。看看下面的示例程序:

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

正如其他人所说,换行符将在windows中的文本文件中为您提供一行新行。尝试以下操作:

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

输出

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

根据你对其他人的回复,你正在寻找这样的东西。

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

如下所述更改字符串。

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

您也可以使用string[] something = text.Split('@')。请确保使用单引号将"@"括起来,以将其存储为char类型。这将把最多包含每个"@"的字符存储为数组中的单个单词。然后,您可以使用for循环输出每个(element + System.Environment.NewLine),或者使用System.IO.File.WriteAllLines([file path + name and extension], [array name])将其写入文本文件。如果指定的文件在该位置不存在,则会自动创建该文件。

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