How to get substring that start and end with the same character in C#

1 Answer

0 votes
using System;
 
class Program
{
    static int start = 0, end = 0;
      
    static void char_substr(string str, char ch) {  
        int len =  str.Length;
     
        for (int i = 0; i < len; i++) {  
            if (str[i] == ch) {
                start = i;
                for (int j = i + 1; j < len; j++) {
                    if (str[j] == ch) {  
                        end = j + 1; 
                        break;
                    }
                }
            }
            if (start != 0)
                break;
        }
    }  
    
    static void Main()
    {
        string str = "hgdabcvauyec";
     
        char_substr(str, 'a');

        Console.Write(str.Substring(start, end - start));
    }
}
 
 
 
/*
run:
 
abcva
 
*/

 



answered Apr 17, 2019 by avibootz

Related questions

...