Microsoft did a Great Job with the String ObjectWhere is PadCenter? | | Somehow, in the creation of the String Object in .NET Framework, Microsoft overlooked the need for PadCenter. I'm thankful for the new intrinsic PadLeft and PadRight, but anyone who has ever done much formatting for printing knows that you also need a PadCenter function also. This article provides the code for it.
I originally wrote the code, although I won't tell you how many lines of code it was. I asked Brian to look at it and see if he could improve on the code. Brian is a CS graduate of Georgia Tech. The code shown below is the result of my requirement and Brians code. We hope it helps someone.
' Replacement for Non existant PadCenter method of string object
' accepts string and the pad width, returns the padded string
Private Function PadCenter(ByVal s As String, ByVal n As Integer) _
As String
Return s.PadRight(s.Length + _
Math.Max((n - s.Length), 0) \ 2).PadLeft(n).Substring(0, n)
End Function
| |