How to count the number of characters in a string without spaces and special characters in C#

1 Answer

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

class CountCharactersInStringWithoutSpacesAndSpecialCharacters_CSharp
{
    static void Main()
    {
        string str = "8c# rust, java 123 c &c++.";
        
        int result = Regex.Replace(str, "[^A-Za-z]", "").Length;
        
        Console.WriteLine(result);
    }
}



/*
run

11

*/

 



answered Sep 15, 2024 by avibootz
...