Extract substrings between any pair of delimiters | C # program to implement the above approach ; Function to print strings present between any pair of delimeters ; Stores the indices of ; If opening delimeter is encountered ; If closing delimeter is encountered ; Extract the position of opening delimeter ; Length of substring ; Extract the substring ; Driver Code
using System ; using System . Collections ; class GFG { static void printSubsInDelimeters ( string str ) { Stack dels = new Stack ( ) ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] == ' [ ' ) { dels . Push ( i ) ; } else if ( str [ i ] == ' ] ' && dels . Count > 0 ) { int pos = ( int ) dels . Peek ( ) ; dels . Pop ( ) ; int len = i - 1 - pos ; string ans = str . Substring ( pos + 1 , len ) ; Console . WriteLine ( ans ) ; } } } static void Main ( ) { string str = " [ This ▁ is ▁ first ] ▁ ignored ▁ text ▁ [ This ▁ is ▁ second ] " ; printSubsInDelimeters ( str ) ; } }