How to split text file into words in C#

1 Answer

0 votes
using System;
using System.Text.RegularExpressions;
using System.IO;

/*
 data.txt
 --------
 c cpp python cshap
 java php

*/

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (string line in File.ReadAllLines("d:\\data.txt"))
            {
                string[] words = SplitToWords(line);

                foreach (string word in words) {
                    Console.WriteLine(word);
                }
            }
        }
        static string[] SplitToWords(string s) {
            return Regex.Split(s, @"\W+");
        }
    }
}


/*
run:

c
cpp
python
cshap
java
php
 
*/

 



answered Aug 8, 2018 by avibootz

Related questions

1 answer 126 views
1 answer 232 views
3 answers 283 views
1 answer 302 views
1 answer 186 views
1 answer 142 views
...