නූලක් තුළ (ඇත්ත වශයෙන්ම වර්‍ගයක්) ඇති වූ සිදුවීම් ගණනය කරන්නේ කෙසේද?


892

මම යමක් කරමි, මට නූලකින් කොපමණ ප්‍රමාණයක් /සොයාගත හැකිදැයි මට වැටහී ඇති අතර, පසුව එය මට පහර දුන්නේය, එය කිරීමට ක්‍රම කිහිපයක් ඇති නමුත් හොඳම (හෝ පහසුම) කුමක්ද යන්න තීරණය කිරීමට නොහැකි විය .

මේ මොහොතේ මම මෙවැනි දෙයක් සමඟ යමි:

string source = "/once/upon/a/time/";
int count = source.Length - source.Replace("/", "").Length;

නමුත් මම එයට කිසිසේත් කැමති නැත, ඕනෑම අයෙක්?

මට ඇත්තටම මේ ගැන හාරා බැලීමට RegExඅවශ්‍ය නැහැ නේද?

මම දන්නවා මගේ නූලට මා සොයන පදය ඇති බව, එබැවින් ඔබට එය උපකල්පනය කළ හැකිය ...

ඇත්තෙන්ම නූල් සඳහා එහිදී දිග> 1 ,

string haystack = "/once/upon/a/time";
string needle = "/";
int needleCount = ( haystack.Length - haystack.Replace(needle,"").Length ) / needle.Length;

36
+1: එය ගණනය කිරීමේ වෙනස් ආකාරයක් බව මම පැවසිය යුතුය. බෙන්ච් මාර්ක් පරීක්ෂණ ප්‍රති results ල ගැන මම පුදුම වෙමි :)
නවීන්

4
එය එතරම් වෙනස් නොවේ ... SQL හි මෙම ක්‍රියාකාරිත්වය ක්‍රියාත්මක කිරීමේ සාමාන්‍ය ක්‍රමය LEN(ColumnToCheck) - LEN(REPLACE(ColumnToCheck,"N",""))එයයි : .
ෂෙරීඩන්

6
ඇත්ත වශයෙන්ම ඔබ බෙදිය යුත්තේ "/".Length
Gerard

3
"/////" තුළ "//" සිදුවීම් ගණන සඳහා ගණනය කළ යුතු යැයි ඔබගේ අවශ්‍යතා මොනවාදැයි මම අසමි. 2 හෝ 4?
ලෙස්

1
රීජෙක්ස් භාවිතා කිරීම බොහෝ විට ඒ සඳහා සුදුසුම ක්‍රමයයි
ඇඩම් හිගින්ස්

Answers:


1037

ඔබ .NET 3.5 භාවිතා කරන්නේ නම් ඔබට මෙය LINQ සමඟ එක් ලයිනර් එකකින් කළ හැකිය:

int count = source.Count(f => f == '/');

ඔබට LINQ භාවිතා කිරීමට අවශ්‍ය නැතිනම් ඔබට මෙය කළ හැකිය:

int count = source.Split('/').Length - 1;

ඔබේ මුල් තාක්‍ෂණය මේ දෙකටම වඩා 30% ක් වේගවත් බව දැන ගැනීම ඔබ පුදුමයට පත් කළ හැකිය! මම "/ වරක් / වරක් / අ / වේලාව /" සමඟ ඉක්මන් මිණුම් ලකුණක් කර ඇති අතර ප්‍රති results ල පහත පරිදි වේ:

ඔබේ මුල් = 12s
source.Count = 19s
source.Split = 17s
foreach ( බොබ්වින්හෝල්ට්ගේ පිළිතුරෙන් ) = 10s

(වේලාවන් 50,000,000 පුනරාවර්තන සඳහා වන බැවින් සැබෑ ලෝකයේ බොහෝ වෙනසක් ඔබ නොදකිනු ඇත.)


6
ඔව්, VS විසින් LINQ දිගු කිරීමේ ක්‍රම නූල් පන්තියේ සඟවයි. මම හිතන්නේ ඔවුන් සිතුවේ devs හට එම සියලු ව්‍යාප්ති ක්‍රම නූල් පන්තියේ පෙන්වීමට අවශ්‍ය නොවන බවයි. බොහෝ විට wise ානවන්ත තීරණයක්.
යූදා ගේබ්‍රියෙල් හිමාන්ගෝ

11
මෙම හැසිරීම විය හැක්කේ VS2010 ස්වයංක්‍රීයව System.Linq නව පන්ති ලිපිගොනු වල ඇතුළත් කර ඇති නිසා VS2008 බොහෝ විට එසේ නොවීමයි. ඉන්ටෙලිසෙන්ස් වැඩ කිරීමට නම් අවකාශය තිබිය යුතුය.
ස්ප්‍රැග්

31
ගණන් සහ බෙදීම් විසඳුම් ක්‍රියාත්මක වන්නේ ඔබ අක්ෂර ගණන් කරන විට පමණක් බව සලකන්න. OP හි විසඳුම මෙන් ඒවා නූල් සමඟ ක්‍රියා නොකරනු ඇත.
පීටර් ලිලෙවෝල්ඩ්

5
f == '\' නොව නූල්, වැලක් දී නූල් දී chars ගැන
තෝමස් වෙලර්

10
මෙය වෙනස් ප්‍රශ්නයකට පිළිතුරක් සේ පෙනේ: "නූලක් තුළ වර්‍ගයක් ඇතිවීම ඔබ ගණන් ගන්නේ කෙසේද?"
බෙන් ආරොන්සන්

188
string source = "/once/upon/a/time/";
int count = 0;
foreach (char c in source) 
  if (c == '/') count++;

තමාට වඩා වේගවත් විය යුතුය source.Replace().


18
පුරෝකථනයක් වෙනුවට for සඳහා මාරුවීමෙන් ඔබට ආන්තික දියුණුවක් ලබා ගත හැකිය, නමුත් ඉතා කුඩා, ඉතා සුළු ප්‍රමාණයක් පමණි.
මාර්ක්

17
ප්‍රශ්නය අසන්නේ අක්‍ෂරයේ නොව නූල් වල සිදුවීම ගණනය කිරීමයි.
යුකිසකුර

4
මෙය නූලක අක්ෂර ගණන් කිරීමකි. මාතෘකාව නූල් එකක නූල් ගණන් කිරීම ගැන ය
තෝමස් වෙල්ලර්

2
Ark මාක් එය ලූපයක් සඳහා පරීක්ෂා කළ අතර එය ඇත්ත වශයෙන්ම foreach භාවිතා කිරීමට වඩා මන්දගාමී විය. සීමාවන් පරීක්ෂා කිරීම නිසා විය හැකිද? (වේලාව තත්පර 1.65 එදිරිව 2.05 මිලි පුනරාවර්තන 5 ක් විය.)
මැනීම

6
ප්‍රශ්නය නූලක් තුළ නූලක් ඉල්ලා සිටින අතර, OP පළ කළ උදාහරණ ගැටළුව ඇත්ත වශයෙන්ම එක් අක්ෂරයක් පමණි, එම අවස්ථාවේ දී මම මෙම පිළිතුර තවමත් වලංගු විසඳුමක් ලෙස හඳුන්වන්නෙමි, මන්ද එය වඩා හොඳ ක්‍රමයක් පෙන්වයි (නූල් සෙවීම වෙනුවට වර්‍ග සෙවීම) ගැටලුව විසඳීමට.
චැඩ්

