Gorgon
Show / Hide Table of Contents

Class GorgonStringFormattingExtension

Extension methods to provide formatting on the string type and additional functionality for the StringBuilder type.

Inheritance
object
GorgonStringFormattingExtension
Inherited Members
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
Namespace: Gorgon.Core
Assembly: Gorgon.Core.dll
Syntax
public static class GorgonStringFormattingExtension

Methods

| Edit this page View Source

Ellipses(string, int, bool, int, params object[])

Function to shorten a string and prefix or postfix an ellipses to the string.

Declaration
public static string Ellipses(this string theString, int maxWidth, bool prefix, int tabSpaceCount, params object[] values)
Parameters
Type Name Description
string theString

The string to shorten.

int maxWidth

The maximum width, in characters, of the string.

bool prefix

true to put the ellipses on the beginning of the string, or false to put on the end.

int tabSpaceCount

Number of spaces to insert for the tab character.

object[] values

Values to put into the string placeholders.

Returns
Type Description
string

The shortened string with ellipses.

Remarks

This will output a shorted version of theString and will prefix or postfix an ellipses '...' to it.

This function will do formatting on the string, such as tab replacement and split the newline characters into new lines before processing and will replace variable values. This way it will get the true length of the string.

If the maxWidth is less than the length of a line, then the ellipses will be added to the string, and the string will be truncated to the max width plus the length of the ellipses, otherwise it will just output the line.

If the maxWidth is less than the length of the string plus the ellipses length, then just the first few characters (up to the width specified by max width) will be output without ellipses.

Specifying true for prefix will put the ellipses on the beginning of the string, false will put it on the end as a suffix.

| Edit this page View Source

Ellipses(string, int, bool, params object[])

Function to shorten a string and prefix or postfix an ellipses to the string.

Declaration
public static string Ellipses(this string theString, int maxWidth, bool prefix, params object[] values)
Parameters
Type Name Description
string theString

The string to shorten.

int maxWidth

The maximum width, in characters, of the string.

bool prefix

true to put the ellipses on the beginning of the string, or false to put on the end.

object[] values

Values to put into the string placeholders.

Returns
Type Description
string

The shortened string with ellipses.

Remarks

This will output a shorted version of theString and will prefix or postfix an ellipses '...' to it.

This function will do formatting on the string, such as tab replacement and split the newline characters into new lines before processing and will replace variable values. This way it will get the true length of the string.

If the maxWidth is less than the length of a line, then the ellipses will be added to the string, and the string will be truncated to the max width plus the length of the ellipses, otherwise it will just output the line.

If the maxWidth is less than the length of the string plus the ellipses length, then just the first few characters (up to the width specified by max width) will be output without ellipses.

Specifying true for prefix will put the ellipses on the beginning of the string, false will put it on the end as a suffix.

| Edit this page View Source

Ellipses(string, int, params object[])

Function to shorten a string and prefix an ellipses to the string.

Declaration
public static string Ellipses(this string theString, int maxWidth, params object[] values)
Parameters
Type Name Description
string theString

The string to shorten.

int maxWidth

The maximum width, in characters, of the string.

object[] values

Values to put into the string placeholders.

Returns
Type Description
string

The shortened string with ellipses.

Remarks

This overload will output a shorted version of theString and will prefix an ellipses '...' to it.

This function will do formatting on the string, such as tab replacement and split the newline characters into new lines before processing and will replace variable values. This way it will get the true length of the string.

If the maxWidth is less than the length of a line, then the ellipses will be added to the string, and the string will be truncated to the max width plus the length of the ellipses, otherwise it will just output the line.

If the maxWidth is less than the length of the string plus the ellipses length, then just the first few characters (up to the width specified by max width) will be output without ellipses.

| Edit this page View Source

GetByteCount(string, bool, Encoding)

Function to return the length of a string, in bytes, with the specified Encoding.

Declaration
public static int GetByteCount(this string value, bool includeLength, Encoding encoding = null)
Parameters
Type Name Description
string value

The string to measure.

bool includeLength

true to include the number of bytes for the encoded length, false to exclude.

Encoding encoding

[Optional] The encoding for the string.

