C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,010 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello
I have a string that can be 3 or 4 characters long, all I need to do is in case my string is 3 chars, add a single space in the beginning/end of my string, of course:
If string.Length = 3 Then string = " " + string
If string.Length = 3 Then string = string + " "
Just wonder if there's a string format syntax available to do the same? Thanks :)
Check the examples:
string s = "abc";
string result1 = string.Format( "Result 1 = '{0,4}'", s );
string result2 = string.Format( "Result 2 = '{0,-4}'", s );
string result3 = $"Result 3 = '{s,4}'";
string result4 = $"Result 4 = '{s,-4}'";
string result5 = s.PadLeft( 4 );
string result6 = s.PadRight( 4 );