Tutorial 17

Strings in Flogram

Characters can be used to represent letters or numbers. Strings are a sequence of characters and are one of the most common data types used in programming. In Flogram, strings are represented as arrays of characters, providing compatibility with JavaScript.

Declaring a String

Strings in Flogram can be declared as such:

fn main():
    myString: String = "Hello"
    draw(myString)

Accessing Characters

Strings are effectively just arrays of characters, you can access individual characters using their index, just like you would with an array:

firstChar := myString[0]  # This will get the character 'H' from the string and store it into ‘firstChar’

Concatenating Strings

You can concatenate strings using {} within a string which will expand variables into strings:

fn main():
    greeting := "Hello"
    name := "Flogram user"
    fullGreeting := "{greeting} {name}"  
    # fullGreeting is now equal to "Hello Flogram user"
    draw(fullGreeting)

String Length

You can find the length of a string using the size function, the same way you would with an array:

length := size(myString)

Example Code

Here’s a complete code example that demonstrates the above concepts related to strings. You can run this code in the Flogram IDE.

fn main():
    greeting := "Hello"
    name := "Flogram user"
    fullGreeting := "{greeting} {name}!"
    draw(fullGreeting) # Outputs: Hello Flogram user!
    length := size(fullGreeting)
    draw("Greeting length: {length}") # Outputs: Greeting length: 19

Strings are versatile and essential in any programming language, providing a way to handle text data. In Flogram, strings are treated as arrays of characters, allowing for convenient manipulation and access to individual characters.

Last updated on Jun 07, 2024

terminal
Terminal output
$ Run your code to see output here.
$