136
int count = new Regex(Regex.Escape(needle)).Matches(haystack).Count;

8
+1 - සමහර අවස්ථාවලදී ඔබට එකතු කිරීමට අවශ්‍ය විය හැකිය RegexOptions.IgnoreCase.
TrueWill

3
මෙය ඇදහිය නොහැකි තරම් අඩු නොවේ ද?
තෝමස් අයුබ්

4
Regex overhead පරිපූර්ණ නොවේ, තවද "මට මේ සඳහා RegEx හෑරීමට අවශ්‍ය නැත, එසේ ද?"
චැඩ්

අවශ්ය නො විය හැකි Regex.Escape(...)නිසාnew System.Text.RegularExpressions.Regex(needle).Matches(haystack).Count;
barlop

3
මම මේ සමඟ ගියේ එයට අක්ෂර පමණක් නොව නූල් සෙවිය හැකි බැවිනි.
ජේම්ස් ඉන්ඩි

86

ඔබට අක්ෂර පමණක් නොව සම්පූර්ණ නූල් සෙවීමට හැකි නම්:

src.Select((c, i) => src.Substring(i))
    .Count(sub => sub.StartsWith(target))

"නූලෙහි එක් එක් අක්ෂර සඳහා, එම අක්ෂරයෙන් ආරම්භ වන ඉතිරි නූල උපස්ථරයක් ලෙස ගන්න; එය ඉලක්කගත නූලෙන් ආරම්භ වන්නේ නම් එය ගණන් කරන්න."


1
ලබා දී ඇති විස්තරයට වඩා පැහැදිලි ආකාරයකින් එය පැහැදිලි කරන්නේ කෙසේදැයි මට විශ්වාස නැත. අවුල් සහගත වන්නේ කුමක්ද?
mqp

58
සුපර් ස්ලෝව්! Html පිටුවක එය උත්සාහ කළ අතර තත්පර 2 ක් ගත වූ මෙම පිටුවේ වෙනත් ක්‍රමවලට සාපේක්ෂව මිනිත්තු 2 ක් පමණ ගත විය. පිළිතුර නිවැරදි ය; එය භාවිතයට ගත නොහැකි තරම් මන්දගාමී විය.
JohnB

2
එකඟයි, ඉතා මන්දගාමීයි. මම ලින්ක් විලාසිතාවේ විසඳුම්වල විශාල රසිකයෙක් නමුත් මෙය ශක්‍ය නොවේ.
Sprague

5
මෙය ඉතා මන්දගාමී වීමට හේතුව එය නූල් n නිර්මාණය කිරීම නිසා දළ වශයෙන් n ^ 2/2 බයිට් වෙන් කිරීමයි.
පීටර් ක්‍රබ්ට්‍රි

6
මගේ 210000 අක්ෂර සඳහා OutOfMemoryException විසි කරනු ලැබේ.
එන්ඩර්

66

මම පර්යේෂණ කිහිපයක් කර ඇති අතර බොහෝ අවස්ථාවල රිචඩ් වොට්සන්ගේ විසඳුම වේගවත් බව සොයාගෙන ඇත. තනතුරේ සෑම විසඳුමකම ප්‍රති results ල සහිත වගුව එයයි ( රෙජෙක්ස් භාවිතා කරන ඒවා හැර, එය "ටෙස්ට් {ටෙස්ට්" වැනි නූල් විග්‍රහ කිරීමේදී ව්‍යතිරේකයන් විසි කරන නිසා)

    Name      | Short/char |  Long/char | Short/short| Long/short |  Long/long |
    Inspite   |         134|        1853|          95|        1146|         671|
    LukeH_1   |         346|        4490|         N/A|         N/A|         N/A|
    LukeH_2   |         152|        1569|         197|        2425|        2171|
Bobwienholt   |         230|        3269|         N/A|         N/A|         N/A|
Richard Watson|          33|         298|         146|         737|         543|
StefanosKargas|         N/A|         N/A|         681|       11884|       12486|

කෙටි උපස්ථරවල (අක්ෂර 1-5) කෙටි නූලකින් (අක්ෂර 10-50) සිදුවීම් සංඛ්‍යාවක් සොයා ගැනීමේදී මුල් ඇල්ගොරිතම වඩාත් සුදුසු බව ඔබට පෙනේ.

බහුකාර්ය උපස්ථර සඳහා ඔබ පහත කේතය භාවිතා කළ යුතුය ( රිචඩ් වොට්සන්ගේ විසඳුම මත පදනම්ව )

int count = 0, n = 0;

if(substring != "")
{
    while ((n = source.IndexOf(substring, n, StringComparison.InvariantCulture)) != -1)
    {
        n += substring.Length;
        ++count;
    }
}

මම මගේම 'පහත් මට්ටමේ' විසඳුමක් එක් කිරීමට සූදානම්ව සිටියෙමි (උපස්ථර නිර්මාණය නොකර, ප්‍රතිස්ථාපනය / බෙදීම හෝ ඕනෑම රීජෙක්ස් / ලින්ක් භාවිතා නොකර), නමුත් ඔබේ ඒවා මගේ (හා අවම වශයෙන් කෙටි) වඩා හොඳය. ස්තූතියි!
ඩෑන් ඩබ්ලිව්

රීජෙක්ස් විසඳුම් සඳහා,Regex.Escape(needle)
තයිමින්

2
අනෙක් අය සඳහා පෙන්වා දීමට, සෙවුම් අගය හිස් දැයි පරීක්ෂා කළ යුතුය, එසේ නොමැතිනම් ඔබ අසීමිත පුඩුවක් තුළට පිවිසෙනු ඇත.
WhoIsRich

2
සමහර විට එය මට, නමුත් සඳහා ය source="aaa" substring="aa"මම ආපසු 2 ලබා ගැනීමට අපේක්ෂා, මේ, වෙනස් නොවන 1. "විසඳුමක්" සඳහා n += substring.Lengthකිරීමටn++
ytoledano

overlappedඔබේ නඩුව සපුරාලීම සඳහා ඔබට ධජය එක් කළ හැකිය :overlapped=True;.... if(overlapped) {++n;} else {n += substring.Length;}
tsionyx

56

LINQ සියළුම එකතු කිරීම් වල ක්‍රියා කරන අතර, නූල් යනු අක්ෂර එකතුවක් පමණක් බැවින්, මෙම ලස්සන කුඩා ලයිනර් ගැන කෙසේද:

var count = source.Count(c => c == '/');

එම නාම අවකාශයේ සිට දිගු කිරීමේ ක්‍රමයක් using System.Linq;මෙන් ඔබේ කේත ගොනුවේ ඉහළින්ම ඇති බවට වග බලා ගන්න .Count.


5
එහි var භාවිතා කිරීම ඇත්තෙන්ම වටී ද? ගණනය කිරීමක් ප්‍රතිස්ථාපනය නොකරන යමක් ආදේශ කිරීමට අවස්ථාවක් තිබේද?
Whatsit

71
Hat වොට්සිට්: ඔබේ වම් අතෙන් 'වර්' ටයිප් කළ හැකි අතර 'ඉන්ට' අත් දෙකම අවශ්‍ය වේ;)
ෂෝන් බ්‍රයිට්

