Solved

c# String operations

Posted on 2008-10-15
6
431 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jquery + C#.net radiobuttonlist 3 47
linq, c# 8 37
WPF user control poped up from a window 1 35
Nested forach loop to linq 3 30
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

697 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