我正在为和弦创建一个程序。我需要在每一行的开头和每一行的末尾有一个空格,所以程序可以识别和弦。这是替换,但我不知道如何添加空格。
richTextBox1.Text = richTextBox1.Text.Replace(" H ", " X ").Replace(" A# ", " H ").Replace(" A ", " A# ").Replace(" G# ", " A ").Replace(" G ", " G# ").Replace(" F# ", " G ").Replace(" F ", " F# ")
.Replace(" E ", " F ").Replace(" D# ", " E ").Replace(" D ", " D# ").Replace(" C# ", " D ").Replace(" C ", " C# ").Replace(" X ", " C ")
.Replace(" Hm ", " Xm ").Replace(" A#m ", " Hm ").Replace(" Am ", " A#m ").Replace(" G#m ", " Am ").Replace(" Gm ", " G#m ").Replace(" F#m ", " Gm ").Replace(" Fm ", " F#m ")
.Replace(" Em ", " Fm ").Replace(" D#m ", " Em ").Replace(" Dm ", " D#m ").Replace(" C#m ", " Dm ").Replace(" Cm ", " C#m ").Replace(" Xm ", " Cm ");
答案 0 :(得分:1)
我用一些行创建文本文件(每行在开头和结尾都没有空格),如: H X. A#H A#A G#A G G# F#G F F# 这是用于在前面和每行末尾创建带有空格的新文件的代码,我希望这将是您想要的:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace StringLineSpaceAdd
{
class Program
{
static void Main(string[] args)
{
TextReader r = File.OpenText(@"D:\myFile.txt");
string currentLine="";
HashSet<string> previousLines = new HashSet<string>();
TextWriter w = File.CreateText(@"D:\\newFile.txt");
while ((currentLine = r.ReadLine()) != null)
{
// Add returns true if it was actually added,
// false if it was already there
if (previousLines.Add(currentLine))
{
currentLine = " " + currentLine + " ";
w.WriteLine(currentLine);
}
}
w.Close();
}
}
}