StringExtensions Class
Provides extension methods for strings.
public static class StringExtensionsMethods
| Name | Description |
|---|---|
| string.ApplyEdits(IEnumerable<TextEdit>) | Applies a set of edits to a string, and returns the resulting string. |
| string.ComputeLineClipping(int, int) | Finds the first and the last lines of a string, and counts the lines between them. |
| string.ComputeLiteralLength(StringLiteralKind) | Computes the length of the representation of a string as a string literal. |
| string.ComputeQuotedLiteralLength() | Computes the length of the quoted literal representation of a string. |
| string.ComputeVerbatimLiteralLength() | Computes the length of the verbatim literal representation of a string. |
| string.ToClippedLiteral(StringLiteralKind, int, int, bool) | Builds and returns a string representing a given string as a C# string literal. If the string is longer than the sum of headLength, tailLength, and the length of an ellipsis (1 if useUnicodeEllipsis is true, 3 otherwise), then it is clipped, taking only the first headLength characters, followed by an ellipsis (either the Unicode character '\x2026' (…), or three dots ...) and the last tailLength characters. |
| string.ToClippedQuotedLiteral(int, int, bool) | Builds and returns a string representing a given string as a C# quoted string literal. If the string is longer than the sum of headLength, tailLength, and the length of an ellipsis (1 if useUnicodeEllipsis is true, 3 otherwise), then it is clipped, taking only the first headLength characters, followed by an ellipsis (either the Unicode character '\x2026' (…), or three dots ...) and the last tailLength characters. |
| string.ToClippedVerbatimLiteral(int, int, bool) | Builds and returns a string representing a given string as a C# verbatim string literal. If the string is longer than the sum of headLength, tailLength, and the length of an ellipsis (1 if useUnicodeEllipsis is true, 3 otherwise), then it is clipped, taking only the first headLength characters, followed by an ellipsis (either the Unicode character '\x2026' (…), or three dots ...) and the last tailLength characters. |
| string.ToLiteral(StringLiteralKind) | Builds and returns a string representing a given string as a C# string literal. |
| string.ToQuotedLiteral() | Builds and returns a string representing a given string as a C# quoted string literal. |
| string.ToVerbatimLiteral() | Builds and returns a string representing a given string as a C# verbatim string literal. |
string.ApplyEdits(IEnumerable<TextEdit>)
Applies a set of edits to a string, and returns the resulting string.
extension(string @this)
{
public string ApplyEdits(IEnumerable<TextEdit> edits)
}Parameters
thisstring- The
stringon which this method is called. editsIEnumerable<TextEdit>- The edits to apply. The
TextEdit.Startof every edit is an index inthis, whatever the other edits do.
Returns
string A string in which TextEdit.Text has replaced the characters of each edit; this itself, if edits is empty.
Exceptions
ArgumentNullExceptionthisisnull. - or -editsisnull.ArgumentOutOfRangeException- An edit has a negative
TextEdit.Start, aTextEdit.Startgreater than the length ofthis, a negativeTextEdit.Length, or characters to replace after the end ofthis. ArgumentException- Two edits overlap.
Remarks
The order of edits matters only for insertions at one index: their texts appear in the result in the order of edits.
Two edits overlap when both replace one character, or when one inserts a text between two characters that the other replaces.
An insertion at the first character that another edit replaces overlaps nothing: the inserted text precedes the replacing text in the result. An insertion just after the last character that another edit replaces follows the replacing text.
See also
string.ComputeLineClipping(int, int)
Finds the first and the last lines of a string, and counts the lines between them.
extension(string? @this)
{
public LineClipping ComputeLineClipping(int headLines, int tailLines)
}Parameters
thisstring?- The
stringon which this method is called. headLinesint- The number of lines to keep at the start of the string, when the string is clipped.
tailLinesint- The number of lines to keep at the end of the string, when the string is clipped.
Returns
LineClipping A LineClipping value, whose ranges refer to this.
Exceptions
ArgumentOutOfRangeExceptionheadLinesis less than 0. - or -tailLinesis less than 0.
Remarks
A line ends at a line feed (\n), at a carriage return (\r), at a CR-LF pair (\r\n), or at the end of the string, as in TextReader.ReadLine.
A line terminator at the end of the string does not start a new line.
A blank line is a line that is empty, or holds white-space characters alone, as Char.IsWhiteSpace defines them. This method ignores the blank lines at the start and at the end of the string, and the rest of these remarks does not count them among the lines of the string. A blank line between two other lines is not ignored.
null, the empty string, and a string of blank lines alone have no lines.
A string of more than (headLines + tailLines + 1) lines is clipped: LineClipping.Head holds its first headLines lines, and LineClipping.Tail its last tailLines lines. The reason for the +1 above is that a clipped text is usually displayed with a placeholder line (e.g., "--- more... ---") between head and tail.
When a string is not clipped, LineClipping.Head holds all of its lines, and LineClipping.Tail is empty.
This method copies no characters and allocates no memory.
See also
string.ComputeLiteralLength(StringLiteralKind)
Computes the length of the representation of a string as a string literal.
extension(string? @this)
{
public int ComputeLiteralLength(StringLiteralKind literalKind)
}Parameters
thisstring?- The
stringon which this method is called. literalKindStringLiteralKind- A
StringLiteralKindconstant specifying the kind of string literal that would be built.
Returns
int The length of the string that would result from calling string.ToLiteral on this with literalKind as last parameter.
Exceptions
ArgumentExceptionliteralKindis neitherStringLiteralKind.QuotednorStringLiteralKind.Verbatim.
string.ComputeQuotedLiteralLength()
Computes the length of the quoted literal representation of a string.
Parameters
thisstring?- The
stringon which this method is called.
Returns
int The length of the string that would result from calling string.ToQuotedLiteral on this.
string.ComputeVerbatimLiteralLength()
Computes the length of the verbatim literal representation of a string.
Parameters
thisstring?- The
stringon which this method is called.
Returns
int The length of the string that would result from calling string.ToVerbatimLiteral on this.
string.ToClippedLiteral(StringLiteralKind, int, int, bool)
Builds and returns a string representing a given string as a C# string literal. If the string is longer than the sum of headLength, tailLength, and the length of an ellipsis (1 if useUnicodeEllipsis is true, 3 otherwise), then it is clipped, taking only the first headLength characters, followed by an ellipsis (either the Unicode character '\x2026' (…), or three dots ...) and the last tailLength characters.
extension(string? @this)
{
public string ToClippedLiteral(StringLiteralKind literalKind, int headLength, int tailLength, bool useUnicodeEllipsis = false)
}Parameters
thisstring?- The
stringon which this method is called. literalKindStringLiteralKind- A
StringLiteralKindconstant specifying the kind of string literal to build. headLengthint- The number of characters to leave before clipping.
tailLengthint- The number of characters to leave after clipping.
useUnicodeEllipsisbooltrueto use a single Unicode character Ellipsis ('\x2026',…) to replace the clipped part;falseto use three dots (...) instead. Default isfalse.
Returns
string A newly-constructed string.
Exceptions
ArgumentExceptionliteralKindis neitherStringLiteralKind.QuotednorStringLiteralKind.Verbatim.ArgumentOutOfRangeExceptionheadLengthis less than 0. - or -tailLengthis less than 0.
See also
string.ToClippedQuotedLiteral(int, int, bool)
Builds and returns a string representing a given string as a C# quoted string literal. If the string is longer than the sum of headLength, tailLength, and the length of an ellipsis (1 if useUnicodeEllipsis is true, 3 otherwise), then it is clipped, taking only the first headLength characters, followed by an ellipsis (either the Unicode character '\x2026' (…), or three dots ...) and the last tailLength characters.
extension(string? @this)
{
public string ToClippedQuotedLiteral(int headLength, int tailLength, bool useUnicodeEllipsis = false)
}Parameters
thisstring?- The
stringon which this method is called. headLengthint- The number of characters to leave before clipping.
tailLengthint- The number of characters to leave after clipping.
useUnicodeEllipsisbooltrueto use a single Unicode character Ellipsis ('\x2026',…) to replace the clipped part;falseto use three dots (...) instead. Default isfalse.
Returns
string A newly-constructed string.
Exceptions
ArgumentOutOfRangeExceptionheadLengthis less than 0. - or -tailLengthis less than 0.
See also
string.ToClippedVerbatimLiteral(int, int, bool)
Builds and returns a string representing a given string as a C# verbatim string literal. If the string is longer than the sum of headLength, tailLength, and the length of an ellipsis (1 if useUnicodeEllipsis is true, 3 otherwise), then it is clipped, taking only the first headLength characters, followed by an ellipsis (either the Unicode character '\x2026' (…), or three dots ...) and the last tailLength characters.
extension(string? @this)
{
public string ToClippedVerbatimLiteral(int headLength, int tailLength, bool useUnicodeEllipsis = false)
}Parameters
thisstring?- The
stringon which this method is called. headLengthint- The number of characters to leave before clipping.
tailLengthint- The number of characters to leave after clipping.
useUnicodeEllipsisbooltrueto use a single Unicode character Ellipsis ('\x2026',…) to replace the clipped part;falseto use three dots (...) instead. Default isfalse.
Returns
string A newly-constructed string.
Exceptions
ArgumentOutOfRangeExceptionheadLengthis less than 0. - or -tailLengthis less than 0.
See also
string.ToLiteral(StringLiteralKind)
Builds and returns a string representing a given string as a C# string literal.
extension(string? @this)
{
public string ToLiteral(StringLiteralKind literalKind)
}Parameters
thisstring?- The
stringon which this method is called. literalKindStringLiteralKind- A
StringLiteralKindconstant specifying the kind of string literal to build.
Returns
string A newly-constructed string.
Exceptions
ArgumentExceptionliteralKindis neitherStringLiteralKind.QuotednorStringLiteralKind.Verbatim.
See also
string.ToQuotedLiteral()
Builds and returns a string representing a given string as a C# quoted string literal.
string.ToVerbatimLiteral()
Builds and returns a string representing a given string as a C# verbatim string literal.