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 184 views
1 answer 203 views
1 answer 72 views
1 answer 133 views
1 answer 126 views
1 answer 186 views
1 answer 143 views
...