473,416 Members | 1,502 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,416 software developers and data experts.

String method that counts substrings

Is there a string method that allows gives you the number of times a
substring appears in your string.

Looping through my string performing IndexOf("substring",startPos), seems
like overkill.

thank you.
CR
Nov 17 '05 #1
10 5379
Use the split method!
If the string was splited 2 times then the substring appears 2 times!

i don't know if this is the best option but, who knows...

"CodeRazor" <Co*******@discussions.microsoft.com> wrote in message
news:48**********************************@microsof t.com...
Is there a string method that allows gives you the number of times a
substring appears in your string.

Looping through my string performing IndexOf("substring",startPos), seems
like overkill.

thank you.
CR

Nov 17 '05 #2
yes, that's what i thought originally, but it seems that the split method
works uses a char to split the string into substrings.
The delimeter i need to use has to be longer than a char.

e.g. string myString = "<p>hello<p>my nam is<p>ljksdf";
I want to split myString into an array but at after each <p>
The split method only works if you're using a delimeter of one character.

Any ideas?
Nov 17 '05 #3
"CodeRazor" <Co*******@discussions.microsoft.com> schrieb im Newsbeitrag
news:48**********************************@microsof t.com...
Is there a string method that allows gives you the number of times a
substring appears in your string.

Looping through my string performing IndexOf("substring",startPos), seems
like overkill.

thank you.
CR


Hi CR,

why do you think it's overkill.
No implementation could it do simpler than searching for the substring and
counting the matches.
What are you doing more?
In fact the way suggested by Rodrigo is more overkill, because it's creating
additional string instances (one more than the count), wich never are used.

Christof
Nov 17 '05 #4
regular expressions will perform simple to complex pattern matches, including
give you the number of occurances.

look up the Regex class and documentation on regular expression syntax.
"Rodrigo Ferreira" wrote:
Use the split method!
If the string was splited 2 times then the substring appears 2 times!

i don't know if this is the best option but, who knows...

"CodeRazor" <Co*******@discussions.microsoft.com> wrote in message
news:48**********************************@microsof t.com...
Is there a string method that allows gives you the number of times a
substring appears in your string.

Looping through my string performing IndexOf("substring",startPos), seems
like overkill.

thank you.
CR


Nov 17 '05 #5
hi,

Try this

string[] parts = (new Regex("<p>")).Split( sourcestring);

please note that I wrote the above code here, so it may not work, just try
it and play with the RegEx.Split

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"CodeRazor" <Co*******@discussions.microsoft.com> wrote in message
news:48**********************************@microsof t.com...
Is there a string method that allows gives you the number of times a
substring appears in your string.

Looping through my string performing IndexOf("substring",startPos), seems
like overkill.

thank you.
CR

Nov 17 '05 #6
Ignacio,

that's perfect. It splits up my string with my specified substring. What i
was looking for.
Very elegant solution. Thank you.

See:

string test = "";
string strText="One<p>Two<p>Three<p>four";

foreach(string strItem in Regex.Split(strText,"<p>"))
{
test += strItem;
test += "-";
}

string result = test;
Nov 17 '05 #7
CodeRazor <Co*******@discussions.microsoft.com> wrote:
Ignacio,

that's perfect. It splits up my string with my specified substring. What i
was looking for.
Very elegant solution. Thank you.

See:

string test = "";
string strText="One<p>Two<p>Three<p>four";

foreach(string strItem in Regex.Split(strText,"<p>"))
{
test += strItem;
test += "-";
}

string result = test;


When building up a string with a loop, you should use a StringBuilder -
if you ever received a string with thousands of <p> markers in, your
concatenation would get very expensive.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8
You should be able to use the old version.

Microsoft.VisualBasic.Split

--
Jonathan Allen
"CodeRazor" <Co*******@discussions.microsoft.com> wrote in message
news:B2**********************************@microsof t.com...
yes, that's what i thought originally, but it seems that the split method
works uses a char to split the string into substrings.
The delimeter i need to use has to be longer than a char.

e.g. string myString = "<p>hello<p>my nam is<p>ljksdf";
I want to split myString into an array but at after each <p>
The split method only works if you're using a delimeter of one character.

Any ideas?

Nov 17 '05 #9

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
CodeRazor <Co*******@discussions.microsoft.com> wrote:
Ignacio,

that's perfect. It splits up my string with my specified substring. What
i
was looking for.
Very elegant solution. Thank you.

See:

string test = "";
string strText="One<p>Two<p>Three<p>four";

foreach(string strItem in Regex.Split(strText,"<p>"))
{
test += strItem;
test += "-";
}

string result = test;


When building up a string with a loop, you should use a StringBuilder -
if you ever received a string with thousands of <p> markers in, your
concatenation would get very expensive.

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


If there is a string with thousands of <p> markers in it, I would suggest
finding an alternative string altogether :P

Mythran

Nov 17 '05 #10
thanks mythran,

but my intention is not to just build up a long string. I'm going to use the
newly createed substrings in other functions. The example given was dumbed
down just so people understood the immediate funtionality i needed.

cheers for your help. and i will bear in mind the StringBuilder advice.

CR

Nov 17 '05 #11

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

Similar topics

1
by: Dino Buljubasic | last post by:
Hi, I am using lots of string comparisons using String.StartsWith(subString) method where subString might be more than just couple of chars (e.g. "This is the string to compare ") I have...
2
by: Jason Barnett | last post by:
I thought I had seen a method for returning the number of occurances of a substring within a string object, but I can't seem to find it now. Does anyone know of something? I know I can parse...
2
by: Digital Fart | last post by:
following code would split a string "a != b" into 2 strings "a" and "b". but is there a way to know what seperator was used? string charSeparators = { "=", ">=", "<=" , "!=" }; string s1 =...
11
by: Prof.Stanley | last post by:
hello , i ma new in this group and writing over the fact that i have a small project at hand,i am writing a program which can permit the input of a text of arbitary lenght,and the number of double...
3
by: Girish Sahani | last post by:
Given a length k string,i want to search for 2 substrings (overlap possible) in a list consisting of length k-1 strings. These 2 substrings when 'united' give the original string. e.g given...
8
by: girish | last post by:
Hi, I want to generate all non-empty substrings of a string of length >=2. Also, each substring is to be paired with 'string - substring' part and vice versa. Thus, gives me , , , , , ] etc....
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
3
by: gh | last post by:
I have a string that is delimited with commas. Is there a function in C# that will break apart the string at each comma, so I can copy the value to a var? This is a .net v1.1.4 app. TIA
8
by: David Lazos | last post by:
Hi All, I use Contains method of String object to determine if a string variable has another string, like that. *************************** ipAddress.Contains("127.0.0")...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.