Class StringExtensions
This is a class of extension methods for performing common tasks not provided by the System.String class. All but the four Pad methods are derived from long established routines in companion class StringTricks.
Just as importing the System.Linq namespace makes its generic extension methods visible, importing the root WizardWrx namespace, accompanied by a reference to WizardWrx.Core, makes these methods visible on every instance of System.string.
Rather than create ane entirely new class to support one small method, I extended this class to cover RenderEvenWhenNull, even though it is a generic method.
Inheritance
Namespace: WizardWrx
Assembly: WizardWrx.Core.dll
Syntax
public static class StringExtensions : object
Fields
| Improve this Doc View SourceDEFAULT_TOKEN_DELM
Default token terminator string used by the version of public static method, MakeToken, which takes one argument.
Declaration
public const string DEFAULT_TOKEN_DELM = null
Field Value
Type | Description |
---|---|
System.String |
Methods
| Improve this Doc View SourceAppendFullStopIfMissing(String)
Unless the last character of the input string is a period (full stop), append one to the returned string.
Declaration
public static string AppendFullStopIfMissing(this string pstrInput)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrInput | Specify the input string to evaluate and edit as needed. |
Returns
Type | Description |
---|---|
System.String | The input string is returned with a period appended to it. If it already has one, the input string is returned unchanged. |
ApplyFixups(String, StringFixups.StringFixup[])
Call this extension method to perform a one-off string transformation.
Declaration
public static string ApplyFixups(this string pstrIn, StringFixups.StringFixup[] pafixupPairs)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrIn | Input string to transform by applying |
StringFixups.StringFixup[] | pafixupPairs | Array of StringFixup objects to apply to |
Returns
Type | Description |
---|---|
System.String | The |
ArrayOfOne(Char)
Return a one-element array containing the input character, for use as input to the Split method of the System.string class.
Declaration
public static char[] ArrayOfOne(this char pchrTheCharacter)
Parameters
Type | Name | Description |
---|---|---|
System.Char | pchrTheCharacter | Specify the character to be copied into an array of one. |
Returns
Type | Description |
---|---|
System.Char[] | The return value is an array of one element, ready to feed to the string.split method, or anything else that needs an array of one character. |
ArrayOfOne(String)
Return a one-element array containing the input string, for use as input to the Split method of the System.string class.
Declaration
public static string[] ArrayOfOne(this string pstrTheString)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrTheString | Specify the string to be copied into an array of one. |
Returns
Type | Description |
---|---|
System.String[] | The return value is an array of one element, ready to feed to the string.split method, or anything else that needs an array of one string. |
CapitalizeWords(String)
Return the input string with each word capitalized.
Declaration
public static string CapitalizeWords(this string pstr)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstr | The string to process is implicitly passed in by this extension method. |
Returns
Type | Description |
---|---|
System.String | The input string is returned with each of its words capitalized. If the string is already capitalized, this has no effect. Subsequent letters are coerced to lower case. |
Chop(String, Boolean)
Return a new string with the terminal newline, if present, removed.
Declaration
public static string Chop(this string pstrIn, bool pfIncludeNBSP = false)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrIn | Specify the string to be chopped. |
System.Boolean | pfIncludeNBSP | Specify TRUE to add the nonbreaking space to the chopping block, thereby saving a call to ChopNBSP to handle the third case. Since this argument is made optional and defaulted to False, this routine is fully baclwards compatible. |
Returns
Type | Description |
---|---|
System.String | The chopped string is returned, minus its newline if it contained one. This method treats all newlines equally, meaning that any of the following items is treated as a newline. |
ChopNBSP(String)
When the last character of string pstrStringToTrim
is a nonbreaking space, remove it. Otherwise, return a copy of the
input string.
Declaration
public static string ChopNBSP(this string pstrStringToTrim)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrStringToTrim | This string is anticipated to end with an unwanted nonbreaking space. Note that implemtnation as an extension method makes this argument both implicit and hidden from the IDE. The empty string is a degenerate case, which returns another empty string. A null reference returns another null reference. Give me nothing and I return nothing. |
Returns
Type | Description |
---|---|
System.String | The return value is the input string, minus its last character when the last character is a nonbreaking space. Otherwise, the return value is a copy of the original string. |
CountCharacterOccurrences(String, Char)
Strangely, the String class is missing an important static method to count occurrences of a specified character within a string. This is that missing method.
Declaration
public static int CountCharacterOccurrences(this string pstrSource, char pchrToCount)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrSource | Specify the string in which to count occurrences of substring pstrToCount. If pstrSource is null or empty, the method returns zero. Since this is an extension method, pstrIn is supplied by the BCL when it binds this method to an instance of System.string. |
System.Char | pchrToCount | Specify the substring to count in string pstrSource. An empty string causes the method to return MagicNumbers.STRING_INDEXOF_NOT_FOUND, or -1. |
Returns
Type | Description |
---|---|
System.Int32 | The return value is the number of times, if any, that string pstrToCount occurs in string pstrSource, or MagicNumbers.STRING_INDEXOF_NOT_FOUND (-1) if pstrToCount is either null reference or empty the empty string. |
Remarks
This method implements the only overload of the string.IndexOf method that takes a character as its second argument for which I have yet to make use. There is currently no implementation of the overload that stops looking after scanning count characters, nor do I have immediate plans to implement one, though it wouldn't be hard.
This method uses the same algorithm as CountSubstrings, except that its second argument is a single character, which CANNOT be the NULL character.
CountSubstrings(String, String)
Strangely, the String class is missing an important static method to count substrings within a string. This is that missing method.
Declaration
public static int CountSubstrings(this string pstrSource, string pstrToCount)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrSource | Specify the string in which to count occurrences of substring pstrToCount. If pstrSource is null or empty, the method returns zero. Since this is an extension method, pstrIn is supplied by the BCL when it binds this method to an instance of System.string. |
System.String | pstrToCount | Specify the substring to count in string pstrSource. An empty string causes the method to return MagicNumbers.STRING_INDEXOF_NOT_FOUND, or -1. |
Returns
Type | Description |
---|---|
System.Int32 | The return value is the number of times, if any, that string pstrToCount occurs in string pstrSource, or MagicNumbers.STRING_INDEXOF_NOT_FOUND (-1) if pstrToCount is either null reference or empty the empty string. |
Remarks
This method implements the only overloads of the string.IndexOf method that takes a string as its second argument for which I have yet to make use. There is currently no implementation of the overloads that stops looking after scanning count characters, nor do I have immediate plans to implement one, though it wouldn't be hard.
This method is implemented by calling the second overload, which has an additional argument through which to specify a string comparison algorithm, since the algorithms required to implement both are otherwise identical.
CountSubstrings(String, String, StringComparison)
Strangely, the String class is missing an important static method to count substrings within a string. This is that missing method.
Declaration
public static int CountSubstrings(this string pstrSource, string pstrToCount, StringComparison penmComparisonType)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrSource | Specify the string in which to count occurrences of substring pstrToCount. If pstrSource is null or empty, the method returns zero. Since this is an extension method, pstrIn is supplied by the BCL when it binds this method to an instance of System.string. |
System.String | pstrToCount | Specify the substring to count in string pstrSource. The empty string causes the method to return MagicNumbers.STRING_INDEXOF_NOT_FOUND, or -1. |
StringComparison | penmComparisonType | Specify a member of the System.StringComparison enumeration, which defines the rules for performing the comparison. |
Returns
Type | Description |
---|---|
System.Int32 | The return value is the number of times, if any, that string pstrToCount occurs in string pstrSource, zero if pstrSource is a null reference or the empty string, or MagicNumbers.STRING_INDEXOF_NOT_FOUND (-1) if pstrToCount is either a null reference or the empty string. |
Remarks
This method implements the only overloads of the string.IndexOf method that takes a string as its second argument for which I have yet to make use. There is currently no implementation of the overloads that stops looking after scanning count characters, nor do I have immediate plans to implement one, though it wouldn't be hard.
CountUnresolvedEnvironmentStrings(String)
Scan a string for environment string delimiter characters left behind by an environment string expansion.
Declaration
public static int CountUnresolvedEnvironmentStrings(this string pstrInput)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrInput | Specify a string that has had its environment strings expanded. |
Returns
Type | Description |
---|---|
System.Int32 | The return value is the count of remaining environment string delimiters. Please see the remarks for additional information. |
Remarks
There are two reasons that such delimiters might be left behind.
The input string contains environment strings that have no like named strings in the environment block that belongs to the process.
The input string contains a malformed string that is missing one of its delimiting tokens.
This routine wraps CountSubstrings, supplying the required token, which is defined as public character constant in SpecialCharacters, a sibling class. Since you could as well call that routine directly, this routine is syntactic sugar.
See Also
| Improve this Doc View SourceCSVSafe4EmbeddedDoubleQuote(String)
Replace double quotation marks embedded in a field that is about to be inserted into a CSV string with pairs of double quotation marks.
Declaration
public static string CSVSafe4EmbeddedDoubleQuote(this string pstrIn)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrIn | Specify the string to evaluate, which may, or may not, have embedded double quotation marks. Since this is an extension method, pstrIn is supplied by the BCL when it binds this method to an instance of System.string. |
Returns
Type | Description |
---|---|
System.String | The return value is a copy of the string in which embedded double quotation marks are replaced by pairs of double quotation marks, as specified in RFC-4180. |
EncloseInChar(String, Char)
Append a specified character to both ends of a string, unless it is already present.
Declaration
public static string EncloseInChar(this string pstrIn, char pchrEnd)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrIn | Specify the string to evaluate, which may, or may not, end with the character specified in pchrEnd. Since this is an extension method, pstrIn is supplied by the BCL when it binds this method to an instance of System.string. |
System.Char | pchrEnd | Specify the character to append, if absent. |
Returns
Type | Description |
---|---|
System.String | The return value is a new string that is guaranteed to have exactly one of the character specified in pchrEnd at each end. |
EnsureFirstCharIs(String, Char)
Return a string that is guaranteed to start with a specified character.
Declaration
public static string EnsureFirstCharIs(this string pstrInput, char pchrMustBeFirst)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrInput | |
System.Char | pchrMustBeFirst | Ensure that the first character in the string is this one. |
Returns
Type | Description |
---|---|
System.String | The returned string is guaranteed to start with the specified character. |
EnsureLastCharIs(String, Char)
Return a string that is guaranteed to end with a specified character.
Declaration
public static string EnsureLastCharIs(this string pstrInput, char pchrMustBeLast)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrInput | |
System.Char | pchrMustBeLast | Ensure that the last character in the string is this one. |
Returns
Type | Description |
---|---|
System.String | The returned string is guaranteed to end with the specified character. |
EnumerateSubstringPositions(String, String)
Return an array of integers, each a position of substring
pstrToCount
in string pstrSource
.
Declaration
public static int[] EnumerateSubstringPositions(this string pstrSource, string pstrToCount)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrSource | Specify the string in which to count occurrences of substring pstrToCount. If pstrSource is null or empty, the method returns zero. Since this is an extension method, pstrIn is supplied by the BCL when it binds this method to an instance of System.string. |
System.String | pstrToCount | Specify the substring to count in string pstrSource. The empty string causes the method to return MagicNumbers.STRING_INDEXOF_NOT_FOUND, or -1. |
Returns
Type | Description |
---|---|
System.Int32[] | When the method succeeds, its return value is an array of integers,
each of which is the position of an occurrence of |
EnumerateSubstringPositions(String, String, StringComparison)
Return an array of integers, each a position of substring
pstrToCount
in string pstrSource
.
Declaration
public static int[] EnumerateSubstringPositions(this string pstrSource, string pstrToCount, StringComparison penmComparisonType)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrSource | Specify the string in which to count occurrences of substring pstrToCount. If pstrSource is null or empty, the method returns zero. Since this is an extension method, pstrIn is supplied by the BCL when it binds this method to an instance of System.string. |
System.String | pstrToCount | Specify the substring to count in string pstrSource. The empty string causes the method to return MagicNumbers.STRING_INDEXOF_NOT_FOUND, or -1. |
StringComparison | penmComparisonType | Specify a member of the System.StringComparison enumeration, which defines the rules for performing the comparison. |
Returns
Type | Description |
---|---|
System.Int32[] | When the method succeeds, its return value is an array of integers,
each of which is the position of an occurrence of |
EnumFromString<T>(String)
Convert a string to the equivalent instance of a specified enumeration type.
Declaration
public static T EnumFromString<T>(this string pstrEnumAsString)
where T : Enum
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrEnumAsString | Specify the string to convert. |
Returns
Type | Description |
---|---|
T | If the function succeeds, the return value is a member of the specified enumeration. |
Type Parameters
Name | Description |
---|---|
T | The specified type must be a valid Enum type. The method infers the type from the value specified in angle brackets following the method name in the invocation. |
Remarks
This method requires version 7.3 or later of the C# compiler, for which the project configuration contains a setting.
ExtractBetweenIndexOfs(String, Int32, Int32)
Extract the substring bounded by the characters at either end of it.
Declaration
public static string ExtractBetweenIndexOfs(this string pstrWholeString, int pintPosBegin, int pintPosEnd)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrWholeString | Specify the string from which to extract the substring. Since this is an extension method, the CLR supplies this argument when it binds the method to an instance of the System.string class. |
System.Int32 | pintPosBegin | Specify the position, as it would be reported by IndexOf, of the character that bounds the left end of the desired substring. |
System.Int32 | pintPosEnd | Specify the position, as it would be reported by IndexOf, of the character that bounds the right end of the desired substring. |
Returns
Type | Description |
---|---|
System.String | The returned substring begins with the character immediately to the right of the left hand bounding character, and ending with the last character before the right hand bounding character. |
ExtractBetweenIndexOfs(String, String, Int32, Int32)
Extract the substring bounded by the characters at either end of it.
Declaration
public static string ExtractBetweenIndexOfs(this string pstrWholeString, string pstrLeftMarker, int pintPosBegin, int pintPosEnd)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrWholeString | Specify the string from which to extract the substring. Since this is an extension method, the CLR supplies this argument when it binds the method to an instance of the System.string class. |
System.String | pstrLeftMarker | This overload handles the case where the left boundary is bounded by a string. The method needs a copy of the string in order to find the true beginning of the substring to extract, and to compute its length. |
System.Int32 | pintPosBegin | This integer is the position, given by IndexOf, of the character that bounds the left end of the desired substring. |
System.Int32 | pintPosEnd | This integer is the position, given by IndexOf, of the character that bounds the right end of the desired substring. |
Returns
Type | Description |
---|---|
System.String | The returned substring begins with the character immediately to the right of the left hand bounding character, and ending with the last character before the right hand bounding character. Inputs and computed values are thoroughly sanity checked to prevent run-time exceptions. If anything is out of order, an empty string is returned. |
ExtractBoundedSubstrings(String, Char)
Extract a substring that is bounded by a character. See Remarks.
Declaration
public static string ExtractBoundedSubstrings(this string pstrWholeString, char pchrBoundingCharacter)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrWholeString | The substring is extracted from this string. |
System.Char | pchrBoundingCharacter | Specify the bounding character. Please see Remarks. |
Returns
Type | Description |
---|---|
System.String | The return value is the desired substring, without its bounding characters. See Remarks. |
Remarks
The left and right ends must be bounded by the same character. To extract a string bounded on each end by a different character, use the next overload.
Inputs and computed values are thoroughly sanity checked to prevent run-time exceptions. If anything is out of order, an empty string is returned.
ExtractBoundedSubstrings(String, Char, Char)
Extract a substring that is bounded by a character. See Remarks.
Declaration
public static string ExtractBoundedSubstrings(this string pstrWholeString, char pchrLeftBound, char pchrRightBound)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrWholeString | The substring is extracted from this string. |
System.Char | pchrLeftBound | Specify the character that marks the left end of the string. See Remarks. |
System.Char | pchrRightBound | Specify the character that marks the right end of the string. See Remarks. |
Returns
Type | Description |
---|---|
System.String | The returned substring begins with the character immediately to the right of the left hand bounding substring, and ending with the last character before the right hand bounding substring. |
Remarks
The left and right ends are expected to be bounded by different characters. To extract a string bounded on each end by the same character, use the previous overload.
Inputs and computed values are thoroughly sanity checked to prevent run-time exceptions. If anything is out of order, the empty string is returned.
ExtractBoundedSubstrings(String, String, String)
Extract a substring that is bounded by a pair of substrings. See Remarks.
Declaration
public static string ExtractBoundedSubstrings(this string pstrWholeString, string pstrLeftBound, string pstrRightBound)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrWholeString | Specify the string from which to extract the bounded substring. |
System.String | pstrLeftBound | Specify the substring that marks the left end of the string. See Remarks. |
System.String | pstrRightBound | Specify the substring that marks the right end of the string. See Remarks. |
Returns
Type | Description |
---|---|
System.String | The returned substring begins with the character immediately to the right of the left hand bounding substring, and ending with the last character before the right hand bounding substring. |
Remarks
The left and right ends are expected to be bounded by different substrings. To extract a string bounded on each end by the same substring, use the same value for the second and third arguments.
Inputs and computed values are thoroughly sanity checked to prevent run-time exceptions. If anything is out of order, an empty string is returned.
GuardStringIfNeeded(String, Char, Char)
Return a new string that is enclosed in a specified quoting character, defaulting to a double quote, if the input string contains a specified delimiter character, defaulting to a comma.
Declaration
public static string GuardStringIfNeeded(this string pstrThisString, char pchrDelimiter = null, char pchrGuard = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrThisString | Pass in a reference to a string, which may be a null reference or a pointer to the empty string. |
System.Char | pchrDelimiter | Pass in the delimiter character, which cannot be the NULL character,
and must differ from the |
System.Char | pchrGuard | Pass in the guard character (the quoting character), which cannot be
the NULL character, and must differ from the
|
Returns
Type | Description |
---|---|
System.String | When |
LeftPadNChars(String, Int32)
Left pad the string with a specified number of spaces.
Declaration
public static string LeftPadNChars(this string pstrPadThisString, int paddingCharacterCount)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrPadThisString | This argument is supplied by the framework when it binds the method to an instance of the System.String class. |
System.Int32 | paddingCharacterCount | Specify the number of space characters to add on the left end of the string. Please see the Remarks for important details. |
Returns
Type | Description |
---|---|
System.String | The input string is padded on the left with the specified number of space characters. Please see the Remarks for important details. |
Remarks
These methods compensate for the completely logical, if unexpected, behavior of the native PadLeft and PadRight methods on the System.string class. Their objective is to guarantee that the new string is truly padded with a specific number of characters.
The names of the visible arguments differ from my usual Hungarian naming convention so that they conform to the naming convention of the Base Class Library methods that they wrap.
LeftPadNChars(String, Int32, Char)
Left pad the string with a specified number of some arbitrary character.
Declaration
public static string LeftPadNChars(this string pstrPadThisString, int paddingCharacterCount, char paddingChar)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrPadThisString | This argument is supplied by the framework when it binds the method to an instance of the System.String class. |
System.Int32 | paddingCharacterCount | Specify the number of arbitrary characters to add on the left end of the string. Please see the Remarks for important details. |
System.Char | paddingChar | Specify the arbitrary character with which the string is to be padded. |
Returns
Type | Description |
---|---|
System.String | The input string is padded on the left with the specified number of the specified arbitrary character. Please see the Remarks for important details. |
Remarks
These methods compensate for the completely logical, if unexpected, behavior of the native PadLeft and PadRight methods on the System.string class. Their objective is to guarantee that the new string is truly padded with a specific number of characters.
The names of the visible arguments differ from my usual Hungarian naming convention so that they conform to the naming convention of the Base Class Library methods that they wrap.
MakeToken(String)
Given a string containing the name of a form control (field) or other token, create its place holder token.
Declaration
public static string MakeToken(this string pstrFieldName)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrFieldName | String containing the name of the token. |
Returns
Type | Description |
---|---|
System.String | String containing the text of the corresponding template text place holder. See Remarks. |
Remarks
The string is constructed by appending a standard token delimiter, which is a pair of dollar signs, to each end of the string.
The token is exposed as a static property, DEFAULT_TOKEN_DELM.
MakeToken(String, String)
Given a string containing the name of a form control (field) or other token, and another string containing a static token, create its place holder token.
Declaration
public static string MakeToken(this string pstrFieldName, string pstrTokenEnds)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrFieldName | Specify the string containing the name of the token. |
System.String | pstrTokenEnds | Specify the string to attach to both ends of the string specified by pstrFieldName to generate the token. |
Returns
Type | Description |
---|---|
System.String | The string is constructed by appending the token delimiter specified in argument pstrTokenEnds to both ends of the string specified in argument pstrFieldName. |
OldMacLineEndings(String)
Replace a string that may contain mixed or unwanted line endings with a string that contains only the expected line ending type.
Declaration
public static string OldMacLineEndings(this string pstrSource)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrSource | String in which to replace line endings |
Returns
Type | Description |
---|---|
System.String | A copy of |
ParseCommentInHTMLComment(String)
Extract parameters, expressed as key-value pairs, from a standard HTML comment.
Declaration
public static NameValueCollection ParseCommentInHTMLComment(this string pstrInput)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrInput | String containing a well formed HTML comment, surrounding the key-value pairs. If the string is not a well formed HTML comment, with a single space between the comment delimiters and the body, or the string is null or empty, the returned collection is empty. |
Returns
Type | Description |
---|---|
NameValueCollection | A NameValueCollection of parameter names and values, which may be empty, but is guaranteed to be returned, empty or not. |
Examples
Parse this: <!-- ForPage=default;UseTable=False -->
Return this:
=======================
Name Value
----------- -----------
ForPage default
UseTable False
=======================
The returned NameValueCollection contains two members.
Since this method guarantees to return an initialized NameValueCollection, the empty collection is allocated by the first statement, and is unconditionally returned by the last statement.
| Improve this Doc View SourceQuoteString(String)
Append a quote character to both ends of a string, unless it is already present.
Declaration
public static string QuoteString(this string pstrIn)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrIn | String to evaluate, which may, or may not, end with the characterr specified in pchrEnd. |
Returns
Type | Description |
---|---|
System.String | String with quote character at both ends. |
RemoveEndChars(String, Char)
Remove ending character, such as brackets, from a string, if present.
Declaration
public static string RemoveEndChars(this string pstrIn, char pchrEnd)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrIn | String to evaluate, which may, or may not, end with the characterr specified in pchrEnd. |
System.Char | pchrEnd | Character to remove, if present. |
Returns
Type | Description |
---|---|
System.String | String with character pchrEnd removed from both ends. |
RemoveEndQuotes(String)
Remove ending quotation marks from a string, if present.
Declaration
public static string RemoveEndQuotes(this string pstrIn)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrIn | Specify the string to evaluate, which may, or may not, end with quotes. |
Returns
Type | Description |
---|---|
System.String | The return value is a new string with ending quotes, if present, removed. Otherwise, a copy of the original string is returned. |
RenderEvenWhenNull<T>(T, String, String, IFormatProvider)
For any object of generic type T, return result from calling the ToString method on it, unless the instance is a null reference of type T. In that case, return either the supplied string or the localizable default defined in the string resources expsed by the WizardWrx.Common library.
Declaration
public static string RenderEvenWhenNull<T>(this T pgenericObject, string pstrValueIfNull = null, string pstrFormatString = null, IFormatProvider pformatProvider = null)
where T : IFormattable
Parameters
Type | Name | Description |
---|---|---|
T | pgenericObject | Pass in a reference to an object of the specified generic type, or a null reference to that type. |
System.String | pstrValueIfNull | Use this optional string parameter to override the default value, Common.Properties.Resources.MSG_OBJECT_REFERENCE_IS_NULL. Pass a null referencr or omit the parameter to use the default string. |
System.String | pstrFormatString | Use this optional string parameter to override the default format of the value returned by the instance ToString method. Please see the Remarks. |
IFormatProvider | pformatProvider | Use this optional parameter to override the default format provider used by the instance ToString method. Please see the Remarks. |
Returns
Type | Description |
---|---|
System.String |
If
If
Finally, if Please see the Remarks. |
Type Parameters
Name | Description |
---|---|
T | The specified type T must implement IFormattable. |
Remarks
Because this method is generic, it can call the ToString override on any object that explicitly implements the IFormattable interface, so you derive the benefits of any such override.
The optional pstrFormatString
and
pformatProvider
arguments work in
tandem with the override on the instance ToString method. To use it
and the default null reference representation, pass a null reference
as the value of pstrValueIfNull
.
Replace(String, String, String, StringComparison)
This overload of the standard Replace method implements a third
argumeent, comparisonType
that brings a
StringComparison object to the standard find and replace task.
Declaration
public static string Replace(this string pstrIn, string oldValue, string newValue, StringComparison comparisonType)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrIn | This string stands in for the string object to which this method is applied. |
System.String | oldValue | This string is the substring to find and replace in |
System.String | newValue | This string is the substring that replaces every occurrence of
|
StringComparison | comparisonType | This enumeration type specifies the culture, case, and sort rules to be used by certain overloads of the Compare(String, String) and Equals(Object) methods. |
Returns
Type | Description |
---|---|
System.String | The return value is a copy of |
ReplaceEscapedTabsInStringFromResX(String)
Replace TAB characters, escaped as they must be for embedding in the string of a RESX resource, with real TAB characters.
Declaration
public static string ReplaceEscapedTabsInStringFromResX(this string pstrStringFromResX)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrStringFromResX | The input is expected to be a string that was read from a string resource defined in a .RESX file, which may contain specially coded TAB characters that aree unusable as returned. |
Returns
Type | Description |
---|---|
System.String | The return value is a copy of the resource string in which embedded (escaped) TAB characters are replaced with undecordated TABs. |
ReplaceTokensFromList(String, Dictionary<String, Object>)
Given a string containing tokens of the form "^^ListKeyValue^^" where ListKeyValue is the value of a key in the pnvcList collection, which may or may not exist in the collection, replace all such tokens with the contents of the like named value in the collection.
Declaration
public static string ReplaceTokensFromList(this string pstrMsg, Dictionary<string, object> pdctList)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrMsg | String containing the message containing the substitution tokens. |
Dictionary<System.String, System.Object> | pdctList | A Dictionary, in which each key represents a token, and its value represents the value to be substituted for it. The Dictionary may contain anything, as it accepts any Object. The required substitution string is obtained by calling the default ToString method on each Object. To supply a format string, which will be applied to each Object, in turn, use the next overload. |
Returns
Type | Description |
---|---|
System.String | String with tokens replaced, and tokens that have no corresponding object in the pnvcList collection preserved. |
ReplaceTokensFromList(String, Dictionary<String, Object>, Dictionary<String, Object>)
Given a string containing tokens of the form "^^ListKeyValue^^" where ListKeyValue is the value of a key in the pnvcList collection, which may or may not exist in the collection, replace all such tokens with the contents of the like named value in the collection.
Declaration
public static string ReplaceTokensFromList(this string pstrMsg, Dictionary<string, object> pdctList, Dictionary<string, object> pdctDefaults)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrMsg | String containing the message containing the substitution tokens. |
Dictionary<System.String, System.Object> | pdctList | A Dictionary, in which each key represents a token, and its value represents the value to be substituted for it. The Dictionary may contain anything, as it accepts any Object. The required substitution string is obtained by calling the default ToString method on each Object. To supply a format string, which will be applied to each Object, in turn, use the next overload. |
Dictionary<System.String, System.Object> | pdctDefaults | A Dictionary, in which each key represents a token, and its value represents the default value to be substituted for it, if there is no corresponding key in dictionary pdctList. The Dictionary may contain anything, as it accepts any Object. The required substitution string is obtained by calling the default ToString method on each Object. To supply a format string, which will be applied to each Object, in turn, use the next overload. |
Returns
Type | Description |
---|---|
System.String | String with tokens replaced, and tokens that have no corresponding object in the pnvcList OR the pdctDefaults collection preserved. |
ReplaceTokensFromList(String, Dictionary<String, Object>, Dictionary<String, Object>, String)
Given a string containing tokens of the form "^^ListKeyValue^^" where ListKeyValue is the value of a key in the pnvcList collection, which may or may not exist in the collection, replace all such tokens with the contents of the like named session object.
Declaration
public static string ReplaceTokensFromList(this string pstrMsg, Dictionary<string, object> pdctList, Dictionary<string, object> pdctDefaults, string pstrFormat)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrMsg | String containing the message containing the substitution tokens. |
Dictionary<System.String, System.Object> | pdctList | A Dictionary, in which each key represents a token, and its value represents the value to be substituted for it. The Dictionary may contain anything, as it accepts any Object. The required substitution string is obtained by calling the default ToString method on each Object. To supply a format string, which will be applied to each Object, in turn, use the next overload. |
Dictionary<System.String, System.Object> | pdctDefaults | A Dictionary, in which each key represents a token, and its value represents the default value to be substituted for it, if there is no corresponding key in dictionary pdctList. The Dictionary may contain anything, as it accepts any Object. The required substitution string is obtained by calling the default ToString method on each Object. To supply a format string, which will be applied to each Object, in turn, use the next overload. |
System.String | pstrFormat | Format string, acceptable to the static String.Format method, which is used to format the string representation of each object. The string must contain a "[0}" placeholder, which may occur one or more times in the format string, which is replaced by the string returned by the ToString method of each object. |
Returns
Type | Description |
---|---|
System.String | String with tokens replaced, and tokens that have no corresponding object in the pnvcList OR the pdctDefaults collection preserved. |
ReplaceTokensFromList(String, Dictionary<String, Object>, String)
Given a string containing tokens of the form "^^ListKeyValue^^" where ListKeyValue is the value of a key in the pnvcList collection, which may or may not exist in the collection, replace all such tokens with the contents o object.
Declaration
public static string ReplaceTokensFromList(this string pstrMsg, Dictionary<string, object> pdctList, string pstrFormat)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrMsg | String containing the message containing the substitution tokens. |
Dictionary<System.String, System.Object> | pdctList | A Dictionary, in which each key represents a token, and its value represents the value to be substituted for it. The Dictionary may contain anything, as it accepts any Object. The required substitution string is obtained by calling the default ToString method on each Object. To supply a format string, which will be applied to each Object, in turn, use the next overload. |
System.String | pstrFormat | Format string, acceptable to the static String.Format method, which is used to format the string representation of each object. The string must contain a "[0}" placeholder, which may occur one or more times in the format string, which is replaced by the string returned by the ToString method of each object. |
Returns
Type | Description |
---|---|
System.String | String with tokens replaced, and tokens that have no corresponding object in the pnvcList collection preserved. |
ReplaceTokensFromList(String, NameValueCollection)
Given a string containing tokens of the form "^^ListKeyValue^^" where ListKeyValue is the value of a key in the pnvcList collection, which may or may not exist in the collection, replace all such tokens with the contents of the like named value in the collection.
Declaration
public static string ReplaceTokensFromList(this string pstrMsg, NameValueCollection pnvcList)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrMsg | String containing the message containing the substitution tokens. |
NameValueCollection | pnvcList | A NameValueCollection, in which each key represents a token, and its value represents the value to be substituted for it. |
Returns
Type | Description |
---|---|
System.String | String with tokens replaced, and tokens that have no corresponding object in the pnvcList collection preserved. |
ReplaceTokensFromList(String, NameValueCollection, NameValueCollection)
Replace place holders (markers) with the value of the corresponding form control (field), or a default value, if the field is empty and a default is designated.
Declaration
public static string ReplaceTokensFromList(this string pstrTemplate, NameValueCollection pnvcFields, NameValueCollection pnvcDefaults)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrTemplate | String containing a template containing the text and place markers. |
NameValueCollection | pnvcFields | NameValueCollection containing the form control (field) values. |
NameValueCollection | pnvcDefaults | NameValueCollection containing the form control (field) or token default values. Default values are optional. If omitted, the method substitutes an empty string. |
Returns
Type | Description |
---|---|
System.String | String containing the text in the template, with all tokens formatted with default endings replaced by the contents of the corresponding, and like named, control (field) on the input form. Tokens bounded by "##" are replaced by strings from the Session variables collection. The same defaults collection is used for both. |
ReportUnresolvedEnvironmentStrings(String, UInt32)
Display a string that contains unmatched environment strings or unmatched environment string delimiters, followed by details about the locations of the errors.
Declaration
public static string ReportUnresolvedEnvironmentStrings(this string pstrInput, uint puintNEnvStrDlms)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrInput | Specify a string that has had its environment strings expanded. |
System.UInt32 | puintNEnvStrDlms | Specify the count of unmatched delimiters. A companion routine, UnresolvedEnvironmentStrings, can deliver the count, although the call cannot be nested. Please see the remarks. |
Returns
Type | Description |
---|---|
System.String | The return value is a detailed message that shows each unresolved string. |
ReportUnresolvedEnvironmentStrings(String, UInt32, UInt32)
Display a string that contains unmatched environment strings or unmatched environment string delimiters, followed by details about the locations of the errors.
Declaration
public static uint ReportUnresolvedEnvironmentStrings(this string pstrInput, uint puintNEnvStrDlms, uint puintExitCode)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrInput | Specify a string that has had its environment strings expanded. |
System.UInt32 | puintNEnvStrDlms | Specify the count of unmatched delimiters. A companion routine, UnresolvedEnvironmentStrings, can deliver the count, although the call cannot be nested. Please see the remarks. |
System.UInt32 | puintExitCode | This routine is intended to report the error and exit the calling console application, returning the specified value as its exit code. |
Returns
Type | Description |
---|---|
System.UInt32 | The exit code is passed through, so that the control need not return to the caller, but may exit directly or indirectly through Environment.Exit. |
Remarks
After the count is written onto the standard error stream, control returns to its caller, which may take subsequent actions based upon the return value.
When this situation arises, it is usually because an expected environment string is undefined, and the solution MAY be its removal from the string.
RightPadNChars(String, Int32)
Right pad the string with a specified number of spaces.
Declaration
public static string RightPadNChars(this string pstrPadThisString, int paddingCharacterCount)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrPadThisString | This argument is supplied by the framework when it binds the method to an instance of the System.String class. |
System.Int32 | paddingCharacterCount | Specify the number of space characters to add on the right end of the string. Please see the Remarks for important details. |
Returns
Type | Description |
---|---|
System.String | The input string is padded on the right with the specified number of space characters. Please see the Remarks for important details. |
Remarks
These methods compensate for the completely logical, if unexpected, behavior of the native PadLeft and PadRight methods on the System.string class. Their objective is to guarantee that the new string is truly padded with a specific number of characters.
The names of the visible arguments differ from my usual Hungarian naming convention so that they conform to the naming convention of the Base Class Library methods that they wrap.
RightPadNChars(String, Int32, Char)
Left pad the string with a specified number of some arbitrary character.
Declaration
public static string RightPadNChars(this string pstrPadThisString, int paddingCharacterCount, char paddingChar)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrPadThisString | This argument is supplied by the framework when it binds the method to an instance of the System.String class. |
System.Int32 | paddingCharacterCount | Specify the number of arbitrary characters to add on the right end of the string. Please see the Remarks for important details. |
System.Char | paddingChar | Specify the arbitrary character with which the string is to be padded. |
Returns
Type | Description |
---|---|
System.String | The input string is padded on the right with the specified number of the specified arbitrary character. Please see the Remarks for important details. |
Remarks
These methods compensate for the completely logical, if unexpected, behavior of the native PadLeft and PadRight methods on the System.string class. Their objective is to guarantee that the new string is truly padded with a specific number of characters.
The names of the visible arguments differ from my usual Hungarian naming convention so that they conform to the naming convention of the Base Class Library methods that they wrap.
SQLafe4EmbeddedSingleQuote(String)
Replace single quotation marks in a field that is intended to be the value of a column or filter parameter in a SQL string.
Declaration
public static string SQLafe4EmbeddedSingleQuote(this string pstrIn)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrIn | Specify the string to evaluate, which may, or may not, have embedded single quotation marks. Since this is an extension method, pstrIn is supplied by the BCL when it binds this method to an instance of System.string. |
Returns
Type | Description |
---|---|
System.String | The return value is an escaped string suitable for inclusion as the value of a column, filter parameter, or storeed procedure parameteer in a SQL string. |
Truncate(String, Int32)
Supply the missing Truncate function to members of the String class.
Declaration
public static string Truncate(this string pstrSource, int pintMaxLength)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrSource | String to truncate, if it is longer than pintMaxLength. A null string is treated as an empty string, and the return value for either is an empty string. |
System.Int32 | pintMaxLength | Desired maximum length of the returned string. If pstrSource is longer than pintMaxLength characters, the first pintMaxLength are returned. Otherwise, the whole string is returned. If pintMaxLength is less than or equal to zero, an empty string is returned. |
Returns
Type | Description |
---|---|
System.String | If the string length is less than or equal to the specified maximum length, the whole string is returned. Otherwise, the first pintMaxLength characters are returned. Regardless, the return value is a new System.String object. |
UnixLineEndings(String)
Replace a string that may contain mixed or unwanted line endings with a string that contains only the expected line ending type.
Declaration
public static string UnixLineEndings(this string pstrSource)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrSource | String in which to replace line endings |
Returns
Type | Description |
---|---|
System.String | A copy of |
WindowsLineEndings(String)
Replace a string that may contain mixed or unwanted line endings with a string that contains only the expected line ending type.
Declaration
public static string WindowsLineEndings(this string pstrSource)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrSource | String in which to replace line endings |
Returns
Type | Description |
---|---|
System.String | A copy of |