我有一個字符串。將一個換行符添加到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