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 203 views
1 answer 227 views
1 answer 95 views
1 answer 146 views
1 answer 148 views
1 answer 192 views
1 answer 164 views
...