Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trimming string, removing last character in c#

Tags:

c#

regex

asp.net

I am having troubles removing backslash from my string. The string is like this "3adsadas34\". I want to remove the backslash at the end, I tried with:

urlContent = realUrl.Remove(realUrl.Length - 1, 1);

But it doesn't want to work. I would like to know if I can use regex, and if I can, maybe someone can provide regex sample for removing '\' from that string, or some other way to remove the backslash is more then welcome. Thanks in advance, Laziale

like image 453
Laziale Avatar asked Dec 05 '22 17:12

Laziale


2 Answers

Try this

urlContent = realUrl.TrimEnd('\\');

Note: You have to escape the backslash.

char ch = '\\';
string s = "\\";
string verbatimString = @"\";

Your Remove code looks OK. realUrl.Substring(0, realUrl.Length-1) would do the same. The problem might be somewhere else.

like image 199
Olivier Jacot-Descombes Avatar answered Dec 10 '22 09:12

Olivier Jacot-Descombes


you can use trim end,

realUrl.TrimEnd('\\');

Remember, this will remove all trailing occurences of '\'

like image 25
labroo Avatar answered Dec 10 '22 10:12

labroo