7
intඅකුරු සියල්ලම නිවසේ යතුරු වල රැඳී ඇති අතර varඑසේ නොවේ. ඉන්න .. ඉන්න, මම ඩ්වොරක් පාවිච්චි කරනවා
මයිකල් බුවෙන්

2
DBDotA ඔබට 'System.Linq භාවිතා කිරීම' ඇති බවට වග බලා ගන්න. ඔබගේ ගොනුවේ ඉහළින්. එසේම, ඉන්ටෙලිසෙන්ස් විසින් .සම ඇමතුම සැඟවිය හැක. එසේ වුවද, එය සම්පාදනය කර හොඳින් ක්‍රියාත්මක වේ.
යූදා ගේබ්‍රියෙල් හිමාන්ගෝ

3
@JudahGabrielHimango මම var භාවිතා කළ යුතුය තර්ක කරන්නට පිළිවන්, විශේෂයෙන් විචල්ය වර්ගය පෙනෙන අතර, (සහ brevity අනුකූලතාවක් සඳහා) වන විට
EriF89

51
string source = "/once/upon/a/time/";
int count = 0;
int n = 0;

while ((n = source.IndexOf('/', n)) != -1)
{
   n++;
   count++;
}

මගේ පරිගණකයේ එය මිලියන 50 ක පුනරාවර්තන සඳහා සෑම අක්‍ෂරයකම විසඳුමට වඩා තත්පර 2 ක් පමණ වේගවත් වේ.

2013 සංශෝධනය:

නූල වර්‍ගයකට වෙනස් කර එය හරහා නැවත කරන්න. මීටර් 50 පුනරාවර්තන සඳහා මුළු වේලාවෙන් තවත් තත්පරයක් හෝ දෙකක් කපා දමයි!

char[] testchars = source.ToCharArray();
foreach (char c in testchars)
{
     if (c == '/')
         count++;
}

මෙය තවමත් ඉක්මන් ය:

char[] testchars = source.ToCharArray();
int length = testchars.Length;
for (int n = 0; n < length; n++)
{
    if (testchars[n] == '/')
        count++;
}

හොඳ මිනුමක් සඳහා, අරාවෙහි කෙළවරේ සිට 0 දක්වා පුනරාවර්තනය වේගවත්ම බව පෙනේ, 5% කින්.

int length = testchars.Length;
for (int n = length-1; n >= 0; n--)
{
    if (testchars[n] == '/')
        count++;
}

මෙය විය හැක්කේ මන්දැයි මම කල්පනා කරමින් සිටියෙමි (ගූග්ලිං) (ආපසු හැරවීම ඉක්මන් වීම ගැන මට යමක් සිහිපත් වේ), මෙම SO ප්‍රශ්නයට පිළිතුරු සපයන අතර එය දැනටමත් කරදරකාරී ලෙස නූල් වර්‍ග []] තාක්‍ෂණය භාවිතා කරයි. මෙම සන්දර්භය තුළ ආපසු හැරවීමේ උපක්‍රමය අළුත් යැයි මම සිතමි.

C # හි ඇති නූලක තනි අක්ෂර හරහා නැවත යෙදීමට වේගවත්ම ක්‍රමය කුමක්ද?


1
ඔබට වර්‍ගයේ වරහන් දමා source.IndexOf('/', n + 1)නැති කර ගත හැකිය n++:) එසේම, string word = "/"චරිතය වෙනුවට විචල්‍යයක් දමන්න .
neeKo

1
හේයි නිකෝ, නව පිළිතුරු පරීක්ෂා කරන්න. විචල්ය-දිග උපස්ථරයක් සෑදීම දුෂ්කර වුවද.
රිචඩ් වොට්සන්

මම උපසිරැසි හරහා පියවරෙන් සමාන දෙයක් භාවිතා කළෙමි; ඒ තමයි මම තේරුම් ගන්නා තුරු දර්ශකයට ආරම්භක දර්ශකයක් ඇති බව. පළමු විසඳුමට මම වඩාත්ම කැමතියි එය වේගය සහ මතක අඩිපාර අතර හොඳ සමබරතාවයක් ඇති නිසා.
සමීර් බන්ජනොවික්

1
අගය 0 ට සංසන්දනය කිරීම වේගවත් බැවින් එය පසුපසට නැවත වේගවත් වන බව මම කොතැනක හෝ කියවා
ඇත්තෙමි

1
it ෂිට්පොයිට් යූප්. ඔබ යටින් පවතින කේතය දෙස බැලුවහොත් එය ස්වදේශීය ඇමතුමකි. public char [] toCharArray () System ... System.arraycopy (අගය, 0, ප්‍රති result ලය, 0, අගය. දිග); ...}
රිචඩ් වොට්සන්

46

මේ දෙකම ක්‍රියාත්මක වන්නේ තනි අක්ෂර සෙවුම් පද සඳහා පමණි ...

countOccurences("the", "the answer is the answer");

int countOccurences(string needle, string haystack)
{
    return (haystack.Length - haystack.Replace(needle,"").Length) / needle.Length;
}

දිගු ඉඳිකටු සඳහා වඩා හොඳ විය හැකිය ...

නමුත් ඊට වඩා අලංකාර ක්‍රමයක් තිබිය යුතුය. :)


බහු අක්ෂර ආදේශන සඳහා ගණනය කිරීම සඳහා. එය නොමැතිව, පරීක්ෂණයෙහි "යතුර" ලෙස ගණන් කිරීම 6
යවනු ඇත

මිණුම් සලකුණු කර මෙය නූල් සමඟ සංසන්දනය කර ඇත. බෙදීම් මාර්ගය - 1.5 ගුණයක් වේගයෙන් ක්‍රියා කරයි. කුඩෝස්.
ඇලෙක්ස්

20

සංස්කරණය කරන්න:

source.Split('/').Length-1

2
මේක තමයි මම කරන්නේ. සහ source.Split(new[]{"//"}, StringSplitOptions.None).Count - 1බහු අක්ෂර බෙදුම්කරුවන් සඳහා.
bzlm

4
මෙය ගොඩවල් මත අවම වශයෙන් නූල් වෙන් කිරීම් සිදු කරනු ඇත, ඊට අමතරව (සමහර විට) අරා ප්‍රති-ප්‍රමාණ කිහිපයක් - සහ මේ සියල්ල ගණනය කිරීම සඳහාද? අතිශයින්ම අකාර්යක්ෂම, හොඳින් පරිමාණය නොකරන අතර කිසි විටෙක වැදගත් කේතයක භාවිතා නොකළ යුතුය.
සර් ෂර්දාන්

17

C # හි, අනපේක්ෂිත ලෙස උපක්‍රමශීලී සහෝදරයා වන්නේ හොඳ සංගීත උප-කවුන්ටරයකි:

public static int CCount(String haystack, String needle)
{
    return haystack.Split(new[] { needle }, StringSplitOptions.None).Length - 1;
}

1
හොඳ විසඳුමක් - සහ නූල් සඳහා වැඩ කිරීම (වරහන් පමණක් නොවේ)!
ChriPf

ස්තූතියි, භාෂා මාරු කිරීමේදී නූල් හැසිරවීමේ සියුම් කරුණු කිහිපයක් අමතක කිරීම පහසු නැත - අප බොහෝ දෙනාට මේ දිනවල ඇති පරිදි!
ඩේව්

