How to split words from a string into an array of strings in C#

1 Answer

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

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "c# java, vb.net ! c++ $ python%$";

            string[] arr = SplitWords(s);

            foreach (string word in arr)
            {
                Console.WriteLine(word);
            }
        }
        static string[] SplitWords(string s)
        {
            return Regex.Split(s, @"\W+"); // separate non-word
        }
    }
}

/*
run:

c
java
vb
net
c
python

*/

 



answered Jan 24, 2017 by avibootz

Related questions

1 answer 204 views
1 answer 219 views
1 answer 175 views
1 answer 203 views
1 answer 190 views
1 answer 136 views
...