在C#中将换行符添加到字符串中

时间:2008-10-22 02:14:57

标签: c# string

我有一个字符串。

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

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

我的输出应该是这样的

fkdfdsfdflkdkfk@
dfsdfjk72388389@
kdkfkdfkkl@
jkdjkfjd@
jjjk@

12 个答案:

答案 0 :(得分:438)

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

text = text.Replace("@", "@" + System.Environment.NewLine);

答案 1 :(得分:64)

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

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

您还可以使用NewLine类中的Environment属性(我认为它是环境)。

答案 2 :(得分:18)

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

答案 3 :(得分:9)

然后只需修改以前的答案:

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

如果您不想在文本文件中添加换行符,请不要保留它。

答案 4 :(得分:6)

简单的字符串替换将完成这项工作。看看下面的示例程序:

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

答案 5 :(得分:6)

正如其他人所说的那样,新行char会在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);
        }
    }
}

答案 6 :(得分:3)

根据您对其他人的回复,您正在寻找类似的内容。

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

答案 7 :(得分:2)

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

答案 8 :(得分:1)

string strToProcess = "fkdfdsfdflkdkfk@dfsdfjk72388389@kdkfkdfkkl@jkdjkfjd@jjjk@";
var result = strToProcess.Replace("@", "@ \r\n");
Console.WriteLine(result);

Output

答案 9 :(得分:0)

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

答案 10 :(得分:0)

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

答案 11 :(得分:0)

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