Solved

c# String operations

Posted on 2008-10-15
6
427 Views
Last Modified: 2012-05-05
hi
i would like to count the number of words that i have typed:
For example:
The cat in the hat  5 words
The cat, in the hat  5 words
The cat , in the hat  5 words
The cat
 
in the hat  5 words

Note: Quote characters have been used to denote the start and end of the string.  

 a word is defined as a continuous string of the character ranges a-z, A-Z or 0-9 separated by one or more other characters e.g. a012jtU is one word, a0G1#2jtU is two words because the # is a separator.

tanks
0
Comment
Question by:klay8
  • 3
  • 2
6 Comments
 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 500 total points
Comment Utility
string pattern = @"\b(\S+)\b";
int wordCount = Regex.Matches ("The cat , in the hat", pattern, RegexOptions.None).Count;

wordcount has 5 in it
0
 

Author Comment

by:klay8
Comment Utility
what about this a0G1#2jtU??
a0G1#2jtU is two words,
# as a separator
0
 
LVL 26

Expert Comment

by:Anurag Thakur
Comment Utility
\S+ defines the separator between the words
you can add more separators too
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:klay8
Comment Utility
so what will be the pattern?
0
 

Author Comment

by:klay8
Comment Utility
actually not only # is a separator, all non alphanumerics are separators in this example
0
 
LVL 23

Expert Comment

by:Tiggerito
Comment Utility
try

[a-zA-Z0-9]+
string inp = "The cat, in t#he hat";

Regex regex = new Regex("[a-zA-Z0-9]+");
 

MatchCollection matches = regex.Matches(inp);
 

int count = matches.Count;

Open in new window

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now