Class StringDecoratorExtensions
- Namespace
- Cuemon
- Assembly
- Cuemon.Core.dll
Extension methods for the string class hidden behind the IDecorator<T> interface.
public static class StringDecoratorExtensions
- Inheritance
-
StringDecoratorExtensions
Examples
StringDecoratorExtensions provides extension methods on Decorator.Enclose for string manipulation including casing conversion, encoding, URI conversion, set operations, and content inspection. This example wraps " Hello World! " and applies ToCasing with LowerCase, UpperCase, and TitleCase modes, converts the string to a byte array and stream with configurable encoding, and extracts the differing portion between "Hello World!" and "Hello Universe!" using Difference. Key steps also include StartsWith checks with multiple candidate strings and ContainsAny for character matching. Console output confirms each transformed value, such as " hello world! " for lower-casing and "Universe!" for the set difference.
using System;
using System.Globalization;
using System.IO;
using System.Text;
using Cuemon;
using Cuemon.Text;
namespace MyApp.Examples;
public class Example
{
public void Run()
{
string value = " Hello World! ";
var decorator = Decorator.Enclose(value);
// Change casing via decorator
string lower = decorator.ToCasing(CasingMethod.LowerCase); // " hello world! "
string upper = decorator.ToCasing(CasingMethod.UpperCase); // " HELLO WORLD! "
string title = decorator.ToCasing(CasingMethod.TitleCase, new CultureInfo("en-US")); // " Hello World! "
// Convert to byte array
byte[] bytes = decorator.ToByteArray(o => o.Encoding = Encoding.UTF8);
// Convert to stream
using Stream stream = decorator.ToStream(o => o.Encoding = Encoding.UTF8);
// Convert to URI
string url = "https://www.example.com";
var uriDecorator = Decorator.Enclose(url);
Uri uri = uriDecorator.ToUri(); // https://www.example.com/
// Encoding conversions
string ascii = decorator.ToAsciiEncodedString(o => o.Encoding = Encoding.UTF8); // " Hello World! " (non-ASCII chars replaced with empty)
string encoded = decorator.ToEncodedString(o =>
{
o.TargetEncoding = Encoding.ASCII;
o.EncoderFallback = new EncoderReplacementFallback("?");
});
// StartsWith checks
bool starts = decorator.StartsWith(" Hello"); // true
bool startsIgnore = decorator.StartsWith(StringComparison.OrdinalIgnoreCase, "hello"); // true
bool startsAny = decorator.StartsWith("Hi", "Hello"); // true
// Set difference
var helloDecorator = Decorator.Enclose("Hello World!");
string diff = helloDecorator.Difference("Hello Universe!"); // "Universe!"
// ContainsAny for characters
bool hasChar = decorator.ContainsAny('o', StringComparison.Ordinal); // true
bool hasChars = decorator.ContainsAny(StringComparison.Ordinal, 'x', 'y'); // false
}
}
Methods
ContainsAny(IDecorator<string>, char, StringComparison)
Returns a value indicating whether the specified find occurs within the enclosed string of the specified decorator.
public static bool ContainsAny(this IDecorator<string> decorator, char find, StringComparison comparison = StringComparison.OrdinalIgnoreCase)
Parameters
decoratorIDecorator<string>The IDecorator<T> to extend.
findcharThe char to search within enclosed string of the specified
decorator.comparisonStringComparisonOne of the enumeration values that specifies the rules to use in the comparison. Default is OrdinalIgnoreCase.
Returns
- bool
trueif thefindparameter occurs within the enclosed string of the specifieddecorator; otherwise,false.
Exceptions
- ArgumentNullException
decoratoris null -or-findis null.
ContainsAny(IDecorator<string>, StringComparison, params char[])
Returns a value indicating whether the specified values occurs within the enclosed string of the specified decorator object.
public static bool ContainsAny(this IDecorator<string> decorator, StringComparison comparison, params char[] values)
Parameters
decoratorIDecorator<string>The IDecorator<T> to extend.
comparisonStringComparisonOne of the enumeration values that specifies the rules to use in the comparison.
valueschar[]The char sequence to search within the enclosed string of the specified
decorator.
Returns
- bool
trueif thevaluesparameter occurs within the enclosed string of the specifieddecorator; otherwise,false.
Exceptions
- ArgumentNullException
decoratoris null -or-valuesis null.
Difference(IDecorator<string>, string)
Returns the set difference between second and the enclosed string of the specified decorator or Empty if no difference.
public static string Difference(this IDecorator<string> decorator, string second)
Parameters
decoratorIDecorator<string>The IDecorator<T> to extend.
secondstringThe value to compare with the enclosed string of the specified
decorator.
Returns
- string
A string that contains the set difference between
secondand the enclosed string of the specifieddecoratoror Empty if no difference.
StartsWith(IDecorator<string>, IEnumerable<string>)
Determines whether the beginning of the enclosed string of the specified decorator matches at least one string in the specified sequence of strings.
public static bool StartsWith(this IDecorator<string> decorator, IEnumerable<string> strings)
Parameters
decoratorIDecorator<string>The IDecorator<T> to extend.
stringsIEnumerable<string>A sequence of string values to match against.
Returns
- bool
trueif at least one value matches the beginning of the enclosed string of the specifieddecorator; otherwise,false.
Remarks
This match is performed by using a default value of OrdinalIgnoreCase.
StartsWith(IDecorator<string>, StringComparison, IEnumerable<string>)
Determines whether the beginning of the enclosed string of the specified decorator matches at least one string in the specified sequence of strings.
public static bool StartsWith(this IDecorator<string> decorator, StringComparison comparison, IEnumerable<string> strings)
Parameters
decoratorIDecorator<string>The IDecorator<T> to extend.
comparisonStringComparisonOne of the enumeration values that specifies the rules to use in the comparison.
stringsIEnumerable<string>A sequence of string values to match against.
Returns
- bool
trueif at least one value matches the beginning of the enclosed string of the specifieddecorator; otherwise,false.
StartsWith(IDecorator<string>, StringComparison, params string[])
Determines whether the beginning of the enclosed string of the specified decorator matches at least one string in the specified sequence of strings.
public static bool StartsWith(this IDecorator<string> decorator, StringComparison comparison, params string[] strings)
Parameters
decoratorIDecorator<string>The IDecorator<T> to extend.
comparisonStringComparisonOne of the enumeration values that specifies the rules to use in the comparison.
stringsstring[]A sequence of string values to match against.
Returns
- bool
trueif at least one value matches the beginning of this string; otherwise,false.
Remarks
This match is performed by using a default value of OrdinalIgnoreCase.
StartsWith(IDecorator<string>, params string[])
Determines whether the beginning of the enclosed string of the specified decorator matches at least one string in the specified sequence of strings.
public static bool StartsWith(this IDecorator<string> decorator, params string[] strings)
Parameters
decoratorIDecorator<string>The IDecorator<T> to extend.
stringsstring[]A sequence of string values to match against.
Returns
- bool
trueif at least one value matches the beginning of the enclosed string of the specifieddecorator; otherwise,false.
Remarks
This match is performed by using a default value of OrdinalIgnoreCase.
ToAsciiEncodedString(IDecorator<string>, Action<EncodingOptions>)
Encodes all the characters in the enclosed string of the specified decorator to its ASCII encoded variant.
public static string ToAsciiEncodedString(this IDecorator<string> decorator, Action<EncodingOptions> setup = null)
Parameters
decoratorIDecorator<string>The IDecorator<T> to extend.
setupAction<EncodingOptions>The EncodingOptions which may be configured.
Returns
Exceptions
- ArgumentNullException
decoratorcannot be null.
ToByteArray(IDecorator<string>, Action<EncodingOptions>)
Converts the enclosed string of the specified decorator to its equivalent byte[] representation.
public static byte[] ToByteArray(this IDecorator<string> decorator, Action<EncodingOptions> setup = null)
Parameters
decoratorIDecorator<string>The IDecorator<T> to extend.
setupAction<EncodingOptions>The EncodingOptions which may be configured.
Returns
Exceptions
- ArgumentNullException
decoratorcannot be null.- InvalidEnumArgumentException
setupwas initialized with an invalid Preamble.
ToCasing(IDecorator<string>, CasingMethod)
Converts the enclosed string of the specified decorator to either lowercase, UPPERCASE, Title Case or unaltered.
public static string ToCasing(this IDecorator<string> decorator, CasingMethod method = CasingMethod.Default)
Parameters
decoratorIDecorator<string>The IDecorator<T> to extend.
methodCasingMethodThe method to use in the conversion.
Returns
- string
A string that corresponds to the enclosed string of the specified
decoratorwith the applied conversionmethod.
Remarks
Uses InvariantCulture for the conversion.
Exceptions
- ArgumentNullException
decoratorcannot be null.
ToCasing(IDecorator<string>, CasingMethod, CultureInfo)
Converts the enclosed string of the specified decorator to either lowercase, UPPERCASE, Title Case or unaltered using the specified culture.
public static string ToCasing(this IDecorator<string> decorator, CasingMethod method, CultureInfo culture)
Parameters
decoratorIDecorator<string>The IDecorator<T> to extend.
methodCasingMethodThe method to use in the conversion.
cultureCultureInfoThe culture rules to apply the conversion.
Returns
- string
A string that corresponds to the enclosed string of the specified
decoratorwith the applied conversionmethod.
Exceptions
- ArgumentNullException
decoratorcannot be null -or-culturecannot be null.
ToEncodedString(IDecorator<string>, Action<FallbackEncodingOptions>)
Encodes all the characters in the enclosed string of the specified decorator to its encoded string variant.
public static string ToEncodedString(this IDecorator<string> decorator, Action<FallbackEncodingOptions> setup = null)
Parameters
decoratorIDecorator<string>The IDecorator<T> to extend.
setupAction<FallbackEncodingOptions>The FallbackEncodingOptions which may be configured.
Returns
- string
A string variant of the enclosed string of the specified
decoratorthat is encoded with TargetEncoding.
Remarks
The inspiration for this method was retrieved @ SO: https://stackoverflow.com/a/135473/175073.
Exceptions
- ArgumentNullException
decoratorcannot be null.
ToStream(IDecorator<string>, Action<EncodingOptions>)
public static Stream ToStream(this IDecorator<string> decorator, Action<EncodingOptions> setup = null)
Parameters
decoratorIDecorator<string>The IDecorator<T> to extend.
setupAction<EncodingOptions>The EncodingOptions which may be configured.
Returns
Remarks
IEncodingOptions will be initialized with DefaultPreambleSequence and DefaultEncoding.
Exceptions
- ArgumentNullException
decoratorcannot be null.- InvalidEnumArgumentException
setupwas initialized with an invalid Preamble.
ToUri(IDecorator<string>, UriKind)
public static Uri ToUri(this IDecorator<string> decorator, UriKind uriKind = UriKind.Absolute)
Parameters
decoratorIDecorator<string>The IDecorator<T> to extend.
uriKindUriKindSpecifies whether the URI string is a relative URI, absolute URI, or is indeterminate.
Returns
Exceptions
- ArgumentNullException
decoratorcannot be null.- ArgumentNullException
The enclosed string of the specified
decoratorcannot be null.- ArgumentException
The enclosed string of the specified
decoratorcannot be empty or consist only of white-space characters.