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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/224236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 18:52:40  来源:igfitidea点击:

Adding a newline into a string in C#

c#string

提问by balaweblog

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@

采纳答案by CMS

Use Environment.NewLinewhenever you want in any string. An example:

使用Environment.NewLine时,你在任何字符串希望。一个例子:

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

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

回答by Jason

You can add a new line character after the @ symbol like so:

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

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

You can also use the NewLineproperty in the EnvironmentClass (I think it is Environment).

您还可以NewLineEnvironment类中使用该属性(我认为是环境)。

回答by Jason Hymanson

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

回答by Marcus Griep

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.

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

回答by Benjamin Autin

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.

如果您不想要文本文件中的换行符,则不要保留它。

回答by Timothy Carter

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

回答by Hath

as others have said new line char will give you a new line in a text file in windows. try the following:

正如其他人所说,新行字符将在 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);
        }
    }
}

回答by Glinas

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

回答by TehSpowage

You could also use string[] something = text.Split('@'). Make sure you use single quotes to surround the "@" to store it as a chartype. 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.

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

回答by Papun Sahoo

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