How to remove the trailing slashes from a string in C#

1 Answer

0 votes
using System;
using System.Text.RegularExpressions;

class Program
{
    static string RemoveTrailingSlashes(string str) {
        return Regex.Replace(str, "/+$", "");
    }

    static void Main()
    {
        Console.WriteLine(RemoveTrailingSlashes("/ABC/"));
        Console.WriteLine(RemoveTrailingSlashes("/ABC////"));
        Console.WriteLine(RemoveTrailingSlashes("/ABC"));
    }
}



/*
run:

/ABC
/ABC
/ABC

*/

 



answered Jun 14, 2025 by avibootz

Related questions

1 answer 63 views
1 answer 93 views
2 answers 121 views
1 answer 110 views
1 answer 153 views
...