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 += "…"; } return(retValue); }
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); }
internal TruncateMessage Populate( NpgsqlLogSequenceNumber walStart, NpgsqlLogSequenceNumber walEnd, DateTime serverClock, TruncateOptions options, uint[] relationIds) { base.Populate(walStart, walEnd, serverClock); Options = options; RelationIds = relationIds; return(this); }
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; }
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); }
/// <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); }