Accounting for the length of an HTML DIV for truncating text

I have a

that I'm filling with a file name of indeterminable size. If the size exceeds the length of the
then I want it to be cut down and the last 3 characters replaced with a "...". Currently I have the following:

<div id="fileName" class="field-row" title="<%= fileName %>">
    <% if(fileName.Length > 20) { %>
        Filename: <%= fileName.Substring(0,20) %>...
    <% } else { %>
        Filename: <%= fileName %>
    <% } %>
</div>

This works fine but if I have a file name that is made up of i's vs. W's then the div will have unused space on the right.

For example:

"iiiiiiiiii.jpg"

is shorter than:

"WWWWWWWWWW.jpg"

...even though they have the same number of characters.

Is there a better way to do this that accounts for the length of the

?

Thank you,

Aaron

1
задан Aaron Salazar 11 October 2010 в 18:36
поделиться