Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove first two and last two chars in a string?

Tags:

string

c#

Is there an easy way to remove the first 2 and last 2 chars in a string?

I have this string:

\nTESTSTRING\n

How could I easily delete them?

like image 904
Sergio Tapia Avatar asked Oct 20 '09 22:10

Sergio Tapia


1 Answers

str = str.Substring(2,str.Length-4)

Of course you must test that the string contains more than 4 chars before doing this. Also in your case it seems that \n is a single newline character. If all you want to do is remove leading and trailing whitespaces, you should use

str.Trim()

as suggested by Charles

like image 89
Manu Avatar answered Oct 05 '22 05:10

Manu