1
-1 මොකද: ගණන් () සහ ගණන් හෝ දිග අතර වෙනස ඔබ දන්නවාද? යමෙක් ගණන් කිරීම හෝ දිග වෙනුවට ගණන් () භාවිතා කරන්නේ නම් මම අවුලුවන. ගණන් කිරීම () විසින් IEnumerator නිර්මාණය කරන අතර පසුව IEnumerable හි සියලු සිදුවීම් හරහා ගමන් කරන අතර ගණන් හෝ දිග දැනටමත් වස්තුවෙහි ගුණාංග සකසා ඇති අතර එමඟින් සියලු මූලද්‍රව්‍යයන් නැවත සැකසීමේ අවශ්‍යතාවයකින් තොරව ඔබට අවශ්‍ය ගණනය කර ඇත.
aeroson

හොඳ තැනක්, සහ අමුතුම දෙය නම් මගේ පුස්තකාලයේ, මම ශ්‍රිතය ගත් ස්ථානයේ සිට, මම "දිග" භාවිතා කිරීමයි. සංස්කරණය කරන ලදි!
ඩේව්

15
Regex.Matches(input,  Regex.Escape("stringToMatch")).Count

1
ආදාන බහාලුම් රීජෙක්ස් විශේෂ අක්ෂර නම් මෙය නිවැරදි නොවේ Regex.Escape (ආදානය) තිබිය යුතුය
Esben Skov Pedersen

1
ඇත්ත වශයෙන්ම stringToMatchඅවශ්‍යතා ගැලවී යාම මිස input.
තියඩෝර් සූලියස්

හරි ඔයා. එය සවි කර ඇත.
cederlof

13
private int CountWords(string text, string word) {
    int count = (text.Length - text.Replace(word, "").Length) / word.Length;
    return count;
}

මුල් විසඳුම අක්ෂර සඳහා වේගවත්ම නිසා, එය නූල් සඳහා ද වනු ඇතැයි මම සිතමි. ඉතින් මෙන්න මගේ දායකත්වය.

සන්දර්භය සඳහා: මම ලොග් ගොනුවක 'අසමත්' සහ 'සාර්ථකයි' වැනි වචන සොයමින් සිටියෙමි.

Gr, බෙන්


2
"වචන" විචල්‍යය සඳහා හිස් නූලක් නොයන්න (ශුන්‍ය දෝෂයෙන් බෙදීම).
ජෙන්ස්

12
string s = "65 fght 6565 4665 hjk";
int count = 0;
foreach (Match m in Regex.Matches(s, "65"))
  count++;

20
හෝ Regex.Matches (s, "65") ගණන් කරන්න ^ _ ^
මෙටා

සෑම නූලකටම නොවේ. "Abc ++ def ++ xyz" හි "++" සෙවීමට උත්සාහ කරන්න
marsh-wiggle

7

සංගීත දිගු කිරීමේ ක්‍රමය භාවිතා කිරීමට සූදානම් ඕනෑම කෙනෙකුට,

පළ කරන ලද හොඳම පිළිතුරු මත පදනම් වූ මා භාවිතා කරන්නේ මෙන්න:

public static class StringExtension
{    
    /// <summary> Returns the number of occurences of a string within a string, optional comparison allows case and culture control. </summary>
    public static int Occurrences(this System.String input, string value, StringComparison stringComparisonType = StringComparison.Ordinal)
    {
        if (String.IsNullOrEmpty(value)) return 0;

        int count    = 0;
        int position = 0;

        while ((position = input.IndexOf(value, position, stringComparisonType)) != -1)
        {
            position += value.Length;
            count    += 1;
        }

        return count;
    }

    /// <summary> Returns the number of occurences of a single character within a string. </summary>
    public static int Occurrences(this System.String input, char value)
    {
        int count = 0;
        foreach (char c in input) if (c == value) count += 1;
        return count;
    }
}

සම්මත කරන ලද නූල ශුන්‍ය හෝ හිස් නම් දෙවන ක්‍රමය උත්පාත වන්නේ නැද්ද? ශෛලීය දෘෂ්ටි කෝණයකින් බැලූ විට, ඔබ ආදානය හුදෙක් සංගීතයට වඩා System.String ලෙස අර්ථ දක්වන්නේ කුමක් ද?
Nodoid

7
public static int GetNumSubstringOccurrences(string text, string search)
{
    int num = 0;
    int pos = 0;

    if (!string.IsNullOrEmpty(text) && !string.IsNullOrEmpty(search))
    {
        while ((pos = text.IndexOf(search, pos)) > -1)
        {
            num ++;
            pos += search.Length;
        }
    }
    return num;
}

5

මම හිතන්නේ මෙය කිරීමට ඇති පහසුම ක්‍රමය නිත්‍ය ප්‍රකාශන භාවිතා කිරීමයි. මේ ආකාරයෙන් ඔබට myVar.Split ('x') භාවිතා කළ හැකි තරම් බෙදීම් සංඛ්‍යාවක් ලබා ගත හැකි නමුත් බහු අක්ෂර සැකසුමකින්.

string myVar = "do this to count the number of words in my wording so that I can word it up!";
int count = Regex.Split(myVar, "word").Length;

3
string search = "/string";
var occurrences = (regex.Match(search, @"\/")).Count;

වැඩසටහන මඟින් "/ s" හරියටම සොයා ගන්නා සෑම අවස්ථාවකම මෙය ගණනය කරනු ලැබේ (සිද්ධි සංවේදී) සහ මෙම සිදුවීම් ගණන විචල්ය "සිදුවීම්" තුළ ගබඩා වේ.


3

අනාරක්ෂිත බයිට්-බයි-බයිට් සැසඳීම් වැනි අපට යම් ආකාරයක උප නූල් ගණනය කිරීම් නොමැති බව මට හැඟුණි. මුල් පෝස්ටරයේ ක්‍රමය සහ මට සිතිය හැකි ඕනෑම ක්‍රමයක් මම එකට තැබුවෙමි.

මේවා මා විසින් සාදන ලද දිගු දිගු වේ.

namespace Example
{
    using System;
    using System.Text;

    public static class StringExtensions
    {
        public static int CountSubstr(this string str, string substr)
        {
            return (str.Length - str.Replace(substr, "").Length) / substr.Length;
        }

        public static int CountSubstr(this string str, char substr)
        {
            return (str.Length - str.Replace(substr.ToString(), "").Length);
        }

        public static int CountSubstr2(this string str, string substr)
        {
            int substrlen = substr.Length;
            int lastIndex = str.IndexOf(substr, 0, StringComparison.Ordinal);
            int count = 0;
            while (lastIndex != -1)
            {
                ++count;
                lastIndex = str.IndexOf(substr, lastIndex + substrlen, StringComparison.Ordinal);
            }

            return count;
        }

        public static int CountSubstr2(this string str, char substr)
        {
            int lastIndex = str.IndexOf(substr, 0);
            int count = 0;
            while (lastIndex != -1)
            {
                ++count;
                lastIndex = str.IndexOf(substr, lastIndex + 1);
            }

            return count;
        }

