Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function for add blank space after checking string minimum length

Tags:

c#

Method for adding blank space after checking string minimum length,if string enter is not equal to minimum length defined then add blank space after the string to full fill the minimum length condition ? ?

example : minimum length =30

and string = "anuragsaraswat123abc" 20 character

then automatically find the diff between minimum length of string and available string and add blank space. Suggest me the method

like image 833
anurag Avatar asked Jun 11 '26 17:06

anurag


2 Answers

This is covered in the framework with String.PadLeft and String.PadRight

string MyString = "Hello World!";
Console.WriteLine(MyString.PadRight(30, ' '));
like image 50
RB. Avatar answered Jun 13 '26 09:06

RB.


You can use String.PadRight Method for that.

Returns a new string that left-aligns the characters in this string by padding them on the right with a specified Unicode character, for a specified total length.

static string YourMethod(string s)
{
     return s.Length < 30 ? s.PadRight(30 - s.Length, ' ') : s;
}

Here is a DEMO.

Note: Since you can't see the blanks, it doesn't mean they are not there ;)

like image 32
Soner Gönül Avatar answered Jun 13 '26 10:06

Soner Gönül



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!