在每一行的开头和结尾添加空格 C#
Add blank spaces in the beginning and end of each line C#
我正在为和弦创建一个程序。我需要在每一行的开头和每一行的结尾都有一个 space,所以程序可以识别 chords.Here 是替换的,但我不知道如何添加空白 spaces.
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 ");
我创建了一些文本文件(每一行的开头和结尾都没有 space),例如:
HX
#H
一个#
G#A
G##
F#G
F#
这是制作新文件的代码,每行前面和末尾都带有 space 我希望这会如您所愿:
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();
}
}
}
我正在为和弦创建一个程序。我需要在每一行的开头和每一行的结尾都有一个 space,所以程序可以识别 chords.Here 是替换的,但我不知道如何添加空白 spaces.
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 ");
我创建了一些文本文件(每一行的开头和结尾都没有 space),例如: HX #H 一个# G#A G## F#G F# 这是制作新文件的代码,每行前面和末尾都带有 space 我希望这会如您所愿:
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();
}
}
}