text: Remove the first and last occurrence of a given Character from a String | C # Program to implement the above approach ; Function to remove first and last occurrence of a given character from the given String ; Traverse the given String from the beginning ; If ch is found ; Traverse the given String from the end ; If ch is found ; Driver Code
code: using System ; class GFG { static String removeOcc ( String s , char ch ) { for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == ch ) { s = s . Substring ( 0 , i ) + s . Substring ( i + 1 ) ; break ; } } for ( int i = s . Length - 1 ; i > - 1 ; i -- ) { if ( s [ i ] == ch ) { s = s . Substring ( 0 , i ) + s . Substring ( i + 1 ) ; break ; } } return s ; } public static void Main ( String [ ] args ) { String s = " hello ▁ world " ; char ch = ' l ' ; Console . Write ( removeOcc ( s , ch ) ) ; } }