Solved

c# String operations

Posted on 2008-10-15
6
429 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
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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:Tiggerito
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 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