Wrox Programmer Forums
|
C# 4.0 aka C# 2010 General Discussion Discussions about the C# 4.0, C# 4, Visual C# 2010 language and tool not related to any specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 4.0 aka C# 2010 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 22nd, 2011, 06:36 PM
Authorized User
 
Join Date: May 2011
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default String search and count

Quick question. Is there a way to search a string and return the number of matches? Example, the string is "kkllkjhgfklkjjj" and the code would return the number of k's.
 
Old July 22nd, 2011, 06:43 PM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

Mark, as a string is just a collection of characters you can use LINQ to filter the string any way you want:
Code:
var count = (from c in s
                   where c == 'k'
                   select c).Count();
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
 
Old July 22nd, 2011, 07:15 PM
Authorized User
 
Join Date: May 2011
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Christian, works like a charm, as usual, thanks a bunch for your reply.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Word search - Count Hits bonekrusher XSLT 5 January 2nd, 2008 10:26 AM
New text search doesn't preselect search string planoie Visual Studio 2005 0 July 23rd, 2007 06:47 AM
Count all occurences of a character in a string thomaz C# 2 February 9th, 2007 01:52 PM
getting a count of values from within a string cole SQL Server 2000 6 September 5th, 2006 11:20 AM
how to count the database search results of record gilgalbiblewheel Classic ASP Databases 2 July 31st, 2004 10:37 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.