        public static int CountChar(this string str, char substr)
        {
            int length = str.Length;
            int count = 0;
            for (int i = 0; i < length; ++i)
                if (str[i] == substr)
                    ++count;

            return count;
        }

        public static int CountChar2(this string str, char substr)
        {
            int count = 0;
            foreach (var c in str)
                if (c == substr)
                    ++count;

            return count;
        }

        public static unsafe int CountChar3(this string str, char substr)
        {
            int length = str.Length;
            int count = 0;
            fixed (char* chars = str)
            {
                for (int i = 0; i < length; ++i)
                    if (*(chars + i) == substr)
                        ++count;
            }

            return count;
        }

        public static unsafe int CountChar4(this string str, char substr)
        {
            int length = str.Length;
            int count = 0;
            fixed (char* chars = str)
            {
                for (int i = length - 1; i >= 0; --i)
                    if (*(chars + i) == substr)
                        ++count;
            }

            return count;
        }

        public static unsafe int CountSubstr3(this string str, string substr)
        {
            int length = str.Length;
            int substrlen = substr.Length;
            int count = 0;
            fixed (char* strc = str)
            {
                fixed (char* substrc = substr)
                {
                    int n = 0;

                    for (int i = 0; i < length; ++i)
                    {
                        if (*(strc + i) == *(substrc + n))
                        {
                            ++n;
                            if (n == substrlen)
                            {
                                ++count;
                                n = 0;
                            }
                        }
                        else
                            n = 0;
                    }
                }
            }

            return count;
        }

        public static int CountSubstr3(this string str, char substr)
        {
            return CountSubstr3(str, substr.ToString());
        }

        public static unsafe int CountSubstr4(this string str, string substr)
        {
            int length = str.Length;
            int substrLastIndex = substr.Length - 1;
            int count = 0;
            fixed (char* strc = str)
            {
                fixed (char* substrc = substr)
                {
                    int n = substrLastIndex;

                    for (int i = length - 1; i >= 0; --i)
                    {
                        if (*(strc + i) == *(substrc + n))
                        {
                            if (--n == -1)
                            {
                                ++count;
                                n = substrLastIndex;
                            }
                        }
                        else
                            n = substrLastIndex;
                    }
                }
            }

            return count;
        }

        public static int CountSubstr4(this string str, char substr)
        {
            return CountSubstr4(str, substr.ToString());
        }
    }
}

පරීක්ෂණ කේතය අනුගමනය කිරීමෙන් ...

static void Main()
{
    const char matchA = '_';
    const string matchB = "and";
    const string matchC = "muchlongerword";
    const string testStrA = "_and_d_e_banna_i_o___pfasd__and_d_e_banna_i_o___pfasd_";
    const string testStrB = "and sdf and ans andeians andano ip and and sdf and ans andeians andano ip and";
    const string testStrC =
        "muchlongerword amuchlongerworsdfmuchlongerwordsdf jmuchlongerworijv muchlongerword sdmuchlongerword dsmuchlongerword";
    const int testSize = 1000000;
    Console.WriteLine(testStrA.CountSubstr('_'));
    Console.WriteLine(testStrA.CountSubstr2('_'));
    Console.WriteLine(testStrA.CountSubstr3('_'));
    Console.WriteLine(testStrA.CountSubstr4('_'));
    Console.WriteLine(testStrA.CountChar('_'));
    Console.WriteLine(testStrA.CountChar2('_'));
    Console.WriteLine(testStrA.CountChar3('_'));
    Console.WriteLine(testStrA.CountChar4('_'));
    Console.WriteLine(testStrB.CountSubstr("and"));
    Console.WriteLine(testStrB.CountSubstr2("and"));
    Console.WriteLine(testStrB.CountSubstr3("and"));
    Console.WriteLine(testStrB.CountSubstr4("and"));
    Console.WriteLine(testStrC.CountSubstr("muchlongerword"));
    Console.WriteLine(testStrC.CountSubstr2("muchlongerword"));
    Console.WriteLine(testStrC.CountSubstr3("muchlongerword"));
    Console.WriteLine(testStrC.CountSubstr4("muchlongerword"));
    var timer = new Stopwatch();
    timer.Start();
    for (int i = 0; i < testSize; ++i)
        testStrA.CountSubstr(matchA);
    timer.Stop();
    Console.WriteLine("CS1 chr: " + timer.Elapsed.TotalMilliseconds + "ms");

    timer.Restart();
    for (int i = 0; i < testSize; ++i)
        testStrB.CountSubstr(matchB);
    timer.Stop();
    Console.WriteLine("CS1 and: " + timer.Elapsed.TotalMilliseconds + "ms");

    timer.Restart();
    for (int i = 0; i < testSize; ++i)
        testStrC.CountSubstr(matchC);
    timer.Stop();
    Console.WriteLine("CS1 mlw: " + timer.Elapsed.TotalMilliseconds + "ms");

    timer.Restart();
    for (int i = 0; i < testSize; ++i)
        testStrA.CountSubstr2(matchA);
    timer.Stop();
    Console.WriteLine("CS2 chr: " + timer.Elapsed.TotalMilliseconds + "ms");

    timer.Restart();
    for (int i = 0; i < testSize; ++i)
        testStrB.CountSubstr2(matchB);
    timer.Stop();
    Console.WriteLine("CS2 and: " + timer.Elapsed.TotalMilliseconds + "ms");

    timer.Restart();
    for (int i = 0; i < testSize; ++i)
        testStrC.CountSubstr2(matchC);
    timer.Stop();
    Console.WriteLine("CS2 mlw: " + timer.Elapsed.TotalMilliseconds + "ms");

    timer.Restart();
    for (int i = 0; i < testSize; ++i)
        testStrA.CountSubstr3(matchA);
    timer.Stop();
    Console.WriteLine("CS3 chr: " + timer.Elapsed.TotalMilliseconds + "ms");

    timer.Restart();
    for (int i = 0; i < testSize; ++i)
        testStrB.CountSubstr3(matchB);
    timer.Stop();
    Console.WriteLine("CS3 and: " + timer.Elapsed.TotalMilliseconds + "ms");

    timer.Restart();
    for (int i = 0; i < testSize; ++i)
        testStrC.CountSubstr3(matchC);
    timer.Stop();
    Console.WriteLine("CS3 mlw: " + timer.Elapsed.TotalMilliseconds + "ms");

    timer.Restart();
    for (int i = 0; i < testSize; ++i)
        testStrA.CountSubstr4(matchA);
    timer.Stop();
    Console.WriteLine("CS4 chr: " + timer.Elapsed.TotalMilliseconds + "ms");

    timer.Restart();
    for (int i = 0; i < testSize; ++i)
        testStrB.CountSubstr4(matchB);
    timer.Stop();
    Console.WriteLine("CS4 and: " + timer.Elapsed.TotalMilliseconds + "ms");

    timer.Restart();
    for (int i = 0; i < testSize; ++i)
        testStrC.CountSubstr4(matchC);
    timer.Stop();
    Console.WriteLine("CS4 mlw: " + timer.Elapsed.TotalMilliseconds + "ms");

    timer.Restart();
    for (int i = 0; i < testSize; ++i)
        testStrA.CountChar(matchA);
    timer.Stop();
    Console.WriteLine("CC1 chr: " + timer.Elapsed.TotalMilliseconds + "ms");

    timer.Restart();
    for (int i = 0; i < testSize; ++i)
        testStrA.CountChar2(matchA);
    timer.Stop();
    Console.WriteLine("CC2 chr: " + timer.Elapsed.TotalMilliseconds + "ms");

    timer.Restart();
    for (int i = 0; i < testSize; ++i)
        testStrA.CountChar3(matchA);
    timer.Stop();
    Console.WriteLine("CC3 chr: " + timer.Elapsed.TotalMilliseconds + "ms");

    timer.Restart();
    for (int i = 0; i < testSize; ++i)
        testStrA.CountChar4(matchA);
    timer.Stop();
    Console.WriteLine("CC4 chr: " + timer.Elapsed.TotalMilliseconds + "ms");
}

