如何在txt文件中省略数字?
我一直在尝试在使用文本文件时忽略文本文件中的数字,File.ReadAllLines
但找不到它,因此示例如下所示
1 In the land far away.
没有简单的代码可以省略数字。我有笔记,但忘了放在哪里。
因此,我们将不胜感激任何帮助。
我已经开始使用替换功能删除每个数字。但是我试图记住它的代码片段。
_recognizer.LoadGrammarAsync(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(@"Dictionary.txt" )))));
您可以使用RegEx将所有数字替换为空字符串:
string value = "1 In the land far away.";
value = Regex.Replace(value, @"^[\d-]*\s*", "");
// value = " In the land far away."
或者在您的情况下,由于ReadAllLines返回一个字符串数组,因此您可以执行类似的操作:
string[] fileContent = File.ReadAllLines(@"Dictionary.txt");
for(int index = 0; index < fileContent.Length; index++)
fileContent[index] = Regex.Replace(fileContent[index], @"^[\d-]*\s*", "");
_recognizer.LoadGrammarAsync(new Grammar(new GrammarBuilder(new Choices(fileContent))));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句