views:

146

answers:

3

This question is along the lines of http://stackoverflow.com/questions/371088/html-table-horizontal-spacing

We have an old code base and can not use css. When an entire column in a table is empty, the resulting table is very ugly. We have a text email solution that adds spaces to the end up a word up to the remaining set of characters that you specify using New String. Since New String only takes a char, was seeing what 1 liner, small amount of code examples people could come up with.

We use .net 3.5sp1

Public Function StringSize(ByVal data As String, ByVal size As Short, ByVal usehtml As Boolean) As String
    If data.Length > size Then
        Return Left(data, size - 4) & "... "
    Else
        If usehtml Then
            'small algorithm here ( & nbsp ; )
        Else
            Return data & New String(" ", size - Len(data))
        End If
    End If
End Function
+1  A: 

Your best option is to use a StringBuilder and a For loop.

Dim builder As New System.Text.StringBuilder()
For i = 1 To size - Len(data)
    ' Be sure to take out the space in the nbsp, Stack Overflow doesn't like it for some reason.
    builder.Append("& nbsp;")
Next
Return builder.ToString()
MiffTheFox
+1  A: 

If you really want to pad with  , then - well, frankly - I'm not sure it is a great idea, but something like (excuse the C#):

int count = size - data.Length;
StringBuilder sb = new StringBuilder(
    data, data.Length + (6 * count));
for(int i = 0 ; i < count ; i++) {
    sb.Append("&nbsp;");
}
string s = sb.ToString();

The following is a bad way to do it in one line with LINQ; included only for interest:

string s = data + string.Concat(Enumerable.Repeat("&nbsp;", size - data.Length).ToArray());
Marc Gravell
I assume the downvote is for using C#... that is just petty; the translation is absolutely trivial for someone familiar with VB... I maintain that an answer in VB is fine for most "C#" questions, and an answer in C# is fine for most "VB" questions - as most of the time, the question is really about the framework, not the language. Obviously for *language* specifics it would matter. But this isn't a language question.
Marc Gravell
Freaky! The   disappears when formatted...
Marc Gravell
A: 

1) Lucase: If you must know, anything dealing with html, xml can not be edited because we have over 200 customers that have customizations applied to our base product. All internal code has been rewritten from vb6 to .net

2) Marc: I dont care if the answer is in vb or c#. I can read and write both and I hope most .net programmers can aswell. We just happen to write in vb.net at work.

Thanks for the ideas. Looks like I wasnt missing some readable .net 1 liner to accomplish what I wanted.