ප්‍රති Results ල: CSX CountSubstrX හා CCX CountCharX සමඟ අනුරූප වේ. "chr" '_', "සහ" සහ "සඳහා නූලක් සොයයි, සහ" mlw "" බොහෝ දිග වචන "සඳහා නූලක් සොයයි.

CS1 chr: 824.123ms
CS1 and: 586.1893ms
CS1 mlw: 486.5414ms
CS2 chr: 127.8941ms
CS2 and: 806.3918ms
CS2 mlw: 497.318ms
CS3 chr: 201.8896ms
CS3 and: 124.0675ms
CS3 mlw: 212.8341ms
CS4 chr: 81.5183ms
CS4 and: 92.0615ms
CS4 mlw: 116.2197ms
CC1 chr: 66.4078ms
CC2 chr: 64.0161ms
CC3 chr: 65.9013ms
CC4 chr: 65.8206ms

අවසාන වශයෙන්, මට අක්ෂර මිලියන 3.6 ක් සහිත ගොනුවක් තිබුණි. එය "derp adfderdserp dfaerpderp deasderp" 100,000 වතාවක් පුනරාවර්තනය විය. මෙම ප්‍රති .ල මෙන් 100 ගුණයක් ඉහත ක්‍රම සමඟ මම ගොනුව තුළ “ඩර්ප්” සෙව්වෙමි.

CS1Derp: 1501.3444ms
CS2Derp: 1585.797ms
CS3Derp: 376.0937ms
CS4Derp: 271.1663ms

ඉතින් මගේ 4 වන ක්‍රමය අනිවාර්යයෙන්ම ජයග්‍රාහකයා වේ, නමුත් යථාර්ථවාදීව ගත් කල, මිලියන 3.6 ක ලිපිගොනු 100 වතාවක් පමණක් නරක අතට 1586 ක් ගත කළේ නම්, මේ සියල්ල නොසැලකිලිමත් ය.

මාර්ගය වන විට, මම මිලියන 3.6 ක අක්ෂර ගොනුවේ 100 වතාවක් CountSubstr සහ CountChar ක්‍රම සමඟ 'd' වර්‍ගය පරිලෝකනය කළෙමි. ප්රතිපල...

CS1  d : 2606.9513ms
CS2  d : 339.7942ms
CS3  d : 960.281ms
CS4  d : 233.3442ms
CC1  d : 302.4122ms
CC2  d : 280.7719ms
CC3  d : 299.1125ms
CC4  d : 292.9365ms

මේ අනුව විශාල පිදුරු මඩුවක තනි අක්ෂර ඉඳිකටු සඳහා මුල් පෝස්ටර් ක්‍රමය ඉතා නරක ය.

සටහන: සියලුම අගයන් මුදාහැරීමේ අනුවාද ප්‍රතිදානය වෙත යාවත්කාලීන කරන ලදි. මම මෙය පළ කළ පළමු වරට මුදා හැරීමේ මාදිලිය මත ගොඩ නැගීමට අහම්බෙන් අමතක විය. මගේ සමහර ප්‍රකාශ සංශෝධනය කර ඇත.


කාර්ය සාධන ප්‍රති .ල වලට ස්තූතියි. 10 වේගයෙහි සාධක වෙනස ලින්ක් හෝ වෙනත් පිළිවෙලට ලිඛිත විසඳුමක් සලකා බැලීමට හේතුවක් නොවිය හැකි නමුත් දිගු කිරීමේ ක්‍රමයක් සමඟ යන්න.
ඇන්ඩ්‍රියාස් රීෆ්

2

නූල් ඇතිවීම සඳහා සාමාන්‍ය ශ්‍රිතයක්:

public int getNumberOfOccurencies(String inputString, String checkString)
{
    if (checkString.Length > inputString.Length || checkString.Equals("")) { return 0; }
    int lengthDifference = inputString.Length - checkString.Length;
    int occurencies = 0;
    for (int i = 0; i < lengthDifference; i++) {
        if (inputString.Substring(i, checkString.Length).Equals(checkString)) { occurencies++; i += checkString.Length - 1; } }
    return occurencies;
}

2
මෙය විශාල තාවකාලික නූල් සංඛ්‍යාවක් නිර්මාණය කරන අතර කසළ එකතු කරන්නා ඉතා වෙහෙස මහන්සි වී වැඩ කරයි.
එරික්ලෝ

2
string source = "/once/upon/a/time/";
int count = 0, n = 0;
while ((n = source.IndexOf('/', n) + 1) != 0) count++;

රිචඩ් වොට්සන්ගේ පිළිතුරේ වෙනසක්, කාර්යක්ෂමතාව වැඩි කිරීමත් සමඟ තරමක් වේගවත් වන අතර, නූලෙහි වර්‍ගය සිදුවන වාර ගණන වැඩි වන අතර අඩු කේතයක්!

සෑම සිද්ධියක්ම පුළුල් ලෙස පරීක්‍ෂා නොකර මම පැවසිය යුතු වුවත්, භාවිතා කිරීමෙන් ඉතා සැලකිය යුතු වේග වැඩි දියුණු කිරීමක් මම දුටුවෙමි.

int count = 0;
for (int n = 0; n < source.Length; n++) if (source[n] == '/') count++;

2
            var conditionalStatement = conditionSetting.Value;

            //order of replace matters, remove == before =, incase of ===
            conditionalStatement = conditionalStatement.Replace("==", "~").Replace("!=", "~").Replace('=', '~').Replace('!', '~').Replace('>', '~').Replace('<', '~').Replace(">=", "~").Replace("<=", "~");

            var listOfValidConditions = new List<string>() { "!=", "==", ">", "<", ">=", "<=" };

            if (conditionalStatement.Count(x => x == '~') != 1)
            {
                result.InvalidFieldList.Add(new KeyFieldData(batch.DECurrentField, "The IsDoubleKeyCondition does not contain a supported conditional statement. Contact System Administrator."));
                result.Status = ValidatorStatus.Fail;
                return result;
            }

නූලකින් කොන්දේසි සහිත ප්‍රකාශ පරීක්ෂා කිරීමට සමාන දෙයක් කිරීමට අවශ්‍යය.

මා සොයන දේ තනි අක්‍ෂරයකින් ප්‍රතිස්ථාපනය කර තනි චරිතයේ අවස්ථා ගණනය කළේය.

නිසැකවම ඔබ භාවිතා කරන තනි අක්‍ෂරය වැරදි ගණනය කිරීම් වලක්වා ගැනීමට පෙර මෙය සිදුවීමට පෙර නූලෙහි නොපවතිනදැයි පරීක්ෂා කළ යුතුය.


