Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to write a program in WPF which will read a text.txt file with openfiledialouge and will show result occurrence of only special characters(~!@#$%^&*()_+-) in textbox. I want to count the occurrence of only special characters and output in textbox.

sample output:
Number of #: 242
Number of @ : 212
.
.
.etc

What I have tried:

protected void OFD_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();

if (openFile.ShowDialog()==true)
{
string filepath = openFile.FileName;
textbox.Text = File.ReadAllText(filepath);


}}
Posted
Updated 30-Jan-18 19:53pm
v2

See here: Counting Lines in a String[^] - it's to do with lines, but they are just a "special delimiter" (i.e. '\n') which you could replace with an array of "searchable characters".
It presents a variety of ways to do it, and tells you the probable performance cost of each.
 
Share this answer
 
So in order to do this I suggest you read up on regexes but here is an example

Regex Class (System.Text.RegularExpressions)[^]

Regular Expression Language - Quick Reference | Microsoft Docs[^]

C# Regular Expressions[^]

[^]

C#
// Define special characters in regex string
var regexString = @"[(~!@#$%^&*()_+\-)]";
// Simulate reading text/content from a file
            var fileContent = @"My name is !@#$@# and I live in #$%%^&^%@! at 100 #$% mainstreet";

            var rex = new Regex(regexString, RegexOptions.IgnoreCase);

            var results = rex.Matches(fileContent);

            foreach (var item in results)
            {
                // find matches on individual items and get count
                var subRegex = Regex.Matches(fileContent, item.ToString());
                Console.WriteLine("Character: {0} - appearances: {1}", item, subRegex.Count);
            }
 
Share this answer
 
Comments
Ĥãsŋäḭn Ɏḁɋoȱb 31-Jan-18 1:50am    
thanks for your reply , but my question was about counting only special characters from the text file and showing them in textbox in WPF.

sample output:
Number of # : 34
Number of @ : 121
David_Wimbley 31-Jan-18 10:20am    
Did you even run the code or read my answer? The code does what you need...Your going to have to do some work on your side to actually put it in place how you need it, this isn't a 100% solution but it gets you 90% there.

If your looking for an exact solution to your problem then you need to go to freelancer.com

All you have to do is read the code and massage it into your wpf app...no one here has access to your app, code, or hard drive.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900