如何检查 streamread 行中是否有 4 个或更多空格
how to check if there are 4 or more spaces in streamread line
我正在尝试为文本文件中的管道替换 space,但如果有 4 个或更多 space。到目前为止我的代码是:
string MyNewFile;
using (StreamWriter sWriter = new StreamWriter(MyNewFile, false, encoding, 1))
{
using (StreamReader sReplaceReader = new StreamReader(myFile))
{
string line, textLine = "";
while ((line = sReplaceReader.ReadLine()) != null)
{
if (line.Contains(" ")>=4 )//if contains 4 or more spaces
{
textLine = line.Replace(" ", "|");
}
sWriter.WriteLine(textLine);
}
}
}
我的想法是通过方法中的参数获取 space 的数量,但是,我不知道如何输入 line.Replace(4.space 或 4 char.IsWhiteSpace(),分隔符)。我希望你解释得很好
正则表达式是完成这项工作的好工具。试试这个:
string MyNewFile;
using (StreamWriter sWriter = new StreamWriter(MyNewFile, false, encoding, 1))
{
using (StreamReader sReplaceReader = new StreamReader(myFile))
{
string line, textLine = "";
while ((line = sReplaceReader.ReadLine()) != null)
{
RegexOptions options = RegexOptions.None;
Regex regex = new Regex("[ ]{4,}", options);
string textLine = regex.Replace(line, "|");
sWriter.WriteLine(textLine);
}
}
}
这与这里的答案非常相似:How do I replace multiple spaces with a single space in C#?
您可以使用 RegEx
来执行此操作,并且可以创建一个接受变量输入的方法(因此您可以指定要替换的字符和连续实例的最小数量,以及替换字符串:
public static string ReplaceConsecutiveCharacters(string input, char search,
int minConsecutiveCount, string replace)
{
return input == null
? null
: new Regex($"[{search}]{{{minConsecutiveCount},}}", RegexOptions.None)
.Replace(input, replace);
}
然后可以这样调用:
static void Main()
{
var testStrings = new List<string>
{
"Has spaces scattered throughout the body .",
" starts with spaces and ends with spaces "
};
foreach (var testString in testStrings)
{
var result = ReplaceConsecutiveCharacters(testString, ' ', 4, "|");
Console.WriteLine($"'{testString}' => '{result}'");
}
GetKeyFromUser("\nDone! Press any key to exit...");
}
输出
My idea is to get the number of spaces by a parameter in a method but, I dont know how to put something like line.Replace(4.space or 4 char.IsWhiteSpace(), separator)
private string SpacesToDelimiter(string input, int numSpaces = 4, string delimiter = "|")
{
string target = new String(' ', numSpaces);
return input.Replace(target, delimiter);
}
这样称呼它:
string MyNewFile = "...";
using (StreamWriter sWriter = new StreamWriter(MyNewFile, false, encoding, 1))
{
foreach(string line in File.ReadLines(myFile))
{
sWriter.WriteLine(SpacesToDelimiter(line));
}
}
我正在尝试为文本文件中的管道替换 space,但如果有 4 个或更多 space。到目前为止我的代码是:
string MyNewFile;
using (StreamWriter sWriter = new StreamWriter(MyNewFile, false, encoding, 1))
{
using (StreamReader sReplaceReader = new StreamReader(myFile))
{
string line, textLine = "";
while ((line = sReplaceReader.ReadLine()) != null)
{
if (line.Contains(" ")>=4 )//if contains 4 or more spaces
{
textLine = line.Replace(" ", "|");
}
sWriter.WriteLine(textLine);
}
}
}
我的想法是通过方法中的参数获取 space 的数量,但是,我不知道如何输入 line.Replace(4.space 或 4 char.IsWhiteSpace(),分隔符)。我希望你解释得很好
正则表达式是完成这项工作的好工具。试试这个:
string MyNewFile;
using (StreamWriter sWriter = new StreamWriter(MyNewFile, false, encoding, 1))
{
using (StreamReader sReplaceReader = new StreamReader(myFile))
{
string line, textLine = "";
while ((line = sReplaceReader.ReadLine()) != null)
{
RegexOptions options = RegexOptions.None;
Regex regex = new Regex("[ ]{4,}", options);
string textLine = regex.Replace(line, "|");
sWriter.WriteLine(textLine);
}
}
}
这与这里的答案非常相似:How do I replace multiple spaces with a single space in C#?
您可以使用 RegEx
来执行此操作,并且可以创建一个接受变量输入的方法(因此您可以指定要替换的字符和连续实例的最小数量,以及替换字符串:
public static string ReplaceConsecutiveCharacters(string input, char search,
int minConsecutiveCount, string replace)
{
return input == null
? null
: new Regex($"[{search}]{{{minConsecutiveCount},}}", RegexOptions.None)
.Replace(input, replace);
}
然后可以这样调用:
static void Main()
{
var testStrings = new List<string>
{
"Has spaces scattered throughout the body .",
" starts with spaces and ends with spaces "
};
foreach (var testString in testStrings)
{
var result = ReplaceConsecutiveCharacters(testString, ' ', 4, "|");
Console.WriteLine($"'{testString}' => '{result}'");
}
GetKeyFromUser("\nDone! Press any key to exit...");
}
输出
My idea is to get the number of spaces by a parameter in a method but, I dont know how to put something like line.Replace(4.space or 4 char.IsWhiteSpace(), separator)
private string SpacesToDelimiter(string input, int numSpaces = 4, string delimiter = "|")
{
string target = new String(' ', numSpaces);
return input.Replace(target, delimiter);
}
这样称呼它:
string MyNewFile = "...";
using (StreamWriter sWriter = new StreamWriter(MyNewFile, false, encoding, 1))
{
foreach(string line in File.ReadLines(myFile))
{
sWriter.WriteLine(SpacesToDelimiter(line));
}
}