Use the String. format() method to pad the string with spaces on left and right, and then replace these spaces with the given character using String. replace() method. For left padding, the syntax to use the String.
The %d specifier indicates a placeholder for a decimal number. The width on that suggests it's a 6 digit number.
The Java String. format() method returns the formatted string by a given locale, format, and argument. If the locale is not specified in the String. format() method, it uses the default locale by calling the Locale.
This will give you exactly the strings that you asked for:
string s = "String goes here";
string lineAlignedRight = String.Format("{0,27}", s);
string lineAlignedCenter = String.Format("{0,-27}",
String.Format("{0," + ((27 + s.Length) / 2).ToString() + "}", s));
string lineAlignedLeft = String.Format("{0,-27}", s);
As of Visual Studio 2015 you can also do this with Interpolated Strings (its a compiler trick, so it doesn't matter which version of the .net framework you target).
string value = "String goes here";
string txt1 = $"{value,20}";
string txt2 = $"{value,-20}";
The first and the last, at least, are possible using the following syntax:
String.Format("{0,20}", "String goes here");
String.Format("{0,-20}", "String goes here");
You've been shown PadLeft
and PadRight
. This will fill in the missing PadCenter
.
public static class StringUtils
{
public static string PadCenter(this string s, int width, char c)
{
if (s == null || width <= s.Length) return s;
int padding = width - s.Length;
return s.PadLeft(s.Length + padding / 2, c).PadRight(width, c);
}
}
Note to self: don't forget to update own CV: "One day, I even fixed Joel Coehoorn's code!" ;-D -Serge
try this:
"String goes here".PadLeft(20,' ');
"String goes here".PadRight(20,' ');
for the center get the length of the string and do padleft and padright with the necessary characters
int len = "String goes here".Length;
int whites = len /2;
"String goes here".PadRight(len + whites,' ').PadLeft(len + whites,' ');
Thanks for the discussion, this method also works (VB):
Public Function StringCentering(ByVal s As String, ByVal desiredLength As Integer) As String
If s.Length >= desiredLength Then Return s
Dim firstpad As Integer = (s.Length + desiredLength) / 2
Return s.PadLeft(firstpad).PadRight(desiredLength)
End Function
Here is the C# version:
public string StringCentering(string s, int desiredLength)
{
if (s.Length >= desiredLength) return s;
int firstpad = (s.Length + desiredLength) / 2;
return s.PadLeft(firstpad).PadRight(desiredLength);
}
To aid understanding, integer variable firstpad is used. s.PadLeft(firstpad) applies the (correct number of) leading white spaces. The right-most PadRight(desiredLength) has a lower binding finishes off by applying trailing white spaces.
Here's a VB.NET version I created, inspired by Joel Coehoorn's answer, Oliver's edit, and shaunmartin's comment:
<Extension()>
Public Function PadCenter(ByVal [string] As String, ByVal width As Integer, ByVal c As Char) As String
If [string] Is Nothing Then [string] = String.Empty
If (width <= [string].Length) Then Return [string]
Dim padding = width - [string].Length
Return [string].PadLeft([string].Length + (padding \ 2), c).PadRight(width, c)
End Function
<Extension()>
Public Function PadCenter(ByVal [string] As String, ByVal width As Integer) As String
If [string] Is Nothing Then [string] = String.Empty
If (width <= [string].Length) Then Return [string]
Dim padding = width - [string].Length
Return [string].PadLeft([string].Length + (padding \ 2)).PadRight(width)
End Function
This is set up as a string extension, inside a Public Module (the way you do Extensions in VB.NET, a bit different than C#). My slight change is that it treats a null string as an empty string, and it pads an empty string with the width value (meets my particular needs). Hopefully this will convert easily to C# for anyone who needs it. If there's a better way to reference the answers, edits, and comments I mentioned above, which inspired my post, please let me know and I'll do it - I'm relatively new to posting, and I couldn't figure out to leave a comment (might not have enough rep yet).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With