I am doing something where I realised I wanted to count how many /
s I could find in a string, and then it struck me, that there were several ways to do it, but couldn't decide on what the best (or easiest) was.
At the moment I'm going with something like:
string source = "/once/upon/a/time/";
int count = source.Length - source.Replace("/", "").Length;
But I don't like it at all, any takers?
I don't really want to dig out RegEx
for this, do I?
I know my string is going to have the term I'm searching for, so you can assume that...
Of course for strings where length > 1,
string haystack = "/once/upon/a/time";
string needle = "/";
int needleCount = ( haystack.Length - haystack.Replace(needle,"").Length ) / needle.Length;
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…