我有一个字符串。将一个换行符添加到C中的字符串中#
string strToProcess = "[email protected]@[email protected]@[email protected]";
我需要在每个字符串中出现“@”符号后添加一个换行符。
我的输出应该是这样的
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
我有一个字符串。将一个换行符添加到C中的字符串中#
string strToProcess = "[email protected]@[email protected]@[email protected]";
我需要在每个字符串中出现“@”符号后添加一个换行符。
我的输出应该是这样的
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
string text = "[email protected]@[email protected]@[email protected]";
text = text.Replace("@", "@" + System.Environment.NewLine);
但我需要写在这样fkdfdsfdflkdkfk @ dfsdfjk72388389 @ kdkfkdfkkl @ jkdjkfjd @ jjjk一个文本文件,此信息@换行符在文本文件中无效。 – balaweblog 2008-10-22 04:08:18
使用“\ r \ n”,它显示在文本正文;) – 2013-10-27 12:52:35
你可以像这样的@符号后添加一个新行字符:
string newString = oldString.Replace("@", "@\n");
您也可以使用NewLine
属性的Environment
类(我认为它是环境)。
但我需要写这样 fkdfdsfdflkdkfk @ dfsdfjk72388389 @ kdkfkdfkkl @ jkdjkfjd @ jjjk一个文本文件,此信息@ 新行字符不是在文本文件中有效。 – balaweblog 2008-10-22 03:23:22
一个简单的字符串替换将完成这项工作。看看下面的例子程序:
using System;
namespace NewLineThingy
{
class Program
{
static void Main(string[] args)
{
string str = "[email protected]@[email protected]@[email protected]";
str = str.Replace("@", "@" + Environment.NewLine);
Console.WriteLine(str);
Console.ReadKey();
}
}
}
以前的答案接近,但满足了@
符号贴近,你会想,要成为str.Replace("@", "@" + System.Environment.NewLine)
实际需求。这将保持@
符号并为当前平台添加适当的换行符。
我编辑了以前的答案之一来反映这一点。 – 2008-10-22 02:27:22
谢谢你:-) – 2008-10-22 02:56:44
然后,只需修改前面的答案:
Console.Write(strToProcess.Replace("@", "@" + Environment.Newline));
如果你不需要想在文本文件中的换行,然后不保存它。
根据你对其他人的回复,类似这样的事情就是你要找的。
string file = @"C:\file.txt";
string strToProcess = "[email protected]@[email protected]@[email protected]";
string[] lines = strToProcess.Split(new char[] { '@' }, StringSplitOptions.RemoveEmptyEntries);
using (StreamWriter writer = new StreamWriter(file))
{
foreach (string line in lines)
{
writer.WriteLine(line + "@");
}
}
正如其他人所说的新行字符将在Windows中的文本文件中给你一个新的行。 尝试以下操作:
using System;
using System.IO;
static class Program
{
static void Main()
{
WriteToFile
(
@"C:\test.txt",
"[email protected]@[email protected]@[email protected]",
"@"
);
/*
output in test.txt in windows =
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
*/
}
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 str = "[email protected]@[email protected]@[email protected]";
str = str.Replace("@", Environment.NewLine);
richTextBox1.Text = str;
你也可以使用string[] something = text.Split('@')
。确保您使用单引号将“@”包围起来以将其存储为char
类型。 这将字符存储直至并包括每个“@”作为阵列中的各个字。然后,您可以输出的每个(element + System.Environment.NewLine
)使用for循环或将其写入使用System.IO.File.WriteAllLines([file path + name and extension], [array name])
一个文本文件。如果指定的文件不存在于该位置,它将自动创建。
protected void Button1_Click(object sender, EventArgs e)
{
string str = "[email protected]@[email protected]@[email protected]";
str = str.Replace("@", "@" + "<br/>");
Response.Write(str);
}
using System;
using System.IO;
using System.Text;
class Test
{
public static void Main()
{
string strToProcess = "[email protected]@[email protected]@[email protected]";
strToProcess.Replace("@", Environment.NewLine);
Console.WriteLine(strToProcess);
}
}
string strToProcess = "[email protected]@[email protected]@[email protected]";
var result = strToProcess.Replace("@", "@ \r\n");
Console.WriteLine(result);
什么叫 “换行是在文本文件中没有有效的” 是什么意思?你如何写文本文件? – 2008-10-22 05:08:52
邮政代码使用写文件了。 \ r \ n(窗口 - Environment.Newline)或\ n(UNIX)应该工作... – Gishu 2008-10-22 05:20:14