Learn to perform your first token-aware find-and-replace operation using the uCalc Transformer.
Product:
Class:
Warning
uCalc API Preview Release Notice:The uCalc engine has successfully transitioned to modern cross-platform environments.The next phase envolves some structural changes, performance optimizations, and API refinements.The API is subject to breaking changes prior to the stable release. Please evaluate the preview version thoroughly before production use.
Welcome to the uCalc Transformer! This is the engine's tool for structural, token-aware find-and-replace operations. It's more powerful and safer than a standard string Replace() function because it understands the structure of your text, distinguishing between code, numbers, and string literals.
This guide will walk you through the essential steps to perform your first transformation.
Every transformation follows the same basic workflow:
Let's break down each step.
First, you need an instance of the Transformer. You can create one from your uCalc object.
using uCalcSoftware;
var uc = new uCalc();
using (var t = new uCalc.Transformer()) {;
Note: The using block ensures the transformer's resources are automatically released when it goes out of scope.
FromTo()Next, you define the transformation logic using the FromTo() method. It takes two arguments: the pattern to find and the text to replace it with.
Our pattern will look for the word "Hello" followed by a variable named name.
using uCalcSoftware;
var uc = new uCalc();
// A rule to find "Hello {name}" and replace it with "Greetings, {name}!"
t.FromTo("Hello {name}", "Greetings, {name}!");
Here, {name} is a variable that captures the word immediately following "Hello". This captured value can be used in the replacement string.
Now, provide the source string you want to modify. You can do this by assigning a string directly to the transformer object, which is a shortcut for setting its Text property.
using uCalcSoftware;
var uc = new uCalc();
t = "Hello World";
Finally, call the Transform() method to execute the rules. The result can be retrieved by using the transformer object where a string is expected.
using uCalcSoftware;
var uc = new uCalc();
Console.WriteLine(t.Transform());
Here is the complete code from all four steps:
using uCalcSoftware;
var uc = new uCalc();
using (var t = new uCalc.Transformer()) {
t.FromTo("Hello {name}", "Greetings, {name}!");
t = "Hello World";
Console.WriteLine(t.Transform()); // Output: Greetings, World!
};
You might ask, "Why not just use a standard string Replace() function?" The power of the Transformer lies in its token awareness. A standard replace is character-based and "blind" to structure. The Transformer breaks text into meaningful units (tokens) first, so it knows the difference between the number 10 and the string "10".
This prevents common bugs, like accidentally changing text inside a string literal when refactoring code. The next tutorial, [Capturing Text with Variables](TODO: Broken Link), will explore this concept in more detail.
ID: 1195
using uCalcSoftware;
var uc = new uCalc();
using (var t = new uCalc.Transformer()) {
// Define the rule and execute the transform in a single, chained statement.
t.FromTo("Hello", "Greetings");
Console.WriteLine(t.Transform("Hello World!"));
}
Greetings World! using uCalcSoftware; var uc = new uCalc(); using (var t = new uCalc.Transformer()) { // Define the rule and execute the transform in a single, chained statement. t.FromTo("Hello", "Greetings"); Console.WriteLine(t.Transform("Hello World!")); }
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
{
uCalc::Transformer t;
t.Owned(); // Causes t to be released when it goes out of scope
// Define the rule and execute the transform in a single, chained statement.
t.FromTo("Hello", "Greetings");
cout << t.Transform("Hello World!") << endl;
}
}
Greetings World! #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; { uCalc::Transformer t; t.Owned(); // Causes t to be released when it goes out of scope // Define the rule and execute the transform in a single, chained statement. t.FromTo("Hello", "Greetings"); cout << t.Transform("Hello World!") << endl; } }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Using t As New uCalc.Transformer()
'// Define the rule and execute the transform in a single, chained statement.
t.FromTo("Hello", "Greetings")
Console.WriteLine(t.Transform("Hello World!"))
End Using
End Sub
End Module
Greetings World! Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Using t As New uCalc.Transformer() '// Define the rule and execute the transform in a single, chained statement. t.FromTo("Hello", "Greetings") Console.WriteLine(t.Transform("Hello World!")) End Using End Sub End Module
ID: 1196
using uCalcSoftware;
var uc = new uCalc();
using (var t = new uCalc.Transformer()) {
// This rule replaces the ALPHANUMERIC token 'x', not just the character 'x'.
t.FromTo("x", "value");
var code = """
if (x > 10) print("Max value is x");
""";
// The 'x' inside the string is ignored because QuoteSensitive is true by default.
Console.WriteLine(t.Transform(code));
}
if (value > 10) print("Max value is x"); using uCalcSoftware; var uc = new uCalc(); using (var t = new uCalc.Transformer()) { // This rule replaces the ALPHANUMERIC token 'x', not just the character 'x'. t.FromTo("x", "value"); var code = """ if (x > 10) print("Max value is x"); """; // The 'x' inside the string is ignored because QuoteSensitive is true by default. Console.WriteLine(t.Transform(code)); }
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
{
uCalc::Transformer t;
t.Owned(); // Causes t to be released when it goes out of scope
// This rule replaces the ALPHANUMERIC token 'x', not just the character 'x'.
t.FromTo("x", "value");
auto code = R"(if (x > 10) print("Max value is x");)";
// The 'x' inside the string is ignored because QuoteSensitive is true by default.
cout << t.Transform(code) << endl;
}
}
if (value > 10) print("Max value is x"); #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; { uCalc::Transformer t; t.Owned(); // Causes t to be released when it goes out of scope // This rule replaces the ALPHANUMERIC token 'x', not just the character 'x'. t.FromTo("x", "value"); auto code = R"(if (x > 10) print("Max value is x");)"; // The 'x' inside the string is ignored because QuoteSensitive is true by default. cout << t.Transform(code) << endl; } }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Using t As New uCalc.Transformer()
'// This rule replaces the ALPHANUMERIC token 'x', not just the character 'x'.
t.FromTo("x", "value")
Dim code = "if (x > 10) print(""Max value is x"");"
'// The 'x' inside the string is ignored because QuoteSensitive is true by default.
Console.WriteLine(t.Transform(code))
End Using
End Sub
End Module
if (value > 10) print("Max value is x"); Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Using t As New uCalc.Transformer() '// This rule replaces the ALPHANUMERIC token 'x', not just the character 'x'. t.FromTo("x", "value") Dim code = "if (x > 10) print(""Max value is x"");" '// The 'x' inside the string is ignored because QuoteSensitive is true by default. Console.WriteLine(t.Transform(code)) End Using End Sub End Module
This page last modified on: