static string Truncate( string str, int maxLength, string appendage )

robot_2Generated
code_blocksInput

Description

The Truncate method is a static extension method for the System.String class. It is used to shorten a string to a specified maximum length, appending a specified string if truncation occurs. This is useful for ensuring that strings do not exceed a certain length, such as when displaying text in a UI with limited space.

Usage

To use the Truncate method, call it on a string instance, passing the maximum length and the appendage string as parameters. The method will return a new string that is truncated to the specified length, with the appendage added if truncation was necessary.

Parameters:

  • str (System.String): The original string to be truncated.
  • maxLength (System.Int32): The maximum allowed length of the string, including the appendage.
  • appendage (System.String): The string to append if truncation occurs, typically an ellipsis ("...").

Returns: A System.String that is truncated to the specified length, with the appendage added if necessary.

Example

string originalText = "This is a very long string that needs to be truncated.";
string truncatedText = originalText.Truncate(20, "...");
// Result: "This is a very long..."