How to remove a slash from a string in C #

I have a line like this: "/ Audit report" It is assigned to the rep variable. If i print

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

It returns "/ uditReport" instead of the desired "AuditReport", that is, it does not remove the slash. How can I remove it?

+4
source share
8 answers

.NET row indexes are zero based. The documentation for Remove states that the first argument is "Zero-based position to start character deletion."

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

As an alternative, using Substring more readable, in my opinion:

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

Or, you can use TrimStart , depending on your requirements. Note that if your line starts with several consecutive slashes, then TrimStart will delete all of them.)

 string r = rep.SysName.TrimStart('/'); 
+17
source

Try:

 var r = rep.SysName.Remove(0, 1); 
+6
source

You need:

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

The first parameter is the beginning, the second is the number of characters to delete. (1,1) will delete the second character, not the first.

+4
source

The index is based on 0, so you remove the second character. Try instead

 var r = rep.SysName.Remove(0, 1); 
+2
source

What is "/AuditReport".Replace("/","") ?

0
source

.NET row indexes are zero-based, so:

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

You can also use:

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

You need to write var r = rep.SysName.Remove(0, 1); . I assume you have a VisualBasic background (e.g. me :-)), arrays, strings, etc. In C #, they start with an index of 0 instead of 1 , as in some other languages.

0
source

If you are dealing with Uri , you can do it like this:

 var route = uri.GetComponents(UriComponents.Path, UriFormat.UriEscaped); 

For example, it will return api/report instead of /api/report .

0
source

Source: https://habr.com/ru/post/1383013/


All Articles