{"id":4664,"date":"2022-12-11T09:43:09","date_gmt":"2022-12-11T08:43:09","guid":{"rendered":"https:\/\/ekiwi.de\/?p=4664"},"modified":"2022-12-11T09:53:37","modified_gmt":"2022-12-11T08:53:37","slug":"c-find-or-replace-first-occurrence-of-character-in-string","status":"publish","type":"post","link":"https:\/\/ekiwi.de\/en\/index.php\/4664\/c-find-or-replace-first-occurrence-of-character-in-string\/","title":{"rendered":"C#: Find or replace first occurrence of character in string"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of content<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ekiwi.de\/en\/index.php\/4664\/c-find-or-replace-first-occurrence-of-character-in-string\/#Replace_does_not_work\" >Replace() does not work<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ekiwi.de\/en\/index.php\/4664\/c-find-or-replace-first-occurrence-of-character-in-string\/#Solution\" >Solution<\/a><\/li><\/ul><\/nav><\/div>\n<p>If you program a lot with C#, for example in Visual Studio, it happens again and again that you want to process strings. A problem can be that you want to find a word, certain characters or whole sentences within a string, i.e. a substring, in order to replace it with something else, for example.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Replace_does_not_work\"><\/span>Replace() does not work<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>As is well known, there are always many or several ways to get to the target in programming languages. An obvious variant would be to simply use the <em>Replace()<\/em> function \u00e1 la:<\/p>\n<pre><code><span style=\"color: #000080;\">myText <\/span>= <span style=\"color: #000080;\">myText<\/span>.<span style=\"color: #808000;\">Replace<\/span>(\"<span style=\"color: #993300;\">searchString<\/span>\", \"<span style=\"color: #993300;\">replaceString<\/span>\");<\/code><\/pre>\n<p>Here <em>&#8220;searchString&#8221;<\/em> is the searched substring and <em>&#8220;replaceString&#8221;<\/em> is the string with which we want to replace<em> &#8220;searchString&#8221;<\/em>. In this case the string <em>&#8220;myText&#8221;<\/em> is searched.<\/p>\n<p>The problem is that the <em>Replace<\/em> function replaces <strong>all occurrences<\/strong> of <em>searchString<\/em>. In our question, however, we only want to replace the first occurrence of a certain searched string. This means that you can only use <em>Replace<\/em> if you want to replace all occurrences.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Solution\"><\/span>Solution<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>First you have to find out the position where the string occurs first. This can be done with the <em>IndexOf()<\/em> function. Then you remove the string you are looking for from the string (<em>Remove()<\/em> function), and then insert the new string (<em>Insert()<\/em> function). In C# code the whole thing looks like this, where <em>int i<\/em> is the zero-based integer value at which position searchString was found the first time.<\/p>\n<pre><code><span style=\"color: #0000ff;\">int<\/span> i = <span style=\"color: #000080;\">myText<\/span>.<span style=\"color: #808000;\">IndexOf<\/span>(\"<span style=\"color: #993300;\">searchString<\/span>\");\r\n<span style=\"color: #000080;\">myText<\/span> = <span style=\"color: #000080;\">myText<\/span>.<span style=\"color: #808000;\">Remove<\/span>(i, \"<span style=\"color: #993300;\">searchString<\/span>\".Length);\r\n<span style=\"color: #000080;\">myText<\/span> = <span style=\"color: #000080;\">myText<\/span>.<span style=\"color: #808000;\">Insert<\/span>(i, \"<span style=\"color: #993300;\">replaceString<\/span>\");<\/code><\/pre>\n<p>&nbsp;<\/p>","protected":false},"excerpt":{"rendered":"<p>If you program a lot with C#, for example in Visual Studio, it happens again and again that you want<\/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":[872,876,875],"class_list":["post-4664","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming","tag-c-en","tag-programming-en","tag-visual-studio-en"],"_links":{"self":[{"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/posts\/4664","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=4664"}],"version-history":[{"count":0,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/posts\/4664\/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=4664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/categories?post=4664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/tags?post=4664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}