Strings and Runes in Scilab

Our example will demonstrate how strings and characters (called “runes” in some languages) are handled in Scilab. Scilab treats strings as arrays of characters, and it uses UTF-8 encoding for string literals.

// Define a string with Thai characters
s = "สวัสดี"

// Print the length of the string
disp("Len: " + string(length(s)))

// Print the ASCII values of each character
disp("ASCII values:")
for i = 1:length(s)
    printf("%d ", ascii(part(s, i)))
end
disp(" ")

// Count the number of characters
disp("Character count: " + string(length(s)))

// Iterate over each character in the string
disp("Characters and their positions:")
for i = 1:length(s)
    printf("'%s' starts at %d\n", part(s, i), i)
end

// Define a function to examine a character
function examineChar(c)
    if c == "t" then
        disp("found tee")
    elseif c == "ส" then
        disp("found so sua")
    end
endfunction

// Use the examineChar function
disp("Examining characters:")
for i = 1:length(s)
    examineChar(part(s, i))
end

In this Scilab code:

  1. We define a string s containing Thai characters.

  2. We use length(s) to get the length of the string, which corresponds to the number of characters.

  3. We iterate through the string using a for loop and the part() function to access individual characters. We print their ASCII values using the ascii() function.

  4. We demonstrate iterating over the string again, this time printing each character and its position.

  5. We define a function examineChar that checks if a character matches certain values. This is similar to comparing runes in some other languages.

  6. Finally, we use the examineChar function on each character of our string.

Note that Scilab doesn’t have a direct equivalent to “runes” or Unicode code points. It treats strings as sequences of characters and doesn’t provide built-in functions for working with Unicode code points directly. The ascii() function will return the ASCII value for ASCII characters, but may not work as expected for non-ASCII Unicode characters.

To run this Scilab script, save it to a file (e.g., string_example.sce) and execute it in Scilab:

--> exec('string_example.sce', -1)

This will display the results of our string operations and character examinations.