2

නූල් වලින් නූල්:

".." "" ජේඩී ජේඩී ජේඩී ජේඩී යනාදිය සොයා ගන්න. ආදිය.

var strOrigin = " .. JD JD JD JD etc. and etc. JDJDJDJDJDJDJDJD and etc.";
var searchStr = "etc";
int count = (strOrigin.Length - strOrigin.Replace(searchStr, "").Length)/searchStr.Length.

මෙය අසංවර / අවුල් සහගත ලෙස බැහැර කිරීමට පෙර කාර්ය සාධනය පරීක්ෂා කරන්න ...


2

මගේ ආරම්භක පියවර මට මෙවැනි දෙයක් ලබා දුන්නේය:

public static int CountOccurrences(string original, string substring)
{
    if (string.IsNullOrEmpty(substring))
        return 0;
    if (substring.Length == 1)
        return CountOccurrences(original, substring[0]);
    if (string.IsNullOrEmpty(original) ||
        substring.Length > original.Length)
        return 0;
    int substringCount = 0;
    for (int charIndex = 0; charIndex < original.Length; charIndex++)
    {
        for (int subCharIndex = 0, secondaryCharIndex = charIndex; subCharIndex < substring.Length && secondaryCharIndex < original.Length; subCharIndex++, secondaryCharIndex++)
        {
            if (substring[subCharIndex] != original[secondaryCharIndex])
                goto continueOuter;
        }
        if (charIndex + substring.Length > original.Length)
            break;
        charIndex += substring.Length - 1;
        substringCount++;
    continueOuter:
        ;
    }
    return substringCount;
}

public static int CountOccurrences(string original, char @char)
{
    if (string.IsNullOrEmpty(original))
        return 0;
    int substringCount = 0;
    for (int charIndex = 0; charIndex < original.Length; charIndex++)
        if (@char == original[charIndex])
            substringCount++;
    return substringCount;
}

ප්‍රතිස්ථාපනය හා බෙදීම භාවිතා කරමින් පිදුරු මඩුවක ඉඳිකටුවක් තත්පර 21+ ක් ලබා දෙන අතර මේ සඳහා 15.2 ක් ගතවේ.

substring.Length - 1CharIndex වෙත එකතු වන ටිකක් එකතු කිරීමෙන් පසුව සංස්කරණය කරන්න (එය කළ යුතු ආකාරයට), එය තත්පර 11.6 කි.

2 වන සංස්කරණය: මම අක්ෂර දෙකකින් යුත් නූල් 26 ක් භාවිතා කළ නූලක් භාවිතා කළෙමි, එකම නියැදි පෙළට යාවත්කාලීන කළ වේලාවන් මෙන්න:

පිදුරු මඩුවක ඉඳිකටුවක් (OP හි අනුවාදය): තත්පර 7.8

යෝජිත යාන්ත්‍රණය: තත්පර 4.6.

3 වන සංස්කරණය: තනි අක්ෂර කෝනර් කේස් එකක් එකතු කිරීමෙන් එය තත්පර 1.2 ක් විය.

4 වන සංස්කරණය: සන්දර්භය සඳහා: පුනරාවර්තන මිලියන 50 ක් භාවිතා කරන ලදී.


2

මගේ දිගු කිරීමේ ක්‍රමය වළල්ලට දමනු ඇතැයි සිතුවෙමි (වැඩි විස්තර සඳහා අදහස් බලන්න). මම විධිමත් බංකුවක් සලකුණු කිරීමක් කර නැත, නමුත් බොහෝ අවස්ථාවන් සඳහා එය ඉතා වේගවත් විය යුතු යැයි මම සිතමි.

සංස්කරණය කරන්න: හරි - ඒ නිසා මෙම SO ප්‍රශ්නය අපගේ වර්තමාන ක්‍රියාවට නැංවීමේ කාර්ය සාධනය මෙහි ඉදිරිපත් කර ඇති සමහර විසඳුම් වලට එරෙහිව නැගී සිටින්නේ කෙසේදැයි මට කල්පනා විය. මම සනිටුහන් ටිකක් මඩුල්ල කරන්න තීරණය කළා අපේ විසඳුමක් විසින් සපයන ලද විසඳුමක් කාර්ය සාධනය අනුකූලව ඉතා බව සොයා රිචඩ් වොට්සන් ඔබ විශාල නූල් (100 Kb +) සමග ආක්රමණශීලී සොයන්නාට කරන්නේ දක්වා, විශාල substrings (32 Kb + ) සහ බොහෝ කාවැද්දූ පුනරාවර්තන (10K +). එම අවස්ථාවේදී අපගේ විසඳුම 2X සිට 4X දක්වා මන්දගාමී විය. මේ හා රිචඩ් වොට්සන් විසින් ඉදිරිපත් කරන ලද විසඳුමට අප සැබවින්ම කැමති නිසා, අපි ඒ අනුව අපගේ විසඳුම ප්‍රතිනිර්මාණය කර ඇත්තෙමු. මට අවශ්‍ය වූයේ එයින් ප්‍රයෝජන ගත හැකි ඕනෑම කෙනෙකුට මෙය ලබා දීමටයි.

අපගේ මුල් විසඳුම:

    /// <summary>
    /// Counts the number of occurrences of the specified substring within
    /// the current string.
    /// </summary>
    /// <param name="s">The current string.</param>
    /// <param name="substring">The substring we are searching for.</param>
    /// <param name="aggressiveSearch">Indicates whether or not the algorithm 
    /// should be aggressive in its search behavior (see Remarks). Default 
    /// behavior is non-aggressive.</param>
    /// <remarks>This algorithm has two search modes - aggressive and 
    /// non-aggressive. When in aggressive search mode (aggressiveSearch = 
    /// true), the algorithm will try to match at every possible starting 
    /// character index within the string. When false, all subsequent 
    /// character indexes within a substring match will not be evaluated. 
    /// For example, if the string was 'abbbc' and we were searching for 
    /// the substring 'bb', then aggressive search would find 2 matches 
    /// with starting indexes of 1 and 2. Non aggressive search would find 
    /// just 1 match with starting index at 1. After the match was made, 
    /// the non aggressive search would attempt to make it's next match 
    /// starting at index 3 instead of 2.</remarks>
    /// <returns>The count of occurrences of the substring within the string.</returns>
    public static int CountOccurrences(this string s, string substring, 
        bool aggressiveSearch = false)
    {
        // if s or substring is null or empty, substring cannot be found in s
        if (string.IsNullOrEmpty(s) || string.IsNullOrEmpty(substring))
            return 0;

        // if the length of substring is greater than the length of s,
        // substring cannot be found in s
        if (substring.Length > s.Length)
            return 0;

        var sChars = s.ToCharArray();
        var substringChars = substring.ToCharArray();
        var count = 0;
        var sCharsIndex = 0;

        // substring cannot start in s beyond following index
        var lastStartIndex = sChars.Length - substringChars.Length;

        while (sCharsIndex <= lastStartIndex)
        {
            if (sChars[sCharsIndex] == substringChars[0])
            {
                // potential match checking
                var match = true;
                var offset = 1;
                while (offset < substringChars.Length)
                {
                    if (sChars[sCharsIndex + offset] != substringChars[offset])
                    {
                        match = false;
                        break;
                    }
                    offset++;
                }
                if (match)
                {
                    count++;
                    // if aggressive, just advance to next char in s, otherwise, 
                    // skip past the match just found in s
                    sCharsIndex += aggressiveSearch ? 1 : substringChars.Length;
                }
                else
                {
                    // no match found, just move to next char in s
                    sCharsIndex++;
                }
            }
            else
            {
                // no match at current index, move along
                sCharsIndex++;
            }
        }

        return count;
    }

