# 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 `''`.
Standard C-style escape sequences like `\n` or `\t` are **not** supported in single-quoted strings. Use `#` codes instead (e.g., `#13#10` for newline).
```pascal
// 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.
```pascal
var multi := "This is a
multi-line string.";
PrintLn(multi);
// OUTPUT
// This is a
// multi-line string.
```
### Triple Quoted Strings (Delphi Compatible)
Triple apostrophes `'''` define multiline strings. DWScript performs **smart indentation stripping** based on the indentation of the closing `'''`.
```pascal
var msg := '''
Hello
World
''';
// The 2-space indent is stripped because the closing ''' matches it.
```
### 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.
```pascal
// 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.
```pascal
uses System.WebServer;
resourcestring
rsGreeting = 'Hello, %s!';
resourcestring
rsError = 'An unexpected error occurred.';
PrintLn(Format(rsGreeting, ['Alice']));
// OUTPUT
// Hello, Alice!
```
## Character Literals
Individual characters can be represented by their ASCII/Unicode code using the `#` prefix.
```pascal
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`.
```pascal
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] + ' ');
PrintLn('.');
// OUTPUT
// First char: D
// Last char: t
// D W S c r i p t .
```
:::warning
**Indexing Gotcha:** Strings are **1-based**. See **[Indexing Strategy](/lang/basics/types#indexing-strategy)** for the comparison with Arrays.
:::
## 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. The loop variable is of type **String**.
```pascal
var s := 'Ready 🚀';
PrintLn('Length: ' + s.Length.ToString); // Length is in UTF-16 code units
for var c in s do
Print(c + '|');
// OUTPUT
// 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`.
```pascal
var count := 42;
var s := 'Count: ' + count.ToString;
var name := 'Alice';
PrintLn('Hello, ' + name + '!');
// OUTPUT
// Hello, Alice!
```
### Search & Logic
You can easily check for substrings or patterns.
```pascal
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);
// OUTPUT
// 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](/ref/strings_manipulation)**.
```pascal
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"
// OUTPUT
// DWScript is awesome
// DWSCRIPT IS AWESOME
// DWScript is powerful
// DWScript
```
### Splitting & Joining
Working with lists of strings is seamless.
```pascal
var csv := 'apple,banana,cherry';
var fruits := csv.Split(',');
PrintLn(fruits.Join('; ')); // "apple; banana; cherry"
// OUTPUT
// apple; banana; cherry
```
### Formatting
For complex string building, use the `Format` function (similar to printf).
```pascal
var s := Format(
'Name: %s, Age: %d',
[ 'Alice', 30 ]
);
PrintLn(s);
// OUTPUT
// Name: Alice, Age: 30
```
## Conversions
Convert strings to numbers using method helpers or standard functions.
```pascal
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);
// OUTPUT
// 123
// 3.14
// 0
```
:::info
### Related Reference
* **[String Manipulation](/ref/strings_manipulation)** - Core functions, substrings, and splitting.
* **[String Transformation](/ref/strings_transform)** - Case conversion, trimming, and type conversion.
* **[String Formatting](/ref/strings_format)** - Format function and templating.
* **[String Comparisons](/ref/strings_comparison)** - Equality and wildcard matching.
* **[Encodings Reference](/ref/encoding)** - Base64, Hex, and URL encoding.
:::
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 ''.
Standard C-style escape sequences like \n or \t are not supported in single-quoted strings. Use # codes instead (e.g., #13#10 for newline).
// Type inferencevar s :='It''s a beautiful day';// Explicit typingvar 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. DWScript performs smart indentation stripping based on the indentation of the closing '''.
var msg :='''
Hello
World
''';// The 2-space indent is stripped because the closing ''' matches it.
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 strippedvar 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.
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 charactersforvar i :=1to s.Length do
Print(s[i]+' ');
PrintLn('.');
Result
First char: D
Last char: t
D W S c r i p t .
Indexing Gotcha: Strings are 1-based. See Indexing Strategy for the comparison with Arrays.
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. The loop variable is of type String.
var s :='Ready 🚀';
PrintLn('Length: '+ s.Length.ToString);// Length is in UTF-16 code unitsforvar 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 supportif 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
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);