HP Prime/Commands/Guides/All About Strings

De TI-Planet Wiki
Aller à la navigation Aller à la recherche
La version imprimable n’est plus prise en charge et peut comporter des erreurs de génération. Veuillez mettre à jour les signets de votre navigateur et utiliser à la place la fonction d’impression par défaut de celui-ci.
This is a backup copy of the article from the old "HPWiki" of TI-Planet (that only hosted HP Prime info), initially authored by Iconmaster

The string data type has a type ID of 2.

Making Constants

String constants are enclosed "like this". An unclosed string constant is always a syntax error.

You can use backslash escapes in string constants. The valid escapes are the following:

Escape code Meaning
\n Newline
\r Carriage Return
\t Tab
\\ Backslash
\" Double-quote
"" Double-quote
\0 NUL
\nnn Where 'nnn' is an octal number: The literal character with code nnn. '100', the lowest valid nnn, is @.
\Xnn Where 'nn' is an hex number: The literal character with code nn.
\unnnn Where 'nnnn' is an hex number: The literal character with code nn. The 16-bit version of \X.

Use

String variables are indexable. Getting a number from a string results in a character code, and setting it sets a character code:

LOCAL s := "Hello";
PRINT(s[1]);
s[1] := 52;

The number has to be in range, else an error is thrown. Decimals are rounded down to integers.

Getting a substring can be done by indexing a string variable with two arguments. The first one is index, and the second is the number of characters to get:

LOCAL s := "Hello";
PRINT(s[2,2]);

The second number has to be any number greater than or equal to zero.

Setting an index of a string with another string performs a limited replacement.

LOCAL s := "Hello";
s[1] := "World";

The length of the string assigned to is preserved; that is, if the replacement would make the string longer, it is truncated instead.

Limits

Strings may have at maximum 65,535 (2^16-1) characters in them. Attempting to create a longer string will result in error 38 being thrown.