-
Notifications
You must be signed in to change notification settings - Fork 0
Strings
Strings are a very basic data type in MochaScript. They are created as text wrapped in double quotes (") and have a number of useful operations.
Strings can be added, or concatenated, to each other and any other data type with the plus operator. This allows for dynamic formatting of strings with other values. For example:
>>> "Hello " + "world!"
"Hello, world!"
>>> "Numbers too! " + 4
"Numbers too! 4"
>>> "Strings and other things!" + [1, 2, 3]
"Strings and other things![1, 2, 3]"Currently, MochaScript supports very basic string formatting. This is done using the modulo operator (%) and curly brackets ({}) inside a string. Modding a string s with a value v replaces any occurrences of {} in s with the repr() of v. Here's an example:
>>> "Hello, {}!" % "world"
"Hello, world!"
>>> "2 plus 3 is: {}! {} again" % (2 + 3)
"2 plus 3 is: 5! 5 again"In most programming languages, the minus operator (-) has no use on strings. However, in MochaScript, strings can be subtracted with any other values. String subtraction removes all occurrences of the second value from the first, which, of course, must be a string.
>>> "Hello there!" - " there"
"Hello!"
>>> "ABC-123" - 1
"ABC-23"Like in many other languages, strings can be multiplied with numbers. Here's an example:
>>> "Hello" * 3
"HelloHelloHello"Copyright (C) 2022, Hedge Fleming hedgefleming@gmail.com