How to split string words by punctuation in C#

1 Answer

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

class Program
{
    private static string[] SplitByPunctuation(string s) {
        return Regex.Split(s, @"\W+");
    }
    static void Main() {
        string s = "csharp. c-sharp, c#, desktop .software$ programming!";

        string[] arr = SplitByPunctuation(s);

        foreach (string element in arr)
            Console.WriteLine(element);
    }
}





/*
run:

csharp
c
sharp
c
desktop
software
programming

*/

 



answered Oct 26, 2022 by avibootz

Related questions

1 answer 283 views
1 answer 311 views
1 answer 202 views
1 answer 236 views
1 answer 229 views
1 answer 265 views
1 answer 260 views
...