Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove slash from string in C#

Tags:

string

c#

slash

I have a string like this: "/AuditReport" It is assigned to variable rep. If I type

var r = rep.SysName.Remove(1, 1);

It returns "/uditReport" instead of desired "AuditReport", i.e. it does not remove the slash. How could I remove it?

like image 842
Liker777 Avatar asked Nov 27 '22 09:11

Liker777


2 Answers

String indices in .NET are zero-based. The documentation for Remove states that the first argument is "The zero-based position to begin deleting characters".

string r = rep.SysName.Remove(0, 1);

Alternatively, using Substring is more readable, in my opinion:

string r = rep.SysName.Substring(1);

Or, you could possibly use TrimStart, depending on your requirements. (However, note that if your string starts with multiple successive slashes then TrimStart will remove all of them.)

string r = rep.SysName.TrimStart('/');
like image 131
LukeH Avatar answered Jan 09 '23 10:01

LukeH


Try:

var r = rep.SysName.Remove(0, 1);
like image 36
Abbas Avatar answered Jan 09 '23 10:01

Abbas