How to remove the last occurrence of comma from a string in C#

1 Answer

0 votes
using System;

class Program
{
    static void Main() {
        string s = "c c++, java, python, c#";
        string comma = ",";
        
        int index = s.LastIndexOf(comma);
        
        s = (index < 0) ? s : s.Remove(index, comma.Length);

        Console.Write(s);
    }
}




/*
run:

c c++, java, python c#

*/

 



answered Apr 18, 2022 by avibootz

Related questions

1 answer 133 views
1 answer 150 views
1 answer 145 views
1 answer 134 views
2 answers 159 views
...