2016-03-30 18 views
0

文字列があります(例: "これは例です)。この話では別の言葉 - 「少しのテスト」です。私は文章の中で「何度も何度か試してみたい」ということを何度も知りたい。何か方法はありますか?あなたの「検索用語」は正規表現で使用できる文字が含まれている可能性がある場合は、あなたがRegex.Escapeを使用したい場合があり複数の値の合計を取得する

int sum = Regex.Matches(tmp, "a little test").Count; 

Check it in a fiddle

:ありがとう

string tmp = "This a little test is a little test an example. a little test"; 
int sum = ...... (the count of "a little test") 
+6

[どのようにストライトの出現回数をカウントしますか文字列内にng?](http://stackoverflow.com/questions/541954/how-would-you-count-occurrences-of-a-string-within-a-string) – hellow

+0

@cookiesoft質問には別の意味があるかもしれませんが、実際の質問(および受け入れられた回答)は、OPの質問ではない文字列内の1文字の誤りです。 – Jcl

+0

["質問に" http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-the-titles)、コンセンサスは「いいえ、彼らはすべきではありません」です! –

答えて

6

はあなたのような何かができます:

int sum = Regex.Matches(tmp, Regex.Escape(@"my search term with $pecia| $ymb\0Ls")).Count; 
+1

ブリリアント。 +1。また、(さらに読んで):http://cc.davelozinski.com/c-sharp/c-net-fastest-way-count-substring-occurrences-string –

+0

@JᴀʏMᴇᴇ素晴らしいリンク! – Jcl

関連する問題