예제 #1
0
        public static string Truncate(this string valueToTruncate, int startIndex = 0, int maxLength = 0
                                      , TruncateOptions options = TruncateOptions.FinishWord)
        {
            if (valueToTruncate == null)
            {
                return("");
            }

            if (startIndex > 0 && startIndex < valueToTruncate.Length)
            {
                valueToTruncate = valueToTruncate.Substring(startIndex);
            }

            if (valueToTruncate.Length <= maxLength)
            {
                return(valueToTruncate);
            }

            bool includeEllipsis = (options & TruncateOptions.IncludeEllipsis) ==
                                   TruncateOptions.IncludeEllipsis;
            bool finishWord = (options & TruncateOptions.FinishWord) ==
                              TruncateOptions.FinishWord;
            bool allowLastWordOverflow =
                (options & TruncateOptions.AllowLastWordToGoOverMaxLength) ==
                TruncateOptions.AllowLastWordToGoOverMaxLength;

            string retValue = valueToTruncate;

            if (includeEllipsis)
            {
                maxLength -= 1;
            }

            int lastSpaceIndex = retValue.LastIndexOf(" ",
                                                      maxLength, StringComparison.CurrentCultureIgnoreCase);

            if (!finishWord)
            {
                retValue = retValue.Remove(maxLength);
            }
            else if (allowLastWordOverflow)
            {
                int spaceIndex = retValue.IndexOf(" ",
                                                  maxLength, StringComparison.CurrentCultureIgnoreCase);
                if (spaceIndex != -1)
                {
                    retValue = retValue.Remove(spaceIndex);
                }
            }
            else if (lastSpaceIndex > -1)
            {
                retValue = retValue.Remove(lastSpaceIndex);
            }

            if (includeEllipsis && retValue.Length < valueToTruncate.Length)
            {
                retValue += "&hellip;";
            }
            return(retValue);
        }
예제 #2
0
 internal TruncateMessage Populate(
     NpgsqlLogSequenceNumber walStart, NpgsqlLogSequenceNumber walEnd, DateTime serverClock, uint?transactionXid, TruncateOptions options,
     ReadOnlyArrayBuffer <RelationMessage> relations)
 {
     base.Populate(walStart, walEnd, serverClock, transactionXid);
     Options   = options;
     Relations = relations;
     return(this);
 }
예제 #3
0
        internal TruncateMessage Populate(
            NpgsqlLogSequenceNumber walStart, NpgsqlLogSequenceNumber walEnd, DateTime serverClock, TruncateOptions options,
            uint[] relationIds)
        {
            base.Populate(walStart, walEnd, serverClock);

            Options     = options;
            RelationIds = relationIds;

            return(this);
        }
예제 #4
0
파일: share.aspx.cs 프로젝트: jaytem/minGit
    public string TruncateString(string valueToTruncate, int maxLength, TruncateOptions options)
    {
        if (valueToTruncate == null)
        {
            return "";
        }

        if (valueToTruncate.Length <= maxLength)
        {
            return valueToTruncate;
        }

        bool includeEllipsis = (options & TruncateOptions.IncludeEllipsis) == TruncateOptions.IncludeEllipsis;
        bool finishWord = (options & TruncateOptions.FinishWord) == TruncateOptions.FinishWord;
        bool allowLastWordOverflow = (options & TruncateOptions.AllowLastWordToGoOverMaxLength) == TruncateOptions.AllowLastWordToGoOverMaxLength;

        string retValue = valueToTruncate;

        if (includeEllipsis)
        {
            maxLength -= 1;
        }

        int lastSpaceIndex = retValue.LastIndexOf(" ", maxLength, StringComparison.CurrentCultureIgnoreCase);

        if (!finishWord)
        {
            retValue = retValue.Remove(maxLength);
        }
        else if (allowLastWordOverflow)
        {
            int spaceIndex = retValue.IndexOf(" ", maxLength, StringComparison.CurrentCultureIgnoreCase);
            if (spaceIndex != -1)
            {
                retValue = retValue.Remove(spaceIndex);
            }
        }
        else if (lastSpaceIndex > -1)
        {
            retValue = retValue.Remove(lastSpaceIndex);
        }

        if (includeEllipsis && retValue.Length < valueToTruncate.Length)
        {
            retValue += "...";
        }
        return retValue;
    }
예제 #5
0
        public static string TruncateString(string valueToTruncate, int maxLength, TruncateOptions options)
        {
            bool includeEllipsis = (options & TruncateOptions.IncludeEllipsis) ==
                                   TruncateOptions.IncludeEllipsis;
            bool finishWord = (options & TruncateOptions.FinishWord) ==
                              TruncateOptions.FinishWord;
            bool allowLastWordOverflow =
                (options & TruncateOptions.AllowLastWordToGoOverMaxLength) ==
                TruncateOptions.AllowLastWordToGoOverMaxLength;
            string retValue = valueToTruncate;

            if (!finishWord)
            {
                if (retValue.Length < maxLength)
                {
                    return(retValue);
                }
                else
                {
                    retValue = retValue.Remove(maxLength);
                    String[] array = retValue.Split();
                    String   valor = array[array.Length - 1];
                    return(retValue = retValue.Replace(valor, "..."));
                }
            }
            else if (allowLastWordOverflow)
            {
                int spaceIndex = retValue.IndexOf(" ", maxLength,
                                                  StringComparison.CurrentCultureIgnoreCase);
                if (spaceIndex != -1)
                {
                    retValue = retValue.Remove(spaceIndex);
                }
            }
            return(retValue);
        }
예제 #6
0
        /// <summary>
        /// 截断字符串(可保留完整单词)
        /// </summary>
        /// <param name="valueToTruncate">需处理的字符串</param>
        /// <param name="maxLength">字符数</param>
        /// <param name="options">截断选项</param>
        /// <returns></returns>
        public static string TruncateString(this string valueToTruncate, int maxLength, TruncateOptions options)
        {
            if (valueToTruncate == null)
            {
                return("");
            }

            if (valueToTruncate.Length <= maxLength)
            {
                return(valueToTruncate);
            }

            var includeEllipsis = (options & TruncateOptions.IncludeEllipsis) ==
                                  TruncateOptions.IncludeEllipsis;
            var finishWord = (options & TruncateOptions.FinishWord) ==
                             TruncateOptions.FinishWord;
            var allowLastWordOverflow =
                (options & TruncateOptions.AllowLastWordToGoOverMaxLength) ==
                TruncateOptions.AllowLastWordToGoOverMaxLength;

            var retValue = valueToTruncate;

            if (includeEllipsis)
            {
                maxLength -= 1;
            }

            var lastSpaceIndex = retValue.LastIndexOf(" ",
                                                      maxLength, StringComparison.CurrentCultureIgnoreCase);

            if (!finishWord)
            {
                retValue = retValue.Remove(maxLength);
            }
            else if (allowLastWordOverflow)
            {
                var spaceIndex = retValue.IndexOf(" ",
                                                  maxLength, StringComparison.CurrentCultureIgnoreCase);
                if (spaceIndex != -1)
                {
                    retValue = retValue.Remove(spaceIndex);
                }
            }
            else if (lastSpaceIndex > -1)
            {
                retValue = retValue.Remove(lastSpaceIndex);
            }

            if (includeEllipsis && retValue.Length < valueToTruncate.Length)
            {
                retValue += "...";
            }
            return(retValue);
        }