Tiny way to get the first 25 characters

后端 未结 12 1517
感动是毒
感动是毒 2020-12-29 13:12

Can anyone think of a nicer way to do the following:

public string ShortDescription
{
    get { return this.Description.Length <= 25 ? this.Description :          


        
相关标签:
12条回答
  • 2020-12-29 14:00

    I can't think of any but your approach might not be the best. Are you adding presentation logic into your data object? If so then I suggest you put that logic elsewhere, for example a static StringDisplayUtils class with a GetShortStringMethod( int maxCharsToDisplay, string stringToShorten).

    However, that approach might not be great either. What about different fonts and character sets? You'd have to start measuring the actual string length in terms of pixels. Check out the AutoEllipsis property on the winform's Label class (you'll prob need to set AutoSize to false if using this). The AutoEllipsis property, when true, will shorten a string and add the '...' chars for you.

    0 讨论(0)
  • 2020-12-29 14:03

    You should see if you can reference the Microsoft.VisualBasic DLL into your app so you can make use of the "Left" function.

    0 讨论(0)
  • 2020-12-29 14:10

    The way you've done it seems fine to me, with the exception that I would use the magic number 25, I'd have that as a constant.

    Do you really want to store this in your bean though? Presumably this is for display somewhere, so your renderer should be the thing doing the truncating instead of the data object

    0 讨论(0)
  • 2020-12-29 14:10

    I think the approach is sound, though I'd recommend a few adjustments

    • Move the magic number to a const or configuration value
    • Use a regular if conditional rather than the ternary operator
    • Use a string.Format("{0}...") rather than + "..."
    • Have just one return point from the function

    So:

    public string ShortDescription
    {
        get
        {
            const int SHORT_DESCRIPTION_LENGTH = 25;
    
            string _shortDescription = Description;
    
            if (Description.Length > SHORT_DESCRIPTION_LENGTH)
            {
                _shortDescription = string.Format("{0}...", Description.Substring(0, SHORT_DESCRIPTION_LENGTH));
            }
    
            return _shortDescription;
        }
    }
    

    For a more general approach, you might like to move the logic to an extension method:

    public static string ToTruncated(this string s, int truncateAt)
    {
        string truncated = s;
    
        if (s.Length > truncateAt)
        {
            truncated = string.Format("{0}...", s.Substring(0, truncateAt));
        }
    
        return truncated;
    }
    

    Edit

    I use the ternary operator extensively, but prefer to avoid it if the code becomes sufficiently verbose that it starts to extend past 120 characters or so. In that case I'd like to wrap it onto multiple lines, so find that a regular if conditional is more readable.

    Edit2

    For typographical correctness you could also consider using the ellipsis character (…) as opposed to three dots/periods/full stops (...).

    0 讨论(0)
  • 2020-12-29 14:12
    public static Take(this string s, int i)
    {
        if(s.Length <= i)
            return s
        else
            return s.Substring(0, i) + "..."
    }
    
    public string ShortDescription
    {
        get { return this.Description.Take(25); }
    }
    
    0 讨论(0)
  • 2020-12-29 14:14

    I needed this so often, I wrote an extension method for it:

    public static class StringExtensions
    {
        public static string SafeSubstring(this string input, int startIndex, int length, string suffix)
        {
            // Todo: Check that startIndex + length does not cause an arithmetic overflow - not that this is likely, but still...
            if (input.Length >= (startIndex + length))
            {
                if (suffix == null) suffix = string.Empty;
                return input.Substring(startIndex, length) + suffix;
            }
            else
            {
                if (input.Length > startIndex)
                {
                    return input.Substring(startIndex);
                }
                else
                {
                    return string.Empty;
                }
            }
        }
    }
    

    if you only need it once, that is overkill, but if you need it more often then it can come in handy.

    Edit: Added support for a string suffix. Pass in "..." and you get your ellipses on shorter strings, or pass in string.Empty for no special suffixes.

    0 讨论(0)
提交回复
热议问题