exprv4:how-to:use_text_values

How To Use Text Values

Text values are strings. Use strings for messages, labels, file names, dialog text, status values, and values read from files or input fields.

message = 'Probing started';
tool_name = 'Tool 1';
file_name = 'probe_report.txt';

A string is a value, so it can be stored in a variable, passed to a function, printed, returned from a function, or stored in an object field.


Write A Text Value

The most common style is single quotes:

message = 'Tool change started';

Double quotes also work:

message = "Tool change started";

Use the quote style that makes the text easiest to read.

text1 = 'He said "OK"';
text2 = "It's OK";

Backtick strings are useful when the text contains both single and double quotes.

text = `He said "it's OK"`;

Join Text With Values

Use + to join text.

tool = 3;
message = 'Tool ' + tool;
message;

The final result is `'Tool 3'`.

When a string is involved, + creates a string result.

x = 10;
y = 20;
 
text = 'X=' + x + ', Y=' + y;
text;

Use parentheses when you want a calculation before text is joined.

diameter = 6;
message = 'Radius: ' + (diameter / 2);

Without parentheses, the expression may join text earlier than you intended.

'Value: ' + 1 + 2;      // 'Value: 12'
'Value: ' + (1 + 2);    // 'Value: 3'

Use `print(…)` to show messages while a script runs.

print('Probing started');
 
safe_z = 5;
print('safe z ', safe_z);

`print(…)` is useful for progress messages and for checking variable values while writing a script.


Use Multi-Line Text

Triple double-quoted strings use """ at the start and end.

They are useful for longer messages, templates, or text that should keep line breaks.

message = """
Probe report
Tool: 3
Status: OK
""";
 
print(message);

If the opening """ is followed immediately by a newline, that first newline is not included in the string value. This lets the text start on the next line in the script.

Triple double-quoted strings are raw strings. Escape sequences are not processed inside them.

text = """
first\nsecond
""";

In this example, `\n` stays as backslash plus `n`. Actual line breaks in the source text are preserved.


Use Escape Sequences

Normal quoted strings process escape sequences.

Common examples:

line = 'first\nsecond';
tabbed = 'A\tB';
quote1 = 'It\'s OK';
quote2 = "He said \"OK\"";

Use escape sequences when you need special characters inside a normal quoted string.

For longer text with many quotes or line breaks, triple double-quoted strings are often easier to read.


Check Text

String methods are called with dot syntax.

name = 'Tool 1';
 
name.length();
name.contains('Tool');
name.startswith('Tool');
name.endswith('1');

These methods are useful for input checks and file-name checks.

file = 'probe_report.txt';
 
if(file.endswith('.txt'))
{
    print('text file');
};

Extract Part Of Text

Use string methods when you need part of a string.

text = 'Tool 3';
 
number_text = text.after(' ');
number_text;

The final result is `'3'`.

Other useful methods:

'abcdef'.substr(2, 3);    // 'cde'
'key=value'.before('=');  // 'key'
'key=value'.after('=');   // 'value'

String positions start at `0`.


Split Simple Separated Text

The `before(…)` and `after(…)` methods are useful for simple separated text.

This example reads lines separated by newlines, then reads tokens separated by semicolons:

text = """
tool;diameter;feed
T1;6;800
T2;3;500
""";
 
while(text.length())
{
    line = text.before("\n");
 
    while(line.length())
    {
        if(line.contains(";"))
        {
            token = line.before(";");
            line = line.after(";");
        }
        else
        {
            token = line;
            line = "";
        };
 
        print(token);
    };
 
    text = text.after("\n");
};

The `if(line.contains(“;”))` check handles the last token on each line. Without that check, the last value could be missed or the loop could be harder to reason about.

This style is useful for simple machine data, reports, or configuration text. It is not a full CSV parser for quoted fields or escaped separators.


Clean Text

Text from files, dialogs, or user input may contain extra spaces or inconsistent case.

name = '  tool 1  ';
clean = name.trim().upper();
clean;

The final result is `'TOOL 1'`.

Common cleanup methods:

'  text  '.trim();
'abc'.upper();
'ABC'.lower();

String methods return new strings. Store the result if you want to use the cleaned value later.

name = name.trim();

Convert Text To Numbers

Use parsing methods when text contains a number.

text = '123';
value = text.parse_num();
value * 2;

The final result is `246`.

Other parsing methods:

'FF'.parse_hex();
'1010'.parse_bin();
'true'.parse_bool();

Parsing fails with an error when the string cannot be parsed.

'abc'.parse_num();    // error

Check or clean input before parsing when the text may not be valid.


Use Text From MDI

In MDI, start with `=` to evaluate Expr.

=name='Tool 3';
message='Selected ' + name;
message;

This outputs:

Selected Tool 3

Common Mistakes

Forgetting Quotes

This uses a variable named `Tool`, not the text `Tool`:

name = Tool;      // probably wrong

Use quotes for text:

name = 'Tool';

Expecting Text To Be A Number Automatically

Text that looks like a number is still text.

'10' * 2;              // error
'10'.parse_num() * 2;  // 20

Joining Text Before Calculating

'Value: ' + 1 + 2;      // 'Value: 12'
'Value: ' + (1 + 2);    // 'Value: 3'

Use parentheses when the calculation should happen first.

Using The Wrong Case

String comparisons are case-sensitive.

'Tool' == 'tool';       // false

Convert case first when needed:

'Tool'.lower() == 'tool';

Try This

In MDI, try:

=name=' tool 3 ';
clean=name.trim().upper();
message='Selected ' + clean;
message;

The final result is `'Selected TOOL 3'`.


See Also

exprv4/how-to/use_text_values.txt · Last modified: by 127.0.0.1

Page Tools