?
Solved

c# String operations

Posted on 2008-10-15
6
Medium Priority
?
438 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 2000 total points
ID: 22727974
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
ID: 22728123
what about this a0G1#2jtU??
a0G1#2jtU is two words,
# as a separator
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22728259
\S+ defines the separator between the words
you can add more separators too
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:klay8
ID: 22728577
so what will be the pattern?
0
 

Author Comment

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

Expert Comment

by:Tony McCreath
ID: 22730218
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month16 days, 15 hours left to enroll

864 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