Before Strings: 4. Oversimplified Variables — A name for a value. 🟢

TL:DR

Function / Operator Operation Short description Example Result
+ Combine Joins strings together "Hello" + " World" "Hello World"
== Compare Checks if two strings are equal if (name == "Houdini Oversimplified") 1
[] Get part Extracts part of a string "Houdini"[0:4] "Houd"
replace() Change text Replaces part of a string replace("Hello World", "World", "Houdini") "Hello Houdini"
atof() String → Float Converts text to a float atof("10.5") 10.5
atoi() String → Integer Converts text to an integer atoi("10") 10
itof() Integer → Float Converts an integer to a float itof(10) 10.0
sprintf() Value → String Formats a value as text sprintf("%f", 10.5) "10.500000"
printf() Value → Console Prints a value to the console printf("%f", 10.5) 10.500000

Fundamentals

A string is a value that stores text.

string name = "Houdini Oversimplified";

Here, name is a variable that contains the text "Houdini Oversimplified".

Unlike numbers, strings aren't primarily used for arithmetic.

They're used for words, names, labels, paths, and other text data.

In Vex

string name = "Houdini Oversimplified";
string path = "/obj/geo1";
string group = "mygroup";

s@name = name;

The String Commandments:

sheet_strings_gerardojustel_re1mon.png

Manipulating Strings

1. Combine Strings

// Create two string variables
string first = "Hello";
string second = "World";

// Combine the strings into a new string
string message = first + " " + second;

Result

Hello World

2. Compare Strings

string name = "Houdini Oversimplified";

if (name == "Houdini Oversimplified")
{
    // Do something
}

== checks whether the string contain the same text.

You can also check if they are different:

if (name != "Houdini Oversimplified")
{
    // Do something
}

3. Get part of a string

// Create a string variable
string word = "Houdini";

// Extract characters from index 0 up to (but not including) index 4
string part = word[0:4];

// Store the result as a string attribute
s@part = part;

Result

Houd

4. Change text

// Create a string variable
string text = "Hello World";

// Replace "World" with "Houdini"
text = replace(text, "World", "Houdini");

Result

Hello Houdini

String Casting

String to Float

atof() means ASCII to float.

// Create a string variable containing a number as text
string a = "10.5";

// Convert the string to a float
float value = atof(a);

String to Integer

atoi() means ASCII to integer.

// Create a string variable from the "a" parameter
string a = chs("a");

// Convert the string to an integer and store it as an integer attribute
i@val = atoi(a);

<aside> ⚠️

Important

The string needs to contain something that can be interpreted as a number.

</aside>

Formatting a value into a string

Integer → String

Use itoa() to convert an integer into a string.

// Create an integer variable
int a = 1;

// Convert the integer to a string and store it as a string attribute
s@val = itoa(a);

Float → String

Use sprintf() to format the number as text:

// Create a float variable
float b = 10.5678;

// Format the float as a string and store it as a string attribute
s@val = sprintf("%f", b);

You can also control the number of decimal places:

s@val = sprintf("%.2f", b);

For example, 10.5678 becomes "10.57".

Printing Values

sprintf() can also be used with printf() to print formatted values to the console:

printf("%f", b);

This does not convert b.

It simply tells VEX:

Print this float using this format.

Continue reading: