我有一根绳子。

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

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

我的输出应该像这样

fkdfdsfdflkdkfk@
dfsdfjk72388389@
kdkfkdfkkl@
jkdjkfjd@
jjjk@

当前回答

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

其他回答

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

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

使用环境。你可以在任何字符串中使用换行符。一个例子:

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

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

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

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

如果您不希望文本文件中出现换行符,那么就不要保存它。

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

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.