stringy 2.0.1

dotnet add package stringy --version 2.0.1
NuGet\Install-Package stringy -Version 2.0.1
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="stringy" Version="2.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add stringy --version 2.0.1
#r "nuget: stringy, 2.0.1"
#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 stringy as a Cake Addin
#addin nuget:?package=stringy&version=2.0.1

// Install stringy as a Cake Tool
#tool nuget:?package=stringy&version=2.0.1

Stringy

Stringy is a simple and light-weight runtime string interpolation engine. Built as a utility for a text-based game, the purpose of the library is to provide something that's easier to use and more light-weight than Runtime Text Templates or RazorEngine, but a little more powerful than string.Replace().

On top of string interpolation, Stringy can also be used to facilitate simple expression evaluation. For example, suppose you want to store an arbitrary conditional statement as a "saved filter" for some list of items. You could write your filtering code like so:

string filterStr = "person.Age < 18 && person.Firstname.StartsWith('A')"; // Fetch from database
IStringy engine = new Stringy();

// Assume "people" is an IEnumerable of Person objects
var people = GetListOfPersonObjects();
var filteredPeople = people.Where(p => engine.Set("person", p).EvaluateExpression<bool>(filterStr));

// filteredPeople contains only those Persons with a first name
// that starts with the letter A, and an age lower than 18

Use cases

Stringy favours light string replacement and basic logic and branching needs. If you want to produce large bodies of (formatted) text using complex logic, you will probably find it worth it to use a full templating solution like RazorEngine. Use Stringy if:

  • Your string templates require basic logic and branching but you don't need the full power of C#.

  • You keep strings in a database/persistent storage and your substitution needs are greater than what can be achieved with regular expressions and string.Replace().

  • You need to execute methods and access properties on your model objects from inside your string templates.

  • You need to evaluate stored/persisted expressions

Installation

Simply add the Stringy NuGet package to your solution:

dotnet add package Stringy
dotnet restore

Or, using powershell:

Install-Package Stringy

Examples

Simple string substitution
string str = "Hello World";
const string template = "Substitute this: {value}";

IStringy engine = new Stringy();
engine.Set("value", str);

// Prints: Substitute this: Hello World
Console.WriteLine(engine.Execute(template));

Using engine.Set() you can add any object and its methods and properties will be available in your string templates.

Executing basic math
int num1 = 6;
int num2 = 7;
const string template = "{a} * {b} = {a * b}";

IStringy engine = new Stringy();
engine.Set("a", num1);
engine.Set("b", num2);

// Prints: 6 * 7 = 42
Console.WriteLine(engine.Execute(template));
Calling methods and properties of objects
string str = "Hello World";
const string template = "The lowercase string '{value.ToLower()}' has {value.Length} characters";

IStringy engine = new Stringy();
engine.Set("value", str);

// Prints: The lowercase string 'hello world' has 11 characters
Console.WriteLine(engine.Execute(template));
Simple if/else statements
bool userIsFemale = true;
const string template = "Welcome, dear {isFemale ? 'Lady' : 'Sir'}";

IStringy engine = new Stringy();
engine.Set("isFemale", userIsFemale);

// Prints: Welcome, dear Lady
Console.WriteLine(engine.Execute(template));

License

Apache 2.0

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework 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 tizen40 was computed.  tizen60 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
2.0.1 1,112 5/29/2023
2.0.0 8,823 1/15/2020
1.0.1 1,430 10/24/2018
1.0.0 1,103 10/24/2018

Adds a fix in order to properly support negations