{"id":5023,"date":"2021-04-28T14:17:25","date_gmt":"2021-04-28T13:17:25","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=5023"},"modified":"2022-07-25T08:35:45","modified_gmt":"2022-07-25T07:35:45","slug":"c-find-string-between-matching-brace","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-find-string-between-matching-brace\/","title":{"rendered":"C# Find String between matching brace"},"content":{"rendered":"\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"csharp\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">public static string FindStringBetweenMatchingBrace(string s, int start, char startBrace, char endBrace)\n{\n    var braces = new char[] { startBrace, endBrace };\n    var pos = start;\n    var end = start;\n    var level = 0;\n    var notStarted = true;\n    while (true)\n    {\n        var index = s.IndexOfAny(braces, pos);\n        if (index &lt; 0) break;\n        if (notStarted)\n        {\n            notStarted = false;\n            start = index + 1;\n        }\n        if (s[index] == startBrace) level++;\n        if (s[index] == endBrace) level--;\n        if (level &lt;= 0)\n        {\n            end = index;\n            break;\n        }\n        pos = index + 1;\n    }\n    return s.Substring(start, end - start);\n}<\/pre><\/div>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"javascript\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">function indexOfAny(str, anyOf, startIndex) {\n    if (!str) return -1;\n    if (!anyOf) return -1;\n    let minIndex = -1;\n    for (let j = 0; j &lt; anyOf.length; j++) {\n        let index = str.indexOf(anyOf[j], startIndex);\n        if (index >= 0 &amp;&amp; (minIndex === -1 || index &lt; minIndex)) {\n            minIndex = index;\n        }\n    }\n    return minIndex;\n};\n\nfunction findStringBetweenMatchingBrace(s, start, startBrace, endBrace) {\n    const braces = [startBrace, endBrace];\n    let pos = start;\n    let end = start;\n    let level = 0;\n    let notStarted = true;\n    while (true) {\n        var index = indexOfAny(s, braces, pos);\n        if (index &lt; 0) break;\n        if (notStarted) {\n            notStarted = false;\n            start = index + 1;\n        }\n        if (s[index] == startBrace) level++;\n        if (s[index] == endBrace) level--;\n        if (level &lt;= 0) {\n            end = index;\n            break;\n        }\n        pos = index + 1;\n    }\n    return s.substring(start, end);\n}\n\n\nconst result = findStringBetweenMatchingBrace(\"abc (def efg (abf ddd) t)est\", 0, \"(\", \")\");\nconsole.log(result);\nconst result2 = findStringBetweenMatchingBrace(result, 0, \"(\", \")\");\nconsole.log(result2);\n<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6,5,4,1],"tags":[],"class_list":["post-5023","post","type-post","status-publish","format-standard","hentry","category-dotnet","category-javascript","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/5023","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=5023"}],"version-history":[{"count":6,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/5023\/revisions"}],"predecessor-version":[{"id":5975,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/5023\/revisions\/5975"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=5023"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=5023"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=5023"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}