Table of Contents

Class StringDecoratorExtensions

Namespace
Cuemon.Net
Assembly
Cuemon.Net.dll

Extension methods for the string class hidden behind the IDecorator<T> interface.

public static class StringDecoratorExtensions
Inheritance
StringDecoratorExtensions

Examples

StringDecoratorExtensions in the Net namespace provides URL-encoding and URL-decoding extension methods on Decorator.Enclose for strings with custom encoding support. This example encodes "hello world & some <stuff>" via UrlEncode producing "hello+world+%26+some+%3cstuff%3e", decodes it back to the original via UrlDecode, and also demonstrates calling the static methods directly on Cuemon.Net.StringDecoratorExtensions.UrlEncode. Custom encoding with UTF-8 on "a=b&c=d" produces "a%3db%26c%3dd". Console output confirms each encode and decode operation's round-trip behavior.

using System;
using Cuemon;
using System.Text;
using Cuemon.Net;
using Cuemon.Text;

namespace MyApp.Net
{
    public class StringDecoratorExtensionsExample
    {
        public void Demonstrate()
        {
            // URL-encode a string with special characters
            string raw = "hello world & some <stuff>";

            // Invoke UrlEncode extension method
            string encoded = Decorator.Enclose(raw).UrlEncode();
            Console.WriteLine(encoded); // "hello+world+%26+some+%3cstuff%3e"

            // Also invoke as static method via the Cuemon.Net.StringDecoratorExtensions type
            string encodedStatic = Cuemon.Net.StringDecoratorExtensions.UrlEncode(Decorator.Enclose(raw));
            Console.WriteLine(encodedStatic); // "hello+world+%26+some+%3cstuff%3e"

            // URL-decode the encoded string back
            string decoded = Decorator.Enclose(encoded).UrlDecode();
            Console.WriteLine(decoded); // "hello world & some <stuff>"

            // Invoke UrlDecode as static method via the Cuemon.Net.StringDecoratorExtensions type
            string decodedStatic = Cuemon.Net.StringDecoratorExtensions.UrlDecode(Decorator.Enclose(encoded));
            Console.WriteLine(decodedStatic); // "hello world & some <stuff>"

            // Encode with a specific encoding
            string encodedUtf8 = Decorator.Enclose("a=b&c=d").UrlEncode(o =>
            {
                o.Encoding = Encoding.UTF8;
            });
            Console.WriteLine(encodedUtf8); // "a%3db%26c%3dd"

        }
    }
}

Remarks

Kudos to the mono-project team for this class. I only modified some of the original code to fit into this class. For the original code, have a visit here for the source code: https://github.com/mono/mono/blob/master/mcs/class/System.Web/System.Web/HttpUtility.cs or here for the mono-project website: http://www.mono-project.com/. Primary reason for including these methods: https://edi.wang/post/2018/11/25/netcore-webutility-urlencode-httputility-urlencode

Methods

UrlDecode(IDecorator<string>, Action<EncodingOptions>)

Converts the enclosed string of the specified decorator that has been encoded for transmission in a URL into a decoded string.

public static string UrlDecode(this IDecorator<string> decorator, Action<EncodingOptions> setup = null)

Parameters

decorator IDecorator<string>

The IDecorator<T> to extend.

setup Action<EncodingOptions>

The EncodingOptions which may be configured.

Returns

string

An URL decoded string.

Exceptions

ArgumentNullException

decorator cannot be null.

UrlEncode(IDecorator<string>, Action<EncodingOptions>)

Encodes a URL string from the enclosed string of the specified decorator.

public static string UrlEncode(this IDecorator<string> decorator, Action<EncodingOptions> setup = null)

Parameters

decorator IDecorator<string>

The IDecorator<T> to extend.

setup Action<EncodingOptions>

The EncodingOptions which may be configured.

Returns

string

An URL encoded string.

Exceptions

ArgumentNullException

decorator cannot be null.

See Also