473,383 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

string manipulation

Is there a way to search a string for the number of times
a certain character shows up without having to write my
own function?
string var = "111-222-33.33-2"
var.countchar(var) = 3
Nov 15 '05 #1
10 2302
Kevin <sd***@spamthis.com> wrote:
Is there a way to search a string for the number of times
a certain character shows up without having to write my
own function?
string var = "111-222-33.33-2"
var.countchar(var) = 3


Your example doesn't make a lot of sense - what is it counting there?
You haven't specified a character.

There's no function that I know of which does that - but then it's not
something that needs to be done very often. It's extremely easy to do
though:

int Count (string haystack, char needle)
{
int count=0;
foreach (char c in haystack)
{
if (c==needle)
count++;
}
return count;
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Hi,
parse the string with a regex put the matches in a MatchCollection and take
the count Property of the MatchCollection

--
Mit freundlichen Gruessen - Regards

Ralph Gerbig
ik********@web.de
www.ralphgerbig.de.vu
Nov 15 '05 #3
Ralph Gerbig <ik********@web.de> wrote:
parse the string with a regex put the matches in a MatchCollection and take
the count Property of the MatchCollection


That sounds like an awful lot of work though for something which is
pretty simple. I personally think that regular expressions are rather
overused. They're incredibly powerful, but sometimes a nail is just a
nail, and doesn't need a powertool, just a hammer :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
Ralph,

Can you use Jon's needle and haystack example and let's see what approach is
preferable?
int Count (string haystack, char needle)
{
int count=0;
foreach (char c in haystack)
{
if (c==needle)
count++;
}
return count;
}

Fritz

"Ralph Gerbig" <ik********@web.de> wrote in message
news:ee**************@TK2MSFTNGP11.phx.gbl...
Hi,
parse the string with a regex put the matches in a MatchCollection and take the count Property of the MatchCollection

--
Mit freundlichen Gruessen - Regards

Ralph Gerbig
ik********@web.de
www.ralphgerbig.de.vu

Nov 15 '05 #5
It's an interesting .NET performance question. Just to see how fast each
technique was I wrote a quick test to compare three methods:

Method 1 uses a foreach loop to retrieve each character in the string.

Method 2 uses ToCharArray() to get all the characters in the string, then
uses a for loop (not foreach) to go through each character in the array.

Method 3 uses a Regex and gets the Regex.Matches.Count property.

With a test string that contains 83 characters (arbitrary) it turns out that
method 2 is about five or six times faster than method 1, and about a
hundred times faster than method 3.

With a test string that contains 1000 characters method 2 gets even better,
outperforming method 1 by 7.5 to 1 and method 3 by 150 to 1.

Modifying method 2 to use foreach instead of for didn't measureably change
the performance.

Here's method 2 code:

int CountChar(string s, char ch)
{
int count = 0;
char[] a = s.ToCharArray();
foreach (char c in a)
if (c == ch)
count++;
return count;
}
"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Ralph Gerbig <ik********@web.de> wrote:
parse the string with a regex put the matches in a MatchCollection and take the count Property of the MatchCollection


That sounds like an awful lot of work though for something which is
pretty simple. I personally think that regular expressions are rather
overused. They're incredibly powerful, but sometimes a nail is just a
nail, and doesn't need a powertool, just a hammer :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #6
Bret Mulvey <br***@microsoft.nospam0000.com> wrote:
It's an interesting .NET performance question. Just to see how fast each
technique was I wrote a quick test to compare three methods:

Method 1 uses a foreach loop to retrieve each character in the string.

Method 2 uses ToCharArray() to get all the characters in the string, then
uses a for loop (not foreach) to go through each character in the array.

Method 3 uses a Regex and gets the Regex.Matches.Count property.

With a test string that contains 83 characters (arbitrary) it turns out that
method 2 is about five or six times faster than method 1, and about a
hundred times faster than method 3.


Out of interest, which version of .NET are you using? I believe that
foreach iteration over a string is *much* better with 1.1 than with
1.0.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7
Hi,
It would be three lines :)

--
Mit freundlichen Gruessen - Regards

Ralph Gerbig
ik********@web.de
www.ralphgerbig.de.vu
Nov 15 '05 #8
Ralph Gerbig <ik********@web.de> wrote:
It would be three lines :)


But those three lines would in turn do an awful lot more work - I
suspect you'd find it's very inefficient, and those lines themselves
are *relatively* complicated (that's relative to just counting the
occurrences of the character directly).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #9
It's 1.0. I'll try it with 1.1 to compare.

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Bret Mulvey <br***@microsoft.nospam0000.com> wrote:
It's an interesting .NET performance question. Just to see how fast each
technique was I wrote a quick test to compare three methods:

Method 1 uses a foreach loop to retrieve each character in the string.

Method 2 uses ToCharArray() to get all the characters in the string, then uses a for loop (not foreach) to go through each character in the array.

Method 3 uses a Regex and gets the Regex.Matches.Count property.

With a test string that contains 83 characters (arbitrary) it turns out that method 2 is about five or six times faster than method 1, and about a
hundred times faster than method 3.


Out of interest, which version of .NET are you using? I believe that
foreach iteration over a string is *much* better with 1.1 than with
1.0.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #10
Jon,
I would have used String.IndexOf to find each occurrence of the character,
then I saw your foreach example and thought "Doh! that's simpler".

Something like:

int Count(string haystack, char needle)
{
int count = 0;
for (int index = haystack.IndexOf(needle); index > 0; index =
haystack.IndexOf(needle, ++index))
count++;
return count;
}

Which I would not expect to be more efficient then the foreach loop.

Just another thought on the subject,

Jay

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
Ralph Gerbig <ik********@web.de> wrote:
It would be three lines :)


But those three lines would in turn do an awful lot more work - I
suspect you'd find it's very inefficient, and those lines themselves
are *relatively* complicated (that's relative to just counting the
occurrences of the character directly).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dim | last post by:
I found that C# has some buggy ways to process string across methods. I have a class with on global string var and a method where i add / remove from this string Consider it a buffer... with some...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
4
by: WaterWalk | last post by:
Hello, I'm currently learning string manipulation. I'm curious about what is the favored way for string manipulation in C, expecially when strings contain non-ASCII characters. For example, if...
10
by: micklee74 | last post by:
hi if i have a some lines like this a ) "here is first string" b ) "here is string2" c ) "here is string3" When i specify i only want to print the lines that contains "string" ie the first...
5
by: Niyazi | last post by:
Hi, Does anyone knows any good code for string manipulation similar to RegularExpresion? I might get a value as string in a different format. Example: 20/02/2006 or 20,02,2006 or ...
3
by: crprajan | last post by:
String Manipulation: Given a string like “This is a string”, I want to remove all single characters( alphabets and numerals) like (a, b, 1, 2, .. ) . So the output of the string will be “This is...
7
Frinavale
by: Frinavale | last post by:
I currently have a .NET application that has an object which passes a string (a connection string) as a parameter to another object that does database manipulation. This string isn't stored...
3
by: frankeljw | last post by:
I have 2 Java strings 1st String is a series of names, colons, and numbers ie) Name1:13:Name2:4526:Name3:789:Name4:3729:Name5:6:Name6:44 2nd String is a name ie) Name2 I need to get the...
22
by: mann_mathann | last post by:
can anyone tell me a solution: i cannot use the features in standard c++ string classgh i included the string.h file but still its not working.
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.