මෙන්න අපගේ සංශෝධිත විසඳුම:

    /// <summary>
    /// Counts the number of occurrences of the specified substring within
    /// the current string.
    /// </summary>
    /// <param name="s">The current string.</param>
    /// <param name="substring">The substring we are searching for.</param>
    /// <param name="aggressiveSearch">Indicates whether or not the algorithm 
    /// should be aggressive in its search behavior (see Remarks). Default 
    /// behavior is non-aggressive.</param>
    /// <remarks>This algorithm has two search modes - aggressive and 
    /// non-aggressive. When in aggressive search mode (aggressiveSearch = 
    /// true), the algorithm will try to match at every possible starting 
    /// character index within the string. When false, all subsequent 
    /// character indexes within a substring match will not be evaluated. 
    /// For example, if the string was 'abbbc' and we were searching for 
    /// the substring 'bb', then aggressive search would find 2 matches 
    /// with starting indexes of 1 and 2. Non aggressive search would find 
    /// just 1 match with starting index at 1. After the match was made, 
    /// the non aggressive search would attempt to make it's next match 
    /// starting at index 3 instead of 2.</remarks>
    /// <returns>The count of occurrences of the substring within the string.</returns>
    public static int CountOccurrences(this string s, string substring, 
        bool aggressiveSearch = false)
    {
        // if s or substring is null or empty, substring cannot be found in s
        if (string.IsNullOrEmpty(s) || string.IsNullOrEmpty(substring))
            return 0;

        // if the length of substring is greater than the length of s,
        // substring cannot be found in s
        if (substring.Length > s.Length)
            return 0;

        int count = 0, n = 0;
        while ((n = s.IndexOf(substring, n, StringComparison.InvariantCulture)) != -1)
        {
            if (aggressiveSearch)
                n++;
            else
                n += substring.Length;
            count++;
        }

        return count;
    }

1
string Name = "Very good nice one is very good but is very good nice one this is called the term";
bool valid=true;
int count = 0;
int k=0;
int m = 0;
while (valid)
{
    k = Name.Substring(m,Name.Length-m).IndexOf("good");
    if (k != -1)
    {
        count++;
        m = m + k + 4;
    }
    else
        valid = false;
}
Console.WriteLine(count + " Times accures");

1
string s = "HOWLYH THIS ACTUALLY WORKSH WOWH";
int count = 0;
for (int i = 0; i < s.Length; i++)
   if (s[i] == 'H') count++;

එය නූලෙහි ඇති සෑම අක්ෂරයක්ම පරික්ෂා කරයි, චරිතය ඔබ සොයන චරිතය නම්, ගණන් කිරීමට එකක් එක් කරන්න.


1

ඔබ මෙම වෙබ් පිටුව පරීක්ෂා කරන්නේ නම්, සමාන්තර ලූප භාවිතා කිරීම ඇතුළුව මෙය සිදු කිරීමේ විවිධ ක්‍රම 15 ක් මිණුම් සලකුණු කර ඇත.

වේගවත්ම ක්‍රමය පෙනෙන්නේ ලූප සඳහා තනි නූල් එකක් (ඔබට .Net අනුවාදය <4.0 තිබේ නම්) හෝ සමාන්තරව. සඳහා ලූපයක් (දහස් ගණනක් චෙක්පත් සමඟ .Net> 4.0 භාවිතා කරන්නේ නම්).

"Ss" යනු ඔබේ සෙවුම් නූල යැයි උපකල්පනය කිරීම, "ch" යනු ඔබේ අක්‍ෂර අරාවයි (ඔබ සොයන වර්‍ග එකකට වඩා තිබේ නම්), වේගවත්ම ධාවන කාලය තනි නූල් සහිත කේතයේ මූලික සාරාංශය මෙන්න:

for (int x = 0; x < ss.Length; x++)
{
    for (int y = 0; y < ch.Length; y++)
    {
        for (int a = 0; a < ss[x].Length; a++ )
        {
        if (ss[x][a] == ch[y])
            //it's found. DO what you need to here.
        }
    }
}

මිණුම් සලකුණු ප්‍රභව කේතය ද ලබා දී ඇති බැවින් ඔබට ඔබේම පරීක්ෂණ ක්‍රියාත්මක කළ හැකිය.


1
str="aaabbbbjjja";
int count = 0;
int size = str.Length;

string[] strarray = new string[size];
for (int i = 0; i < str.Length; i++)
{
    strarray[i] = str.Substring(i, 1);
}
Array.Sort(strarray);
str = "";
for (int i = 0; i < strarray.Length - 1; i++)
{

    if (strarray[i] == strarray[i + 1])
    {

        count++;
    }
    else
    {
        count++;
        str = str + strarray[i] + count;
        count = 0;
    }

}
count++;
str = str + strarray[strarray.Length - 1] + count;

මෙය චරිත සිදුවීම ගණනය කිරීම සඳහා ය. මෙම උදාහරණය සඳහා ප්‍රතිදානය "a4b4j3" වනු ඇත


2
'නූලක සිදුවීම් ගණන් කිරීම' වඩා වැඩි අක්ෂර සංඛ්‍යාවක් නොවේ - නරේන්ඩාට ගැලපෙන නූල කුමක්දැයි සඳහන් කරන්නේ කෙසේද?
පෝල් සුලිවන්

1
int ගණන = 0; string str = "අපට foo සහ foo ඇත, කරුණාකර මෙහි foo ගණන් කරන්න"; string stoccurance = "foo"; string [] strarray = str.Split (''); අරා.සෝර්ට් (ස්ට්රේ); str = ""; සඳහා (int i = 0; i <strarray.Length - 1; i ++) {if (strarray [i] == stoccurance) {count ++; +} str = "+ + ස්ට්‍රොකියුරන්ස් +" සඳහා සිදුවීම් ගණන "+ ගණන් කිරීම; මෙම උදාහරණයෙන් ඔබට ඕනෑම නූල් සිදුවීමක් ගණනය කළ හැකිය, මම "foo" හි සිදුවීම ගණනය කරන අතර එය මට ප්‍රතිදානය 3 ලබා දෙනු ඇත.
නරේන්ද්‍ර කුමාර්

1

නූල් පරිසීමකය සඳහා (විෂයය පවසන පරිදි වර්‍ග නඩුව සඳහා නොවේ):
string source = "@@@ වරක් @@@ on @@@ a @@@ වේලාව @@@";
int count = source.Split (නව [] {"@@@" String, StringSplitOptions.RemoveEmptyEntries). දිග - 1;

පෝස්ටරයේ මුල් ප්‍රභව අගය ("/ වරක් / මත / අ / වේලාව /") ස්වාභාවික පරිසීමකය යනු වර්‍ගයක් '/' වන අතර ප්‍රතිචාර මඟින් මූලාශ්‍රය පැහැදිලි කරයි.


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.