String method that counts substrings

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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...
 
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?
 
CodeRazor said:
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
 
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.
 
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,

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;
 
CodeRazor said:
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 said:
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.

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

Mythran
 
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
 
Back
Top