String Type

Strings in DWScript are Unicode-capable (UTF-16) and highly flexible. They support both traditional Pascal functions and modern method-style syntax.

Literals

DWScript supports several styles of string literals to handle everything from simple labels to large blocks of text.

Single Quoted Strings

Standard Pascal strings use single quotes '. To include a single quote inside the string, use two consecutive single quotes ''.

// Type inference
var s := 'It''s a beautiful day';

// Explicit typing
var name : String := 'Alice';
var greeting : String;
greeting := 'Hello, world!';

Double Quoted Strings (Multi-line)

Double quotes " define strings that can span multiple lines directly.

var multi := "This is a
multi-line string.";

PrintLn(multi);
Result
This is a
multi-line string.

Triple Quoted Strings (Delphi Compatible)

Triple apostrophes ''' define multiline strings that are fully compatible with Delphi syntax. Unlike double-quoted strings or raw strings, no indentation stripping is performed. The string is taken literally, including all leading whitespace on each line.

var msg := '''
  Hello
  World
  ''';
// Result contains the 2 spaces indentation

Raw Strings (Heredocs)

Prefixing a string with # (e.g., #'...' or #"...") creates a raw string.

  • Indentation Stripping: If a multiline raw string starts with a newline, common leading indentation is automatically removed.
  • No Escaping: Backslashes are treated as literal characters.
// Common indent is stripped
var sql := #"
  SELECT *
  FROM Users
  WHERE Name = 'Admin'
";

// Literal paths (no double backslash needed)
var path := #'C:\Windows\System32';

Resource Strings

Use the resourcestring keyword to define strings that are stored as resources. This is traditionally used for localizable text.

program ResourceStringsDemo;

resourcestring
  rsGreeting = 'Hello, %s!';
  rsError = 'An unexpected error occurred.';

begin
  PrintLn(Format(rsGreeting, ['Alice']));
end.

Character Literals

Individual characters can be represented by their ASCII/Unicode code using the # prefix.

var lineFeed := #10;
var space := #$20; 
var combined := 'First line'#13#10'Second line';

Character Access & Indexing

Strings in DWScript are 1-based, matching traditional Pascal conventions. The first character is at index 1, and the last character is at index s.Length.

var s := 'DWScript';
PrintLn('First char: ' + s[1]);
PrintLn('Last char: ' + s[s.Length]);

// Loop through characters
for var i := 1 to s.Length do
  Print(s[i] + ' ');
Result
First char: D
Last char: t
D W S c r i p t

Indexing Gotcha: Remember that while Strings are 1-based (starting at 1), Arrays in DWScript are 0-indexed (starting at 0). Using index 0 on a string will result in a runtime error.

Unicode & Surrogate Pairs

DWScript strings are UTF-16 encoded, but the language provides high-level support for handling characters outside the Basic Multilingual Plane (BMP), such as emojis.

When iterating over a string using a for in loop, DWScript iterates over full Unicode characters (code points), automatically handling surrogate pairs.

var s := 'Ready ';

PrintLn('Length: ' + s.Length.ToString); // Length is in UTF-16 code units

for var c in s do
  Print(c + '|');
Result
Length: 8
R|e|a|d|y| ||

Note that s.Length returns the number of UTF-16 code units (where the rocket emoji counts as 2), but the for in loop correctly identifies it as a single character.

Common Operations

DWScript supports Unified Method Syntax, allowing you to call string functions as methods.

Concatenation

Use the + operator to join strings. Explicitly convert non-string types using .ToString.

var count := 42;
var s := 'Count: ' + count.ToString;
var name := 'Alice';
PrintLn('Hello, ' + name + '!');
Result
Hello, Alice!

Search & Logic

You can easily check for substrings or patterns.

var s := 'The quick brown fox';

if 'quick' in s then PrintLn('Found it!'); // 'in' operator support
if s.StartsWith('The') then PrintLn('Starts with The');
if s.Contains('brown') then PrintLn('Contains brown');

var idx := s.IndexOf('fox'); // 17
PrintLn(idx.ToString);
Result
Found it!
Starts with The
Contains brown
17

Manipulation

Methods for modifying or extracting parts of a string return a new string. For the complete list of methods, see String Manipulation.

var s := '  DWScript is awesome  ';

PrintLn(s.Trim);             // "DWScript is awesome"
PrintLn(s.ToUpper);          // "  DWSCRIPT IS AWESOME  "
PrintLn(StrReplace(s, 'awesome', 'powerful')); 

// Slicing and Substrings
PrintLn(s.Copy(3, 8));  // "DWScript"
Result
DWScript is awesome
  DWSCRIPT IS AWESOME  
  DWScript is powerful  
DWScript

Splitting & Joining

Working with lists of strings is seamless.

var csv := 'apple,banana,cherry';
var fruits := csv.Split(',');

PrintLn(fruits.Join('; ')); // "apple; banana; cherry"
Result
apple; banana; cherry

Formatting

For complex string building, use the Format function (similar to printf).

var s := Format(
  'Name: %s, Age: %d',
  [ 'Alice', 30 ]
);
PrintLn(s);
Result
Name: Alice, Age: 30

Conversions

Convert strings to numbers using method helpers or standard functions.

var i := '123'.ToInteger;
var f := '3.14'.ToFloat;
PrintLn(i.ToString);
PrintLn(f.ToString);

// Safe parsing (returns default if failed)
var val := StrToIntDef('abc', 0); 
PrintLn(val.ToString);
Result
123
3.14
0

Related Reference

On this page