473,473 Members | 2,316 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2306
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.