{"id":4658,"date":"2022-12-10T20:00:52","date_gmt":"2022-12-10T19:00:52","guid":{"rendered":"https:\/\/ekiwi.de\/index.php\/4658\/c-anzahl-vorkommen-eines-strings-in-einem-anderen-string\/"},"modified":"2023-08-13T17:47:08","modified_gmt":"2023-08-13T15:47:08","slug":"c-sharp-count-substring-in-string","status":"publish","type":"post","link":"https:\/\/ekiwi.de\/en\/index.php\/4658\/c-sharp-count-substring-in-string\/","title":{"rendered":"C# Count Substring in String"},"content":{"rendered":"<p>When programming in Visual Studio with C# (C-Shap), it can happen that you have to determine how often a substring occurs in another string.\u00a0 With the number you can then make further processing and comparisons in the program.<\/p>\n<p>The easiest way to determine the frequency of occurrence of a string in another string is to use a <a title=\"use regex (regular expression) to get email\" href=\"https:\/\/ekiwi-blog.de\/en\/54130\/regex-for-email\/\" target=\"_blank\" rel=\"noopener\">regex function<\/a>. The corresponding code then looks as follows.<\/p>\n<pre><code><span style=\"color: #0000ff;\">int<\/span> cnt = <span style=\"color: #339966;\">Regex<\/span>.<span style=\"color: #800000;\">Matches<\/span>(myText, \"<span style=\"color: #993300;\">MySearchText<\/span>\").Count<\/code><\/pre>\n<p>In this case the variable &#8220;<em>myText<\/em>&#8221; is the string which should be searched. And the variable &#8220;<em>MySearchString<\/em>&#8221; is the substring whose number is to be determined in &#8220;<em>myText<\/em>&#8220;.<\/p>\n<p>The function <em><strong>Regex.Matches<\/strong><\/em> returns a MatchCollection, which contains all found values. The exact number can now be determined via the Count property of this collection and assigned to an integer variable for further processing, for example.<\/p>\n<p>But you can also use the expression directly in an <strong>if<\/strong>-condition, for example, if you want to determine whether the search value\/substring occurs in a certain number, in order to then execute any other statement, such as for <a title=\"binding more than one property to XAML in C# Visual Studio\" href=\"https:\/\/ekiwi-blog.de\/en\/25911\/binding-multiple-properties-with-xaml-c\/\" target=\"_blank\" rel=\"noopener\">binding some properties<\/a> or similar.<\/p>\n<pre><code><span style=\"color: #0000ff;\"><span style=\"color: #800080;\">if<\/span><\/span>(<span style=\"color: #339966;\">Regex<\/span>.<span style=\"color: #800000;\">Matches<\/span>(myText, \"<span style=\"color: #993300;\">MySearchText<!-- CAPTCHA --><\/span>\").Count &gt; 2)\r\n{\r\n    <span style=\"color: #339966;\">\/\/do something<\/span>\r\n}<\/code><\/pre>\n<p>Thus, it is possible to determine in a very clear way with only one line of code how often a certain text, character, letter or <a title=\"C#: Find or replace first occurrence of character in string\" href=\"https:\/\/ekiwi.de\/en\/index.php\/4664\/c-find-or-replace-first-occurrence-of-character-in-string\/\">character string<\/a> occurs in another text.<\/p>","protected":false},"excerpt":{"rendered":"<p>When programming in Visual Studio with C# (C-Shap), it can happen that you have to determine how often a substring<\/p>\n","protected":false},"author":2,"featured_media":3616,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[870],"tags":[871,872,873,876,874,875],"class_list":["post-4658","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming","tag-c-sharp-en","tag-c-en","tag-programmieren-en","tag-programming-en","tag-string-en","tag-visual-studio-en"],"_links":{"self":[{"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/posts\/4658","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/comments?post=4658"}],"version-history":[{"count":0,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/posts\/4658\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/media\/3616"}],"wp:attachment":[{"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/media?parent=4658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/categories?post=4658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/tags?post=4658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}