How can I truncate my lines with "..." if they are too long?

Hope someone has a good idea. I have lines like this:

abcdefg abcde abc 

I need them to be convinced to show this if it is more than the given length:

 abc .. abc .. abc 

Is there any simple C # code I can use for this?

+71
string c #
Jul 17 '11 at 15:30
source share
11 answers

Here is the logic wrapped in the extension method:

 public static string Truncate(this string value, int maxChars) { return value.Length <= maxChars ? value : value.Substring(0, maxChars) + "..."; } 

Using:

 var s = "abcdefg"; Console.WriteLine(s.Truncate(3)); 
+122
Jul 17 '11 at 15:38
source share
 public string TruncString(string myStr, int THRESHOLD) { if (myStr.Length > THRESHOLD) return myStr.Substring(0, THRESHOLD) + "..."; return myStr; } 

Ignore the naming convention just in case it really needs the THRESHOLD variable or is always the same size.

As an alternative

 string res = (myStr.Length > THRESHOLD) ? myStr.Substring(0, THRESHOLD) + ".." : myStr; 
+7
Jul 17 2018-11-17T00:
source share

All are very good answers, but to clear it up a bit, don't break your line in the middle of a word.

 private string TruncateForDisplay(this string value, int length) { if (string.IsNullOrEmpty(value)) return string.Empty; var returnValue = value; if (value.Length > length) { var tmp = value.Substring(0, length) ; returnValue = tmp.Substring(0, tmp.LastIndexOf(' ') ) + " ..."; } return returnValue; } 
+4
Dec 19 '18 at 1:51
source share

There is no built-in method in the .NET Framework, but it is a very simple way to write yourself. Here are the steps, try to do it yourself and let us know what you come up with.

  • Create a method, possibly an extension method public static void TruncateWithEllipsis(this string value, int maxLength)

  • Verify that the passed value maxLength using the Length property . If value not greater than maxLength , just return value .

  • If we did not return the passed value as is, then we know that we need to truncate. Therefore, we need to get a smaller section of the string using the SubString method. This method will return a smaller section of the string based on the given start and end values. The end position is what was passed by the maxLength parameter, so use this.

  • Return an extra line section plus an ellipsis.

A great exercise for the future would be updating the method and breaking it only after full words. You can also create an overload to indicate how you want to show that the line has been truncated. For example, a method might return "(click for more)" instead of "..." if your application is configured for more details by clicking.

+3
Jul 17 '11 at 15:37
source share

Here is a version that takes into account the length of the ellipses:

  public static string Truncate(this string value, int maxChars) { const string ellipses = "..."; return value.Length <= maxChars ? value : value.Substring(0, maxChars - ellipses.Length) + ellipses; } 
+3
Jan 03 '18 at 20:10
source share

Code behind:

 string shorten(sting s) { //string s = abcdefg; int tooLongInt = 3; if (s.Length > tooLongInt) return s.Substring(0, tooLongInt) + ".."; return s; } 

Markup:

 <td><%= shorten(YOUR_STRING_HERE) %></td> 
+2
Jul 17 2018-11-17T00:
source share

It might be better to implement a method for this purpose:

 string shorten(sting yourStr) { //Suppose you have a string yourStr, toView and a constant value string toView; const int maxView = 3; if (yourStr.Length > maxView) toView = yourStr.Substring(0, maxView) + " ..."; // all you have is to use Substring(int, int) .net method else toView = yourStr; return toView; } 
+2
Jul 17 2018-11-17T00:
source share
 string s = "abcdefg"; if (s.length > 3) { s = s.substring(0,3); } 

You can use the substring function.

+1
Jul 17 '11 at 15:33
source share

I found this question after searching for "C # truncated ellipsis." Using various answers, I created my own solution with the following functions:

  1. Extension method
  2. Add ellipsis
  3. Make the ellipsis optional
  4. Make sure the line is not empty and not empty before trying to truncate it.

     public static class StringExtensions { public static string Truncate(this string value, int maxLength, bool addEllipsis = false) { // Check for valid string before attempting to truncate if (string.IsNullOrEmpty(value)) return value; // Proceed with truncating var result = string.Empty; if (value.Length > maxLength) { result = value.Substring(0, maxLength); if (addEllipsis) result += "..."; } else { result = value; } return result; } } 

I hope this helps someone else.

+1
Jan 16 '18 at 15:28
source share

Of course, here is a sample code:

 string str = "abcdefg"; if (str.Length > X){ str = str.Substring(0, X) + "..."; } 
0
Jul 17 '11 at 15:33
source share

I have this problem recently. I saved the β€œstatus” message in the nvarcharMAX database field, which is 4000 characters. However, my status messages accumulated and fell into the exception.

But this was not a simple case of truncation, since arbitrary truncation could lead to the loss of part of the status message, so I really needed to "truncate" the agreed part of the line.

I solved the problem by converting the string to a string array, deleting the first element, and then restoring the string. Here is the code ("CurrentStatus" is a string containing data) ...

  if (CurrentStatus.Length >= 3750) { // perform some truncation to free up some space. // Lets get the status messages into an array for processing... // We use the period as the delimiter, then skip the first item and re-insert into an array. string[] statusArray = CurrentStatus.Split(new string[] { "." }, StringSplitOptions.None) .Skip(1).ToArray(); // Next we return the data to a string and replace any escaped returns with proper one. CurrentStatus = (string.Join(".", statusArray)) .Replace("\\r\\n", Environment.NewLine); } 

Hope this helps someone.

0
Jan 29 '18 at 16:20
source share



All Articles