Jamie Balfour

Welcome to my personal website.

Find out more about me, my personal projects, reviews, courses and much more here.

Official ZPE/YASS documentationString data type

The String data type is one of the most commonly used data types within ZPE/YASS. String data types contain character sequences stored in a single array. As such, a string can have many operations similar to a list carried out on it. A string data type in ZPE is surrounded by quotation marks.

Examples of values that the String data type could contain include "Hello"

Escape sequences in YASS

YASS has supported escape sequences for some time. Escape sequences allow you to include quote marks within a string without having to use other methods such as inline documents or performing splits or substrings.

YASS
print("Hello \"World\"")

This will print:

Hello "World"

Unescaped strings (ZPE 1.12.7+)

Some times it may be necessary to avoid escaping in a string literal. This might be if you need to write something such as a Windows file path which utilise the \ symbol. ZPE 1.12.7 (July 2024, Loch Lion) introduced unescaping string literals. To do this, prepend the string literal with a @ sign:

YASS
print(@"C:\Program Files\ZPE\")

If the string wasn't escaped in the above, the parser would take the slash at the end as being an escape slash and would try to escape the quote mark.

String templating

String templating was added in ZPE 1.14.1 (Coppergate). A string template is a powerful tool for including expressions within a string that will be evaluated at runtime.

Templating strings starts and ends with a backtick (`). Expressions are encapsulated within {{ and }}:

YASS
$name = input("What is your name?")
$message = `Hello there {{$name}}`
print($message)

You can also use mathematics and function calls, in fact any expression, within a string template:

YASS

function genRandom()

  return random_number(0, 10)

end function

$num = input("Enter your number")
$message = `Your number mulitplied by three is {{$num * 3}}`
print($message)

$message = `Your number mulitplied by a random number is {{$num * genRandom()}}`
print($message)
As of ZPE 1.14.1, string templates can only be assigned to variables and cannot be used in a parameter, for example. ZPE 1.14.2 fixes this.

As a reference type

As of version 1.8.12, strings are handled very differently and act as reference types. This means several things for the string. This first is that using the character index (e.g. $v[0]) and setting a value will change any references to the string. As well as this, using reference functions (also added in version 1.8.12) such as a cut on a string will change the original value of the string across the whole program.

Reference functions (ZPE 1.8.12+)

The String data type provides several reference functions that allow for ease of use:

YASS
$str1 = "Hello world"

//Using TYPO (version 1.9.7+)
declare str2 as string = "Hello world"
  
get (number pos) ⇒ string

Returns a character from a position in a string.

append (string str) ⇒ string

Appends a string to the current string

prepend (string str) ⇒ string

Prepends a string to the current string

cut (number start, number end) ⇒ string

Returns a substring from start to end and removes it from the string.

length () ⇒ number

Returns the length of the string.

Comments

There are no comments on this page.

New comment

Comments are welcome and encouraged, including disagreement and critique. However, this is not a space for abuse. Disagreement is welcome; personal attacks, harassment, or hate will be removed instantly. This site reflects personal opinions, not universal truths. If you can’t distinguish between the two, this probably isn’t the place for you. The system temporarily stores IP addresses and browser user agents for the purposes of spam prevention, moderation, and safeguarding. This data is automatically removed after fourteen days.

Comments powered by BalfComment

Feedback 👍
Comments are sent via email to me.