Strings
Introduction
Strings are a sequence of characters enclosed in either single quotes (''
), double quotes (""
), or backticks (``
). Single quotes and double quotes are working the same way, but backticks (string templates) have some special features.
Creating Strings
To create a string, simply enclose a sequence of characters in single or double quotes.ย For example:
let str1 = 'Hello, world!';
let str2 = "MagiScript is awesome!";
MagiScript also supports template strings, which are enclosed in backticks (``
) instead of single or double quotes. Template strings allow multiple lines and also embed expressions inside a string using placeholders. Placeholder expressions are enclosed in ${}
. For example:
let name = "MagiScript";
let message = `Hello, ${name}!`;
String Methods
Below are the String Methods:
strLen
strLen
ย is a built-in function in MagiScript that returns the length of a string. It takes one argument, which is the string to be measured.ย For example:
let myStr = "MagiScript";
let len = strLen(myStr); // len is 10
strSub
strSub
ย is a built-in function in MagiScript that extracts a substring from a string. It takes two arguments: the string from which the substring will be extracted, and the starting index (inclusive) and the ending index (exclusive) of the substring. For example:
let myStr = "MagiScript";
let subStr = strSub(myStr, 0, 4); // subStr is "Magi"
strCharAt
strCharAt
ย is a built-in function in MagiScript that returns the character at a specified index in a string. It takes two arguments: the string from which the character will be extracted, and the index of the character.ย For example:
let myStr = "MagiScript";
let char = strCharAt(myStr, 2); // char is "g"
strSplit
strSplit
is a built-in function in MagiScript that splits a string into an array of substrings. It takes two arguments: the string to be split, and the separator character or substring. For example:
let myStr = "MagiScript is awesome!";
let words = strSplit(myStr, " "); // words is ["MagiScript", "is", "awesome!"]
Conclusion
In MagiScript, strings are a fundamental data type that can be created using single or double quotes or backticks for template strings. While MagiScript doesnโt support the length property of strings, and accessing the letter at an index using brackets (str[2]), we have strLen
, strSub
, strCharAt
and strSplit
to help manipulate and extract information from strings.
General
๐ย First Steps
MagiScript Editor
๐ย Basics
๐ย Running Your First Program
๐ย App Store
๐ย Atom Settings (Editor)
๐ย Debugging Techniques
Examples
๐ย Atom Time
๐๏ธ Time Practice
๐ย Atom Pi (Pi Revelations)
๐ย Atom Drum
๐๏ธ Atom Stack
๐ย Atom Square
๐ย Atom Level
๐ย Atom THMPR
๐ย Poker Hands
๐ย Keyboard Numeric
๐ย Keyboard NOKIA
๐ย Keyboard Cursor
๐ย Keyboard Media
๐ย Keyboard Custom
Input/Output
๐ย Buttons
๐ย Vibration Motor
๐ย RGB LED
๐ย Devices
๐ย PeekSmith
๐ย SB Watch
๐ย Bluetooth Keyboard
๐ย Bluetooth Mouse
๐ย Timers
๐ย Database
๐ย Events
๐ย System (exit, sleep, rand)
๐ย Objects (card, time)
Language
๐ย Summary
๐ย Comments
๐ย Variable Declarations
๐ย Operators
๐ย Control Flow
๐ย Functions
๐ย Numbers
๐ย Strings
๐ย Arrays
๐ย Objects
๐ย Uint8Array