Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use myString.PadLeft?

Tags:

c#

.net

winforms

Why doesn't this work ?

string myString = "test";
int i = myString.Length; // i = 4
myString.PadLeft(5, '_'); // "myString" is should be equal to "_test", but it still "test"
i = myString.Length; // i = 4 (should be 5)
like image 653
Steven Avatar asked Mar 13 '26 00:03

Steven


1 Answers

Most string methods don't change string itself, but return new string, so use it like this:

myString = myString.PadLeft(5, '_');

EDIT: Ahh yes, all methods. I thought about methods that don't return string and somehow ended with not entirely true sentence.

like image 54
Varius Avatar answered Mar 14 '26 12:03

Varius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!