How to split string into words in C#

2 Answers

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

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "c cpp csharp java python and php";

            string[] words = Regex.Split(s, @"\W+");

            foreach (string word in words) {
                Console.WriteLine(word);
            }
        }
    }
}


/*
run:

c
cpp
csharp
java
python
and
php
 
*/

 



answered Aug 8, 2018 by avibootz
0 votes
using System;
using System.Text.RegularExpressions;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "c cpp csharp java python and php";

            string[] words = SplitToWords(s);

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


/*
run:

c
cpp
csharp
java
python
and
php
 
*/

 



answered Aug 8, 2018 by avibootz

Related questions

1 answer 126 views
1 answer 186 views
1 answer 143 views
1 answer 133 views
1 answer 159 views
1 answer 239 views
239 views asked Aug 8, 2018 by avibootz
1 answer 148 views
...