campnerd campnerd - 1 month ago 5x
C# Question

Omitting a Number in a txt file

How to omit a number in a txt file?

I have been trying to omit a number from a text file when using

i had notes on it and i can not find them and the example is like this

1 In the land far away.

Isn't there a simple code to omit numbers. I had the notes but I forgot where i put them.

so any help will be greatly appreciated.

I had started deleting each number with the replace feature. However I was trying to remember the code snippet for it.

_recognizer.LoadGrammarAsync(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(@"Dictionary.txt" )))));


You can use RegEx to replace all number by an empty string :

string value = "1 In the land far away.";
value = Regex.Replace(value, @"^[\d-]*\s*", "");
// value = " In the land far away."

Or in your case, since ReadAllLines return a string array, you could do something similar :

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))));