Returns
Type Description
int

The length of the string, in bytes.

Remarks

If the includeLength parameter is true, then the return value will also include the number of 7-bit bytes required to encode the length of the string.

If the encoding parameter is null, then UTF-8 encoding will be used.

| Edit this page View Source

GetLines(string)

Function to break a string into an array of strings based on the newline control characters present in the text.

Declaration
public static string[] GetLines(this string renderText)
Parameters
Type Name Description
string renderText

The text to evaluate.

Returns
Type Description
string[]

The array of strings representing a single line per newline control character.

| Edit this page View Source

GetLines(StringBuilder, ref string[])

Function to break a string into an array of strings based on the newline control characters present in the text.

Declaration
public static void GetLines(this StringBuilder text, ref string[] buffer)
Parameters
Type Name Description
StringBuilder text

The text to evaluate.

string[] buffer

The array of strings representing a single line per newline control character.

| Edit this page View Source

IndexOf(StringBuilder, char)

Function to find the index of a character in a StringBuilder.

Declaration
public static int IndexOf(this StringBuilder theString, char character)
Parameters
Type Name Description
StringBuilder theString

The string to search.

char character

Character to search for.

Returns
Type Description
int

The index of the character, or -1 if not found.

Exceptions
Type Condition
ArgumentNullException

Thrown when the theString parameter is null.

| Edit this page View Source

IndexOf(StringBuilder, string, int, StringComparison)

Function to find the index of a character in a StringBuilder.

Declaration
public static int IndexOf(this StringBuilder theString, string characters, int startIndex = 0, StringComparison comparison = StringComparison.InvariantCulture)
Parameters
Type Name Description
StringBuilder theString

The string to search.

string characters

Characters to search for.

int startIndex

[Optional] The index to start searching from.

StringComparison comparison

[Optional] One of the enumeration values that specifies the rules for the search.

Returns
Type Description
int

The index of the character, or -1 if not found.

Exceptions
Type Condition
ArgumentNullException

Thrown when the theString parameter is null.

| Edit this page View Source

IsWhiteSpaceOrEmpty(string)

Function to determine if the string is composed of whitespace, or is empty.

Declaration
public static bool IsWhiteSpaceOrEmpty(this string value)
Parameters
Type Name Description
string value

The string to examine.

Returns
Type Description
bool

true if the string is composed of whitespace, or is empty. false if not.

Exceptions
Type Condition
ArgumentNullException

Thrown if the value parameter is null.

| Edit this page View Source

LastIndexOf(StringBuilder, char)

Function to find the last index of a character in a StringBuilder.

Declaration
public static int LastIndexOf(this StringBuilder theString, char character)
Parameters
Type Name Description
StringBuilder theString

The string to search.

char character

Character to search for.

Returns
Type Description
int

The index of the character, or -1 if not found.

Exceptions
Type Condition
ArgumentNullException

Thrown when the theString parameter is null.

| Edit this page View Source

LastIndexOf(StringBuilder, string, StringComparison)

Function to find the last index of a character in a StringBuilder.

Declaration
public static int LastIndexOf(this StringBuilder theString, string characters, StringComparison comparison = StringComparison.InvariantCulture)
Parameters
Type Name Description
StringBuilder theString

The string to search.

string characters

Characters to search for.

StringComparison comparison

[Optional] One of the enumeration values that specifies the rules for the search.

Returns
Type Description
int

The index of the character, or -1 if not found.

Exceptions
Type Condition
ArgumentNullException

Thrown when the theString parameter is null.

| Edit this page View Source

ToStringWithDeclaration(XDocument)

Function to convert a Linq to XML document into a string including a declaration element.

Declaration
public static string ToStringWithDeclaration(this XDocument document)
Parameters
Type Name Description
XDocument document

The document to convert.

Returns
Type Description
string

The XML document serialized as a string.

Remarks

This method addresses a shortcoming of the Linq-to-XML XDocument.ToString() method. The original method leaves out the declaration element when converted to a string.

  • Edit this page
  • View Source
In this article
Back to top Copyright 2023 - Licensed under the MIT license by Michael Winsor (Tape_Worm).
Send comments on this topic to the author