Stax.StringToUrl 1.0.0

dotnet add package Stax.StringToUrl --version 1.0.0
NuGet\Install-Package Stax.StringToUrl -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Stax.StringToUrl" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Stax.StringToUrl --version 1.0.0
#r "nuget: Stax.StringToUrl, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Stax.StringToUrl as a Cake Addin
#addin nuget:?package=Stax.StringToUrl&version=1.0.0

// Install Stax.StringToUrl as a Cake Tool
#tool nuget:?package=Stax.StringToUrl&version=1.0.0

String To Url

This NuGet library contains an extension method to turn any string into a string which can be used in an URL. A practical example is how I use this on Musicstax where I take track, artist or playlist names and turn them into a URL.

Example:

Input: Charlie Puth That's Hilarious

Output: charlie-puth-thats-hilarious

Why I have written this

This is a common feature I use for generating URLs from strings which could be of use for others. Replacing diacritics, cleaning out non alphanumeric characters and properly formatting a string into a proper URL/path can be fiddly. This library helps solve that by making it as easy as calling an extension method to string.

How to use / what this does

Once you have installed the StringToUrl nuget package to your solution you will be able to use this.

There are two ways to use StringToUrl.

  • With the default options
  • By overriding options with a UrlOptions object
With default options

On any string you can do:

string.ConvertToUrl()

This will:

  • Replace diacritics. Eg: ā is turned into a
  • Remove any non alphanumeric characters
  • Replace any spaces with -
  • Remove any instances where words were stripped in such a way that one or more dashes are next to each other and replace them with just one
  • Return the generated URL path

Example:

var name = "Charlie Puth That's Hilarious";

name.ConvertToUrl(); // returns charlie-puth-thats-hilarious
Overriding default options

You can pass through a UrlOptions object to string.ConvertToUrl() to override some the default options.

Option Description
SpaceReplacementCharacter<br/>string By default spaces are replaced with a dash. You can override this with any value.
Case<br/>StringCase enum By default the returned URL is converted to lowercase. You can override this with:<br/>StringCase.Upper - returns the string in all uppercase<br/>StringCase.Unchanged - returns the string without any case change<br/>StringCase.Lower - default - returns the string in all lowercase
MaxLength<br/>int By default the entire input string is converted and then returned. You can choose to set a max character length by setting this.<br/>Note: if the ending character is a SpaceReplacementCharacter this is removed so that the returned URL does not end in a dash for example.
Append<br/>string Add to the end of the generated URL path.
Prepend<br/>string Add to the start of the generated URL path. Eg: your domain.

Any of these options can be set without others being set.

To use this:

var name = "Charlie Puth That's Hilarious";

var options = new UrlOptions {
    SpaceReplacementCharacter = "_",
    Case = StringCase.UPPER,
    MaxLength = 25,
    Prepend = "https://google.co.nz/".
    Append = "/index"
};

name.ConvertToUrl(options); // returns https://google.co.nz/CHARLIE_PUTH_THATS_HILARI/index

As noted, you do not need to set all options:

var name = "Charlie Puth That's Hilarious";

var options = new UrlOptions {
    MaxLength = 25
};

name.ConvertToUrl(options); // returns charlie-puth-thats-hilari
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.3 is compatible.  netstandard1.4 was computed.  netstandard1.5 was computed.  netstandard1.6 was computed.  netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Universal Windows Platform uap was computed.  uap10.0 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 1,128 4/18/2022